diff --git a/arkui-plugins/custom-import-plugin.js b/arkui-plugins/custom-import-plugin.js index 84b8a6bd5847b908683c89111b4447e2b3ac87b3..13bda857c75007547cc77e61af12ad3081c3e7ba 100644 --- a/arkui-plugins/custom-import-plugin.js +++ b/arkui-plugins/custom-import-plugin.js @@ -40,6 +40,24 @@ module.exports = function (babel) { t.variableDeclarator(t.identifier('arkts'), requireCall) ]); + pathNode.replaceWithMultiple([newImport, arkts]); + } + if (sourceValue === '@koalaui/libarkts-mirror' && pathNode.node.specifiers.length === 1 && t.isImportNamespaceSpecifier(pathNode.node.specifiers[0])) { + const currentFileDir = path.dirname(pathNode.hub.file.opts.filename); + const configDir = process.cwd(); + const relativePath = path.relative(currentFileDir, configDir); + const importPath = relativePath ? path.join(relativePath, 'path') : './path'; + + const newImport = t.importDeclaration( + [t.importSpecifier(t.identifier('getArktsMirrorPath'), t.identifier('getArktsMirrorPath'))], + t.stringLiteral(importPath) + ); + + const requireCall = t.callExpression(t.identifier('require'), [t.callExpression(t.identifier('getArktsMirrorPath'), [])]); + const arkts = t.variableDeclaration('const', [ + t.variableDeclarator(t.identifier('mirrorArkts'), requireCall) + ]); + pathNode.replaceWithMultiple([newImport, arkts]); } } diff --git a/arkui-plugins/package.json b/arkui-plugins/package.json index 75420af81786ffa48e32ebf34398eeab351cfaaf..d395ff8427e94db76abb2e553befc5850f952aea 100644 --- a/arkui-plugins/package.json +++ b/arkui-plugins/package.json @@ -31,6 +31,7 @@ "typescript": "^5.0.0" }, "dependencies": { - "@koalaui/libarkts": "../koala-wrapper" + "@koalaui/libarkts": "../koala-wrapper", + "@koalaui/libarkts-mirror": "../koala-wrapper/mirror" } } diff --git a/arkui-plugins/path.ts b/arkui-plugins/path.ts index a8646fcbf9f1a0b882082eca9843bd5441c171d9..d3f82da3fdbf8387d9c337bd6cb0fc6361c13e6b 100644 --- a/arkui-plugins/path.ts +++ b/arkui-plugins/path.ts @@ -59,3 +59,7 @@ export function getCommonPath() { export function getCompatPath() { return path.join(findRootDir(), 'koala-wrapper/koalaui/compat', './dist/src/index.js'); } + +export function getArktsMirrorPath() { + return path.join(findRootDir(), 'koala-wrapper/mirror', './build/arkts-api/index.js'); +} \ No newline at end of file diff --git a/koala-wrapper/BUILD.gn b/koala-wrapper/BUILD.gn index 58807818d720d8eb681392d0927e90d37e035192..6f56b06234b6ce7facceb8694a786fb2fa9e0925 100644 --- a/koala-wrapper/BUILD.gn +++ b/koala-wrapper/BUILD.gn @@ -15,10 +15,12 @@ import("//build/ohos.gni") import("//build/config/components/ets_frontend/ets2abc_config.gni") npm_path = "//prebuilts/build-tools/common/nodejs/current/bin/npm" +mirror_ui2abc_dir = "//foundation/arkui/ace_engine/frameworks/bridge/arkts_frontend/koala_mirror/ui2abc" action("gen_sdk_ts_wrapper") { script = "build_ts_wrapper.py" deps = [ "./native:es2panda" ] + external_deps = [ "ace_engine:es2panda" ] args = [ "--source_path", rebase_path(get_path_info(".", "abspath")), @@ -30,6 +32,8 @@ action("gen_sdk_ts_wrapper") { "$current_os", "--root_out_dir", rebase_path(root_out_dir), + "--mirror_ui2abc_dir", + rebase_path(mirror_ui2abc_dir) ] outputs = [ "$target_gen_dir" ] diff --git a/koala-wrapper/build_ts_wrapper.py b/koala-wrapper/build_ts_wrapper.py index 178f8772f1db122e5203cc1227c9cfad48f4ba31..fb98cf6b8a0c2a16b75fe24b868148d74aea7e62 100755 --- a/koala-wrapper/build_ts_wrapper.py +++ b/koala-wrapper/build_ts_wrapper.py @@ -18,17 +18,28 @@ import os import shutil import subprocess import sys -import tarfile - - -def copy_files(source_path, dest_path, is_file=False): +from pathlib import Path +import re + +def process_mirror_output(root_path): + root = Path(root_path) + js_files = [file for file in root.rglob('*.js') if file.is_file()] + for file in js_files: + content = file.read_text(encoding='utf-8') + new_content = re.sub(r'require\("@koalaui/(\w+?)"\)', r'require("#koalaui/\1")', content) + new_content = re.sub(r'require\("@common/(\w+?)"\)', r'require("#common/\1")', new_content) + new_content = re.sub(r'require\("@platform/(\w+?)"\)', r'require("#platform/\1")', new_content) + if new_content != content: + file.write_text(new_content, encoding='utf-8') + +def copy_files(source_path, dest_path, is_file=False, ignore_func=None): try: if is_file: os.makedirs(os.path.dirname(dest_path), exist_ok=True) shutil.copy(source_path, dest_path) else: shutil.copytree(source_path, dest_path, dirs_exist_ok=True, - symlinks=True) + symlinks=True, ignore=ignore_func) except Exception as err: raise Exception("Copy files failed. Error: " + str(err)) from err @@ -49,6 +60,28 @@ def build(options): def copy_output(options): + run_cmd(['rm', '-rf', os.path.join(options.source_path, 'mirror/build')]) + # only keep .d.ts files + def copy_to_source_ignore_func(dirname, filenames): + return [f for f in filenames + if os.path.isfile(os.path.join(dirname, f)) and not f.endswith('.d.ts')] + + copy_files(os.path.join(options.mirror_ui2abc_dir, 'libarkts/build'), + os.path.join(options.source_path, 'mirror/build'), + ignore_func=copy_to_source_ignore_func) + + copy_files(os.path.join(options.mirror_ui2abc_dir, '../incremental/common/build'), + os.path.join(options.source_path, 'mirror/build/koalaui/common/dist'), + ignore_func=copy_to_source_ignore_func) + + copy_files(os.path.join(options.mirror_ui2abc_dir, '../incremental/compat/build'), + os.path.join(options.source_path, 'mirror/build/koalaui/compat/dist'), + ignore_func=copy_to_source_ignore_func) + + copy_files(os.path.join(options.mirror_ui2abc_dir, '../interop/build/'), + os.path.join(options.source_path, 'mirror/build/koalaui/interop/dist'), + ignore_func=copy_to_source_ignore_func) + run_cmd(['rm', '-rf', options.output_path]) copy_files(os.path.join(options.source_path, 'build/lib'), os.path.join(options.output_path, 'build/lib')) @@ -59,17 +92,46 @@ def copy_output(options): copy_files(os.path.join(options.source_path, 'package.json'), os.path.join(options.output_path, 'package.json'), True) + # only keep JS files + def copy_to_out_ignore_func(dirname, filenames): + return [f for f in filenames + if os.path.isfile(os.path.join(dirname, f)) and not f.endswith('.js')] + + copy_files(os.path.join(options.mirror_ui2abc_dir, 'libarkts/build'), + os.path.join(options.output_path, 'mirror/build'), + ignore_func=copy_to_out_ignore_func) + + copy_files(os.path.join(options.mirror_ui2abc_dir, '../incremental/common/build'), + os.path.join(options.output_path, 'mirror/build/koalaui/common/dist'), + ignore_func=copy_to_out_ignore_func) + + copy_files(os.path.join(options.mirror_ui2abc_dir, '../incremental/compat/build'), + os.path.join(options.output_path, 'mirror/build/koalaui/compat/dist'), + ignore_func=copy_to_out_ignore_func) + + copy_files(os.path.join(options.mirror_ui2abc_dir, '../interop/build/'), + os.path.join(options.output_path, 'mirror/build/koalaui/interop/dist'), + ignore_func=copy_to_out_ignore_func) + + copy_files(os.path.join(options.source_path, 'mirror/package.json'), + os.path.join(options.output_path, 'mirror/package.json'), True) + + # replace package imports with alias imports + process_mirror_output(os.path.join(options.output_path, 'mirror/build')) + if options.current_os == "mingw" : copy_files(os.path.join(options.root_out_dir, 'libes2panda.dll'), os.path.join(options.output_path, 'build/native/es2panda.node'), True) - copy_files(os.path.join(options.root_out_dir, 'libes2panda.dll'), - os.path.join(options.source_path, 'build/native/es2panda.node'), True) + + copy_files(os.path.join(options.root_out_dir, 'libes2panda_lib.dll'), + os.path.join(options.output_path, 'mirror/build/native/build/es2panda.node'), True) if options.current_os == "linux" or options.current_os == "mac" : copy_files(os.path.join(options.root_out_dir, 'libes2panda.node'), os.path.join(options.output_path, 'build/native/es2panda.node'), True) - copy_files(os.path.join(options.root_out_dir, 'libes2panda.node'), - os.path.join(options.source_path, 'build/native/es2panda.node'), True) + + copy_files(os.path.join(options.root_out_dir, 'libes2panda_lib.node'), + os.path.join(options.output_path, 'mirror/build/native/build/es2panda.node'), True) def parse_args(): @@ -79,6 +141,7 @@ def parse_args(): parser.add_argument('--output_path', help='path to output') parser.add_argument('--root_out_dir', help='path to root out') parser.add_argument('--current_os', help='current_os') + parser.add_argument('--mirror_ui2abc_dir', help='mirror_ui2abc_dir') options = parser.parse_args() return options diff --git a/koala-wrapper/mirror/package.json b/koala-wrapper/mirror/package.json new file mode 100644 index 0000000000000000000000000000000000000000..2942a72a8b970507eaacd38a45be1770abfae5fb --- /dev/null +++ b/koala-wrapper/mirror/package.json @@ -0,0 +1,56 @@ +{ + "name": "@koalaui/libarkts-mirror", + "version": "1.0.0", + "private": true, + "main": "./build/index.js", + "types": "./build/arkts-api/index.d.ts", + "exports": { + ".": "./build/arkts-api/index.js", + "./build/lib/es2panda": "./build/index.js" + }, + "files": [ + "./build/*" + ], + "config": { + "gen_version": "3.0.19" + }, + "devDependencies": { + "@babel/cli": "7.20.7", + "@babel/core": "7.20.12", + "@babel/plugin-proposal-class-properties": "7.18.6", + "@babel/preset-env": "7.20.2", + "@babel/preset-typescript": "7.18.6", + "@babel/runtime": "7.20.13", + "@tsconfig/recommended": "1.0.8", + "node-addon-api": "^8.3.0", + "typescript": "^5.0.0", + "@types/node": "^18.0.0" + }, + "imports": { + "#koalaui/interop": { + "default": "./build/koalaui/interop/dist/lib/src/interop/index.js" + }, + "#koalaui/common": { + "default": "./build/koalaui/common/dist/lib/src/index.js" + }, + "#koalaui/compat": { + "default": "./build/koalaui/compat/dist/src/index.js" + }, + "#common/wrappers": { + "browser": "./build/koalaui/interop/dist/lib/src/wasm/wrappers/index.js", + "node": "./build/koalaui/interop/dist/lib/src/napi/wrappers/index.js" + }, + "#common/wrappers/*": { + "browser": "./build/koalaui/interop/dist/lib/src/wasm/wrappers/*.js", + "node": "./build/koalaui/interop/dist/lib/src/napi/wrappers/*.js", + "default": "./build/koalaui/interop/dist/lib/src/napi/wrappers/*.js" + }, + "#platform": { + "ark": "./build/koalaui/compat/dist/src/ohos/index.js", + "ios": "./build/koalaui/compat/dist/src/typescript/index.js", + "browser": "./build/koalaui/compat/dist/src/typescript/index.js", + "node": "./build/koalaui/compat/dist/src/typescript/index.js", + "default": "./build/koalaui/compat/dist/src/typescript/index.js" + } + } +}