diff --git a/BUILD.gn b/BUILD.gn index 4fc4d1fc49743d57cf124ee46ee9c7085583c3bc..86206bd85c50d1c3034fb7193fba31fae770ea98 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -126,9 +126,8 @@ ohos_copy("previewer_copy") { module_install_name = "" } -ace_loader_dir = +ace_loader_ark_dir = get_label_info(":ace_loader", "target_out_dir") + "/ace_loader_ark" - ohos_copy("ace_loader_ark") { sources = ace_loader_sources outputs = [ target_out_dir + "/$target_name/{{source_file_part}}" ] @@ -142,13 +141,13 @@ ohos_copy("ace_loader_ark_hap") { ":build_ace_loader_library", ] sources = [ ace_loader_lib_dir ] - outputs = [ ace_loader_dir + "/lib" ] + outputs = [ ace_loader_ark_dir + "/lib" ] license_file = "//third_party/parse5/LICENSE" } ohos_copy("ace_loader_node_modules") { deps = [ ":ace_loader_ark_hap" ] sources = [ "//developtools/ace-js2bundle/ace-loader/node_modules" ] - outputs = [ ace_loader_dir + "/node_modules" ] + outputs = [ ace_loader_ark_dir + "/node_modules" ] license_file = "//third_party/parse5/LICENSE" } diff --git a/ace-loader/src/genAbc-plugin.js b/ace-loader/src/genAbc-plugin.js index e33a8c520f5d99aae746b857b455f4356fbd85be..74fdfbc8691417508446880f9da22a5fe490f8f3 100644 --- a/ace-loader/src/genAbc-plugin.js +++ b/ace-loader/src/genAbc-plugin.js @@ -13,9 +13,9 @@ * limitations under the License. */ -const fs = require('fs') -const path = require('path') -const process = require('child_process') +const fs = require('fs'); +const path = require('path'); +const process = require('child_process'); const forward = '(global.___mainEntry___ = function (globalObjects) {' + '\n' + ' var define = globalObjects.define;' + '\n' + @@ -30,50 +30,47 @@ const forward = '(global.___mainEntry___ = function (globalObjects) {' + '\n' + ' var Image = globalObjects.Image;' + '\n' + ' var OffscreenCanvas = globalObjects.OffscreenCanvas;' + '\n' + ' (function(global) {' + '\n' + - ' "use strict";' + '\n' -const last = '\n' + '})(this.__appProto__);' + '\n' + '})' -const firstFileEXT = '_.js' -let output -let isWin = false -let isMac = false -let isDebug = false -let arkDir + ' "use strict";' + '\n'; +const last = '\n' + '})(this.__appProto__);' + '\n' + '})'; +const firstFileEXT = '_.js'; +let output; +let isWin = false; +let isMac = false; +let isDebug = false; +let arkDir; +let nodeJs; class GenAbcPlugin { - constructor(output_, arkDir_, isDebug_) { - output = output_ - arkDir = arkDir_ - isDebug = isDebug_ + constructor(output_, arkDir_, nodeJs_, isDebug_) { + output = output_; + arkDir = arkDir_; + nodeJs = nodeJs_; + isDebug = isDebug_; } apply(compiler) { if (fs.existsSync(path.resolve(arkDir, 'build-win'))) { - isWin = true - } else { - if (fs.existsSync(path.resolve(arkDir, 'build-mac'))) { - isMac = true - } else { - if (!fs.existsSync(path.resolve(arkDir, 'build'))) { - console.error('\u001b[31m', `find build fail`, '\u001b[39m') - return - } - } + isWin = true; + } else if (fs.existsSync(path.resolve(arkDir, 'build-mac'))) { + isMac = true; + } else if (!fs.existsSync(path.resolve(arkDir, 'build'))) { + return; } compiler.hooks.emit.tap('GenAbcPlugin', (compilation) => { - const assets = compilation.assets - const keys = Object.keys(assets) + const assets = compilation.assets; + const keys = Object.keys(assets); keys.forEach(key => { // choice *.js if (output && path.extname(key) === '.js') { - let newContent = assets[key].source() + let newContent = assets[key].source(); if (key.search('./workers/') !== 0 && key !== 'commons.js' && key !== 'vendors.js') { - newContent = forward + newContent + last + newContent = forward + newContent + last; } if (key === 'commons.js' || key === 'vendors.js') { - newContent = `\n\n\n\n\n\n\n\n\n\n\n\n\n\n` + newContent + newContent = `\n\n\n\n\n\n\n\n\n\n\n\n\n\n` + newContent; } const keyPath = key.replace(/\.js$/, firstFileEXT) - writeFileSync(newContent, path.resolve(output, keyPath), key) + writeFileSync(newContent, path.resolve(output, keyPath), key); } }) }) @@ -81,58 +78,49 @@ class GenAbcPlugin { } function writeFileSync(inputString, output, jsBundleFile) { - const parent = path.join(output, '..') - if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { - mkDir(parent) - } - fs.writeFileSync(output, inputString) - if (fs.existsSync(output)) { - js2abcFirst(output) - } else { - console.error('\u001b[31m', `Failed to convert file ${jsBundleFile} to bin. ${output} is lost`, '\u001b[39m') - } + const parent = path.join(output, '..'); + if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { + mkDir(parent); + } + fs.writeFileSync(output, inputString); + if (fs.existsSync(output)) { + js2abcFirst(output); + } } function mkDir(path_) { - const parent = path.join(path_, '..') - if (!(fs.existsSync(parent) && !fs.statSync(parent).isFile())) { - mkDir(parent) - } - fs.mkdirSync(path_) + const parent = path.join(path_, '..'); + if (!(fs.existsSync(parent) && !fs.statSync(parent).isFile())) { + mkDir(parent); + } + fs.mkdirSync(path_); } function js2abcFirst(inputPath) { - let param = '-r' - if (isDebug) { - param += ' --debug' - } + let param = '-r'; + if (isDebug) { + param += ' --debug'; + } - let js2abc = path.join(arkDir, 'build', 'src', 'index.js'); - if (isWin) { - js2abc = path.join(arkDir, 'build-win', 'src', 'index.js'); - } else if (isMac){ - js2abc = path.join(arkDir, 'build-mac', 'src', 'index.js'); - } + let js2abc = path.join(arkDir, 'build', 'src', 'index.js'); + if (isWin) { + js2abc = path.join(arkDir, 'build-win', 'src', 'index.js'); + } else if (isMac) { + js2abc = path.join(arkDir, 'build-mac', 'src', 'index.js'); + } - const cmd = `node --expose-gc "${js2abc}" "${inputPath}" ${param}`; - try { - process.execSync(cmd) - console.info(cmd) - } catch (e) { - console.error('\u001b[31m', `Failed to convert file ${inputPath} to abc`, '\u001b[39m') - } - if (fs.existsSync(inputPath)) { - fs.unlinkSync(inputPath) - } else { - console.error('\u001b[31m', `Failed to convert file ${inputPath} to abc. ${inputPath} is lost`, '\u001b[39m') - } - let abcFile = inputPath.replace(/\.js$/, '.abc'); - if (fs.existsSync(abcFile)) { - let abcFileNew = abcFile.replace(/\_.abc$/, '.abc'); - fs.renameSync(abcFile, abcFileNew) - } else { - console.error('\u001b[31m', `${abcFile} is lost`, '\u001b[39m') - } + const cmd = `${nodeJs} --expose-gc "${js2abc}" "${inputPath}" ${param}`; + process.execSync(cmd); + + if (fs.existsSync(inputPath)) { + fs.unlinkSync(inputPath); + } + + let abcFile = inputPath.replace(/\.js$/, '.abc'); + if (fs.existsSync(abcFile)) { + let abcFileNew = abcFile.replace(/\_.abc$/, '.abc'); + fs.renameSync(abcFile, abcFileNew); + } } -module.exports = GenAbcPlugin +module.exports = GenAbcPlugin; diff --git a/ace-loader/webpack.rich.config.js b/ace-loader/webpack.rich.config.js index 1d78e6d2d28df93cff4a12ab6601913be04a7bc4..4ef644cdf28407f93afeda457399bb63bc4f392a 100644 --- a/ace-loader/webpack.rich.config.js +++ b/ace-loader/webpack.rich.config.js @@ -253,10 +253,14 @@ module.exports = (env) => { config.plugins.push(new ModuleCollectionPlugin()) if (env.compilerType && env.compilerType === 'ark') { let arkDir = path.join(__dirname, 'bin', 'ark'); - if (env.arkFrontendDir) { - arkDir = env.arkFrontendDir; + if (env.arkFrontendDir) { + arkDir = env.arkFrontendDir; } - config.plugins.push(new GenAbcPlugin(process.env.buildPath, arkDir, + let nodeJs = 'node'; + if (env.nodeJs) { + nodeJs = env.nodeJs; + } + config.plugins.push(new GenAbcPlugin(process.env.buildPath, arkDir, nodeJs, env.buildMode === 'debug')) } else { if (env.deviceType) {