From 01bb8f76e43840ca384939d118c2bb9839830d48 Mon Sep 17 00:00:00 2001 From: lihong Date: Tue, 19 Dec 2023 15:12:25 +0800 Subject: [PATCH] lihong67@huawei.com support gn build for third/parse5. Signed-off-by: lihong Change-Id: I259f4179cf98553b05fa953c5e8661ad8f40d6de --- BUILD.gn | 74 +++++++++++++++++++++++++++++ build_parse5.py | 79 +++++++++++++++++++++++++++++++ packages/parse5/package-lock.json | 5 ++ packages/parse5/package.json | 3 +- packages/parse5/tsconfig.json | 5 +- packages/parse5/uglify-source.js | 44 +++++++++++++++++ tsconfig.json | 2 +- 7 files changed, 209 insertions(+), 3 deletions(-) create mode 100644 BUILD.gn create mode 100755 build_parse5.py create mode 100644 packages/parse5/uglify-source.js diff --git a/BUILD.gn b/BUILD.gn new file mode 100644 index 0000000..2b60f40 --- /dev/null +++ b/BUILD.gn @@ -0,0 +1,74 @@ +# Copyright (c) 2023 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. + +import("//build/ohos.gni") +import("//build/ohos/ace/ace.gni") +import("//foundation/arkui/ace_engine/ace_config.gni") + +parse5_lib_dir = + get_label_info(":build_parse5_library", "target_out_dir") + "/parse" +_parse5_project_dir = "//third_party/parse5/packages/parse5" + +action("build_parse5_library") { + script = "build_parse5.py" + depfile = "$target_gen_dir/$target_name.d" + outputs = [ parse5_lib_dir ] + + _tsc_js = _parse5_project_dir + "/node_modules/typescript/bin/tsc" + _uglify_source_js = _parse5_project_dir + "/uglify-source.js" + + inputs = [ + _tsc_js, + _uglify_source_js, + ] + + nodejs_path = "//prebuilts/build-tools/common/nodejs/current/bin/node" + + args = [ + "--depfile", + rebase_path(depfile, root_build_dir), + "--node", + rebase_path(nodejs_path, root_build_dir), + "--tsc-js", + rebase_path(_tsc_js, root_build_dir), + "--parse5-project", + rebase_path(_parse5_project_dir, root_build_dir), + "--parse5-output-dir", + rebase_path(parse5_lib_dir, root_build_dir), + "--uglify-source-js", + rebase_path(_uglify_source_js, root_build_dir), + ] +} + +ohos_copy("parse5") { + deps = [ ":build_parse5_library" ] + sources = [ parse5_lib_dir ] + outputs = [ target_out_dir + "/$target_name" ] + module_install_name = "parse" + subsystem_name = "thirdparty" + part_name = "parse5" + license_file = "//third_party/parse5/LICENSE" +} + +ace_loader_ark_dir = get_label_info("//developtools/ace_js2bundle:ace_loader", + "target_out_dir") + "/ace_loader_ark" + +ohos_copy("parse5_ark_hap") { + deps = [ + ":build_parse5_library", + ":parse5", + "//developtools/ace_js2bundle:ace_loader_ark_hap", + ] + sources = [ parse5_lib_dir ] + outputs = [ ace_loader_ark_dir + "/lib/parse" ] +} diff --git a/build_parse5.py b/build_parse5.py new file mode 100755 index 0000000..0d6d265 --- /dev/null +++ b/build_parse5.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (c) 2020 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. + +import os +import sys +import subprocess +import argparse + +standard_system_build_dir = os.path.join(os.path.dirname(__file__), os.pardir, + os.pardir, 'build', 'scripts', 'util') +build_dir = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, + os.pardir, os.pardir, os.pardir, 'build', 'maple', 'java', 'util') +if os.path.exists(standard_system_build_dir): + sys.path.append( + os.path.join(standard_system_build_dir, os.pardir, os.pardir)) + from scripts.util import build_utils # noqa: E402 +if os.path.exists(build_dir): + sys.path.append(os.path.join(build_dir, os.pardir, os.pardir, os.pardir)) + from maple.java.util import build_utils # noqa: E402 + + +def parse_args(): + + parser = argparse.ArgumentParser() + build_utils.add_depfile_option(parser) + + parser.add_argument('--node', help='path to nodejs exetuable') + parser.add_argument('--tsc-js', help='path to parse5 module tsc') + parser.add_argument('--parse5-project', help='path to parse5 project') + parser.add_argument('--parse5-output-dir', help='path to parse5 output') + parser.add_argument('--uglify-source-js', help='path uglify-source.js') + + options = parser.parse_args() + return options + + +def do_build(build_cmd, uglify_cmd): + for cmd in [build_cmd, uglify_cmd]: + build_utils.check_output(cmd) + + +def main(): + options = parse_args() + + build_cmd = [ + options.node, options.tsc_js + ] + build_cmd.extend(['--project', options.parse5_project]) + build_cmd.extend(['--outDir', options.parse5_output_dir]) + build_cmd.extend(['--module', 'CommonJS']) + build_cmd.extend(['--target', 'ES6']) + depfile_deps = [options.node, options.tsc_js] + depfile_deps.extend(build_utils.get_all_files(options.parse5_project)) + + uglify_cmd = [options.node, options.uglify_source_js, options.parse5_output_dir] + depfile_deps.append(options.uglify_source_js) + + build_utils.call_and_write_depfile_if_stale( + lambda: do_build(build_cmd, uglify_cmd), + options, + depfile_deps=depfile_deps, + input_paths=depfile_deps, + output_paths=([options.parse5_output_dir])) + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/packages/parse5/package-lock.json b/packages/parse5/package-lock.json index 9f3ded2..b90f674 100644 --- a/packages/parse5/package-lock.json +++ b/packages/parse5/package-lock.json @@ -13,6 +13,11 @@ "version": "4.9.5", "resolved": "https://repo.huaweicloud.com/repository/npm/typescript/-/typescript-4.9.5.tgz", "integrity": "sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==" + }, + "uglify-js": { + "version": "3.17.4", + "resolved": "https://repo.huaweicloud.com/repository/npm/uglify-js/-/uglify-js-3.17.4.tgz", + "integrity": "sha512-T9q82TJI9e/C1TAxYvfb16xO120tMVFZrGA3f9/P4424DNu6ypK103y0GPFVa17yotwSyZW5iYXgjYHkGrJW/g==" } } } diff --git a/packages/parse5/package.json b/packages/parse5/package.json index e45057e..e0e38d1 100644 --- a/packages/parse5/package.json +++ b/packages/parse5/package.json @@ -9,7 +9,8 @@ "funding": "https://github.com/inikulin/parse5?sponsor=1", "dependencies": { "entities": "^4.3.0", - "typescript": "^4.9.5" + "typescript": "^4.9.5", + "uglify-js": "3.17.4" }, "keywords": [ "html", diff --git a/packages/parse5/tsconfig.json b/packages/parse5/tsconfig.json index 137ddf4..df169b0 100644 --- a/packages/parse5/tsconfig.json +++ b/packages/parse5/tsconfig.json @@ -5,7 +5,10 @@ "outDir": "dist", "lib": [ "es2020" - ] + ], + "declaration": false, + "declarationMap": false, + "sourceMap": false }, "include": ["**/*.ts"], "exclude": ["**/*.test.ts", "dist"] diff --git a/packages/parse5/uglify-source.js b/packages/parse5/uglify-source.js new file mode 100644 index 0000000..c58a056 --- /dev/null +++ b/packages/parse5/uglify-source.js @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021 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. + */ +import fs from 'fs'; +import path from 'path'; +import uglifyJS from 'uglify-js'; + +const SOURCE_POSITION = 2; +readCode(process.argv[SOURCE_POSITION]); + +function readCode(inputPath) { + if (fs.existsSync(inputPath)) { + const files = fs.readdirSync(inputPath); + files.forEach(function(file) { + const filePath = path.join(inputPath, file); + if (fs.existsSync(filePath)) { + const fileStat = fs.statSync(filePath); + if (fileStat.isFile()) { + const code = fs.readFileSync(filePath, 'utf-8'); + uglifyCode(code, filePath); + } + if (fileStat.isDirectory()) { + readCode(filePath); + } + } + }); + } +} + +function uglifyCode(code, outPath) { + const uglifyCode = uglifyJS.minify(code).code; + fs.writeFileSync(outPath, uglifyCode); +} diff --git a/tsconfig.json b/tsconfig.json index 5f1d46d..35bcaf0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,7 +14,7 @@ "importsNotUsedAsValues": "error", "isolatedModules": true, - "composite": true, + "composite": false, "declarationMap": true, "sourceMap": true }, -- Gitee