From 98cdd784df2687a0663e4142c42715615922bc79 Mon Sep 17 00:00:00 2001 From: yinchuang Date: Thu, 25 Aug 2022 22:34:25 +0800 Subject: [PATCH] Optimize build js Issue:https://gitee.com/openharmony/third_party_jsframework/issues/I5NJ0I Signed-off-by: yinchuang --- BUILD.gn | 9 ++- README.md | 4 +- build.js => build_jsmock_system_plugin.js | 32 +------- build_strip_native_min.js | 99 +++++++++++++++++++++++ js_framework_build.sh | 17 ++-- package.json | 2 +- 6 files changed, 121 insertions(+), 42 deletions(-) rename build.js => build_jsmock_system_plugin.js (80%) create mode 100644 build_strip_native_min.js diff --git a/BUILD.gn b/BUILD.gn index e39ecde3..ab637329 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -58,7 +58,8 @@ action("gen_snapshot") { } } - buildfile = "//third_party/jsframework/build.js" + buildfile_jsmock = "//third_party/jsframework/build_jsmock_system_plugin.js" + buildfile_native_min = "//third_party/jsframework/build_strip_native_min.js" package_file = "//third_party/jsframework/package.json" tsconfig = "//third_party/jsframework/tsconfig.json" @@ -67,7 +68,7 @@ action("gen_snapshot") { test_file = "//third_party/jsframework/test" args = [ - rebase_path(buildfile, root_build_dir), + rebase_path(buildfile_jsmock, root_build_dir), rebase_path(nodejs_path, root_build_dir), rebase_path(js_framework, root_build_dir), rebase_path(node_modules, root_build_dir), @@ -79,6 +80,7 @@ action("gen_snapshot") { rebase_path(babel, root_build_dir), is_mac, rebase_path("//prebuilts", root_build_dir), + rebase_path(buildfile_native_min, root_build_dir), ] inputs = [ @@ -172,7 +174,8 @@ action("gen_snapshot") { # config of compiler ".babelrc", ".eslintrc", - "build.js", + "build_jsmock_system_plugin.js", + "build_strip_native_min.js", "tsconfig.json", "tslint.json", ] diff --git a/README.md b/README.md index 23b70ece..732335c1 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ - `test/ut/`:unit test file - `.eslintrc`:eslint configure - `BUILD.gn`:compiling file of JS UI framework for NinjaJS -- `build.js`:build JS framework +- `build_jsmock_system_plugin.js, build_strip_native_min.js`: build JS framework - `js_framework_build.sh`:script file for JS framework building - `LICENSE`:Apache License - `NOTICE`:third party open source software notice @@ -35,7 +35,7 @@ Please make sure that the following commands work before trying to build: Your `npm` and `node` should be of a later version. You can upgrade them to the **latest stable version**. ### 2. Installing -RollUp tool for packaging has been configured in `build.js`. So after the preceding conditions are met, we can start installing right now. +RollUp tool for packaging has been configured in `build_jsmock_system_plugin.js, build_strip_native_min.js`. So after the preceding conditions are met, we can start installing right now. First, we go to the root directory of the project: ``` diff --git a/build.js b/build_jsmock_system_plugin.js similarity index 80% rename from build.js rename to build_jsmock_system_plugin.js index 25a59fad..3676afde 100644 --- a/build.js +++ b/build_jsmock_system_plugin.js @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -57,22 +57,6 @@ const esPlugin = eslint({ exclude: ['node_modules/**', 'lib/**'] }); -const configInput = { - input: path.resolve(__dirname, 'runtime/preparation/index.ts'), - onwarn, - plugins: [ - esPlugin, - tsPlugin, - json(), - resolve(), - commonjs(), - babel({ - exclude: 'node_moduels/**' - }), - uglify() - ] -}; - const configJSAPIMockInput = { input: path.resolve(__dirname, 'runtime/main/extend/systemplugin/entry.js'), onwarn, @@ -88,24 +72,12 @@ const configJSAPIMockInput = { ] }; -const configOutput = { - file: path.resolve(__dirname, 'dist/strip.native.min.js'), - format: 'umd', - banner: frameworkBanner -}; - const configJSAPIMockOutput = { file: path.resolve(__dirname, 'dist/jsMockSystemPlugin.js'), format: 'umd', banner: frameworkBannerForJSAPIMock }; -rollup.rollup(configInput).then(bundle => { - bundle.write(configOutput).then(() => { - countSize(configOutput.file); - }); -}); - rollup.rollup(configJSAPIMockInput).then(bundle => { bundle.write(configJSAPIMockOutput).then(() => { countSize(configJSAPIMockOutput.file); @@ -114,7 +86,7 @@ rollup.rollup(configJSAPIMockInput).then(bundle => { function countSize(filePath) { const file = path.relative(__dirname, filePath); - fs.stat(filePath, function(error, stats) { + fs.stat(filePath, function (error, stats) { if (error) { console.error('file size is wrong'); } else { diff --git a/build_strip_native_min.js b/build_strip_native_min.js new file mode 100644 index 00000000..2861f0d5 --- /dev/null +++ b/build_strip_native_min.js @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const fs = require('fs'); + +const path = require('path'); + +const rollup = require('rollup'); + +const resolve = require('rollup-plugin-node-resolve'); + +const commonjs = require('rollup-plugin-commonjs'); + +const json = require('rollup-plugin-json'); + +const babel = require('rollup-plugin-babel'); + +const typescript = require('rollup-plugin-typescript2'); + +const { uglify } = require('rollup-plugin-uglify'); + +const { + eslint +} = require('rollup-plugin-eslint'); + +const frameworkBanner = `var global=this; var process={env:{}}; ` + `var setTimeout=global.setTimeout;\n`; + +const frameworkBannerForJSAPIMock = `var global=globalThis;`; + +const onwarn = warning => { + // Silence circular dependency warning + if (warning.code === 'CIRCULAR_DEPENDENCY') { + return; + } + console.warn(`(!) ${warning.message}`); +}; + +const tsPlugin = typescript({ + tsconfig: path.resolve(__dirname, 'tsconfig.json'), + check: true +}); + +const esPlugin = eslint({ + include: ['**/*.ts'], + exclude: ['node_modules/**', 'lib/**'] +}); + +const configInput = { + input: path.resolve(__dirname, 'runtime/preparation/index.ts'), + onwarn, + plugins: [ + esPlugin, + tsPlugin, + json(), + resolve(), + commonjs(), + babel({ + exclude: 'node_moduels/**' + }), + uglify() + ] +}; + +const configOutput = { + file: path.resolve(__dirname, 'dist/strip.native.min.js'), + format: 'umd', + banner: frameworkBanner +}; + +rollup.rollup(configInput).then(bundle => { + bundle.write(configOutput).then(() => { + countSize(configOutput.file); + }); +}); + +function countSize(filePath) { + const file = path.relative(__dirname, filePath); + fs.stat(filePath, function (error, stats) { + if (error) { + console.error('file size is wrong'); + } else { + const size = (stats.size / 1024).toFixed(2) + 'KB'; + console.log(`generate snapshot file: ${file}...\nthe snapshot file size: ${size}...`); + } + }); +} + diff --git a/js_framework_build.sh b/js_framework_build.sh index 0dedb070..79cd5887 100755 --- a/js_framework_build.sh +++ b/js_framework_build.sh @@ -39,8 +39,8 @@ cp -f $6 $9 cp -f $7 $9 cp -f ${10} $9 cp -f $1 $9 +cp -f ${13} $9 cp -r $8 $9 - if [ -d "$prebuilts_path" ]; then echo "prebuilts exists" # address problme of parallzing compile @@ -49,13 +49,17 @@ if [ -d "$prebuilts_path" ]; then cp -r $2 $9 cd $9 if [ "${11}" == 'true' ];then - ./node-v12.18.4-darwin-x64/bin/node build.js + ./node-v12.18.4-darwin-x64/bin/node build_jsmock_system_plugin.js || exit 1 & + ./node-v12.18.4-darwin-x64/bin/node build_strip_native_min.js || exit 1 & # run unit test - ./node-v12.18.4-darwin-x64/bin/node node_modules/.bin/mocha -r ts-node/register test/lib.ts test/ut/**/*.ts test/ut/*.ts + ./node-v12.18.4-darwin-x64/bin/node node_modules/.bin/mocha -r ts-node/register test/lib.ts test/ut/**/*.ts test/ut/*.ts || exit 1 & + wait else - ./node-v12.18.4-linux-x64/bin/node build.js + ./node-v12.18.4-linux-x64/bin/node build_jsmock_system_plugin.js || exit 1 & + ./node-v12.18.4-linux-x64/bin/node build_strip_native_min.js || exit 1 & # run unit test - ./node-v12.18.4-linux-x64/bin/node node_modules/.bin/mocha -r ts-node/register test/lib.ts test/ut/**/*.ts test/ut/*.ts + ./node-v12.18.4-linux-x64/bin/node node_modules/.bin/mocha -r ts-node/register test/lib.ts test/ut/**/*.ts test/ut/*.ts || exit 1& + wait fi else npm run build @@ -72,7 +76,8 @@ else fi rm -rf ./runtime rm -rf ./tsconfig.json -rm -rf ./build.js +rm -rf build_jsmock_system_plugin.js +rm -rf build_strip_native_min.js rm -rf ./test rm -rf ./.eslintrc rm -rf ./.babelrc diff --git a/package.json b/package.json index fb4031fd..9a7f5cc9 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "scripts": { "test:lint": "eslint -c .eslintrc --fix test/**/*.ts", "test:unit": "mocha --timeout 5000 -r ts-node/register test/lib.ts test/ut/**/*.ts test/ut/*.ts", - "build": "node build.js" + "build": "node build_jsmock_system_plugin.js && node build_strip_native_min.js" }, "keywords": [ "javascript",