diff --git a/ace-loader/main.product.js b/ace-loader/main.product.js index 85df6b6..ee38c41 100644 --- a/ace-loader/main.product.js +++ b/ace-loader/main.product.js @@ -153,6 +153,30 @@ function checkMultiResourceBuild(aceBuildJson) { } } +function readWorkerFile() { + const workerEntryObj = {}; + if (process.env.aceBuildJson && fs.existsSync(process.env.aceBuildJson)) { + const workerConfig = JSON.parse(fs.readFileSync(process.env.aceBuildJson).toString()); + if(workerConfig.workers) { + workerConfig.workers.forEach(worker => { + if (!/\.(js)$/.test(worker)) { + worker += '.js'; + } + const relativePath = path.relative(process.env.projectPath, worker); + if (filterWorker(relativePath)) { + workerEntryObj[relativePath.replace(/\.(ts|js)$/,'').replace(/\\/g, '/')] = worker; + } + }) + return workerEntryObj; + } + } + return null; +} + +function filterWorker(workerPath) { + return /\.(ts|js)$/.test(workerPath); +} + module.exports = { deleteFolderRecursive: deleteFolderRecursive, readManifest: readManifest, @@ -160,5 +184,6 @@ module.exports = { compileCardModule: compileCardModule, hashProjectPath: hashProjectPath, checkMultiResourceBuild: checkMultiResourceBuild, - multiResourceBuild: multiResourceBuild + multiResourceBuild: multiResourceBuild, + readWorkerFile: readWorkerFile, }; \ No newline at end of file diff --git a/ace-loader/src/resource-plugin.js b/ace-loader/src/resource-plugin.js index 30a6c60..05131b7 100644 --- a/ace-loader/src/resource-plugin.js +++ b/ace-loader/src/resource-plugin.js @@ -33,6 +33,7 @@ let shareThemePath = ''; let internalThemePath = ''; let resourcesPath; let sharePath; +let workerFile; function copyFile(input, output) { try { @@ -112,7 +113,7 @@ function toCopyFile(file, outputFile, fileStat) { } class ResourcePlugin { - constructor(input_, output_, manifestFilePath_, watchCSSFiles_) { + constructor(input_, output_, manifestFilePath_, watchCSSFiles_, workerFile_ = null) { input = input_; output = output_; manifestFilePath = manifestFilePath_; @@ -121,6 +122,7 @@ class ResourcePlugin { internalThemePath = path.join(input_, 'resources/styles'); resourcesPath = path.resolve(input_, 'resources'); sharePath = path.resolve(input_, '../share/resources'); + workerFile = workerFile_; } apply(compiler) { compiler.hooks.beforeCompile.tap('resource Copy', () => { @@ -331,17 +333,8 @@ function buildManifest(manifest) { } function loadWorker(entryObj) { - if(validateWorkOption()) { - const workerConfig = JSON.parse(fs.readFileSync(process.env.aceBuildJson).toString()); - workerConfig.workers.forEach(worker => { - if (!/\.(js)$/.test(worker)) { - worker += '.js'; - } - const relativePath = path.relative(process.env.projectPath, worker); - if (filterWorker(relativePath)) { - entryObj[relativePath.replace(/\.(ts|js)$/,'').replace(/\\/g, '/')] = worker; - } - }) + if (workerFile) { + entryObj = Object.assign(entryObj, workerFile); } else { const workerPath = path.resolve(input, 'workers'); if (fs.existsSync(workerPath)) { @@ -349,7 +342,8 @@ function loadWorker(entryObj) { readFile(workerPath, workerFiles); workerFiles.forEach((item) => { if (/\.js$/.test(item)) { - const relativePath = path.relative(workerPath, item).replace(/\.js$/, ''); + const relativePath = path.relative(workerPath, item) + .replace(/\.js$/, '').replace(/\\/g, '/'); entryObj[`./workers/` + relativePath] = item; } }); diff --git a/ace-loader/webpack.rich.config.js b/ace-loader/webpack.rich.config.js index b833ea0..b899035 100644 --- a/ace-loader/webpack.rich.config.js +++ b/ace-loader/webpack.rich.config.js @@ -35,7 +35,8 @@ const { loadEntryObj, compileCardModule, hashProjectPath, - checkMultiResourceBuild + checkMultiResourceBuild, + readWorkerFile } = require('./main.product') const richModule = { @@ -242,8 +243,16 @@ function existsPackageJson(config, rootPackageJsonPath, modulePackageJsonPath) { } } +function excludeWorker(workerFile, name) { + if (workerFile) { + return Object.keys(workerFile).includes(name); + } + return /^\.\/workers\//.test(name); +} + module.exports = (env) => { setConfigs(env); + const workerFile = readWorkerFile(); if (process.env.compileMode === 'moduleJson') { compileCardModule(env); config.entry = {}; @@ -258,7 +267,7 @@ module.exports = (env) => { config.output.path = path.resolve(__dirname, process.env.buildPath) config.plugins = [ new ResourcePlugin(process.env.projectPath, process.env.buildPath, - process.env.aceManifestPath, process.env.watchCSSFiles), + process.env.aceManifestPath, process.env.watchCSSFiles, workerFile), new ResultStates({ build: process.env.buildPath }), @@ -304,11 +313,11 @@ module.exports = (env) => { notPreview(env) } } else { - if (process.env.abilityType === 'page') { + if (process.env.compileMode !== 'moduleJson' && process.env.abilityType === 'page') { config.optimization = { splitChunks: { chunks(chunk) { - return !/^\.\/workers\//.test(chunk.name) && !/^\.\/TestAbility/.test(chunk.name); + return !excludeWorker(workerFile, chunk.name) && !/^\.\/TestAbility/.test(chunk.name); }, minSize: 0, cacheGroups: {