diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index a7714a5..3fff1c0 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -137,6 +137,10 @@ export class GenAbcPlugin { return; } + if (projectConfig.compileMode === ESMODULE) { + removeDir(projectConfig.nodeModulesPath); + } + compiler.hooks.compilation.tap('GenAbcPlugin', (compilation) => { if (projectConfig.compileMode === JSBUNDLE || projectConfig.compileMode === undefined) { return; @@ -162,7 +166,6 @@ export class GenAbcPlugin { if (projectConfig.compileMode === ESMODULE) { return; } - removeDir(output); Object.keys(compilation.assets).forEach(key => { // choose *.js if (output && path.extname(key) === EXTNAME_JS) { @@ -365,6 +368,8 @@ function writeFileSync(inputString: string, output: string, jsBundleFile: string fs.writeFileSync(cacheOutputPath, inputString); if (fs.existsSync(cacheOutputPath)) { const fileSize: any = fs.statSync(cacheOutputPath).size; + output = toUnixPath(output); + cacheOutputPath = toUnixPath(cacheOutputPath); intermediateJsBundle.push({path: output, size: fileSize, cacheOutputPath: cacheOutputPath}); } else { logger.error(red, `ETS:ERROR Failed to convert file ${jsBundleFile} to bin. ${output} is lost`, reset); @@ -418,8 +423,6 @@ function splitJsBundlesBySize(bundleArray: Array, groupNumber: number): an } function invokeWorkersModuleToGenAbc(moduleInfos: Array): void { - removeDir(buildPathInfo); - removeDir(projectConfig.nodeModulesPath); filterIntermediateModuleByHashJson(buildPathInfo, moduleInfos); filterModuleInfos.forEach(moduleInfo => { if (moduleInfo.isCommonJs) { diff --git a/compiler/webpack.config.js b/compiler/webpack.config.js index 03f67a2..eb5e1a6 100644 --- a/compiler/webpack.config.js +++ b/compiler/webpack.config.js @@ -17,6 +17,7 @@ const path = require('path'); const fs = require('fs'); const CopyPlugin = require('copy-webpack-plugin'); const Webpack = require('webpack'); +const { CleanWebpackPlugin } = require('clean-webpack-plugin'); const { GenAbcPlugin } = require('./lib/gen_abc_plugin'); const { OHMResolverPlugin } = require('./lib/resolve_ohm_url'); const buildPipeServer = require('./server/build_pipe_server'); @@ -338,6 +339,25 @@ function setGenAbcPlugin(env, config) { } } +function setCleanWebpackPlugin(workerFile, config) { + let cleanPath = []; + cleanPath.push(projectConfig.buildPath); + if (workerFile) { + let workerFilesPath = Object.keys(workerFile); + for (let workerFilePath of workerFilesPath) { + cleanPath.push(path.join(projectConfig.buildPath, workerFilePath, '..')); + } + } + + config.plugins.push( + new CleanWebpackPlugin({ + dry: false, + dangerouslyAllowCleanPatternsOutsideProject: true, + cleanOnceBeforeBuildPatterns: cleanPath + }) + ); +} + module.exports = (env, argv) => { const config = {}; setProjectConfig(env); @@ -348,6 +368,7 @@ module.exports = (env, argv) => { const workerFile = readWorkerFile(); setOptimizationConfig(config, workerFile); setCopyPluginConfig(config); + setCleanWebpackPlugin(workerFile, config); if (env.isPreview !== "true") { loadWorker(projectConfig, workerFile);