diff --git a/config/mp/paths.js b/config/mp/paths.js index 2348670..c4b1d0d 100644 --- a/config/mp/paths.js +++ b/config/mp/paths.js @@ -48,9 +48,12 @@ module.exports = { appWebpackCache: resolveRoot('node_modules/.cache'), appTsBuildInfoFile: resolveRoot('node_modules/.cache/tsconfig.tsbuildinfo'), publicUrlOrPath: '/', - appOutSrc: resolveRoot('src'), - appOutPath: resolveRoot('.'), + appRootSrc: resolveRoot('src'), + appRootPath: resolveRoot('.'), oakConfigJson: resolveRoot('oak.config.json'), + oakGeneralBusinessAppPath: resolveRoot( + 'node_modules/oak-general-business/app' + ), }; diff --git a/config/mp/webpack.config.js b/config/mp/webpack.config.js index c870edb..044bc28 100644 --- a/config/mp/webpack.config.js +++ b/config/mp/webpack.config.js @@ -20,6 +20,7 @@ const pkg = require(paths.appPackageJson); // Check if TypeScript is setup const useTypeScript = fs.existsSync(paths.appTsConfig); +const createEnvironmentHash = require('./webpack/persistentCache/createEnvironmentHash'); const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false'; const copyPatterns = [].concat(pkg.copyWebpack || []).map((pattern) => @@ -116,8 +117,10 @@ module.exports = function (webpackEnv) { }, resolve: { alias: { - '@': paths.appSrc, assert: require.resolve('assert'), + '@': paths.appSrc, + '@project': paths.appRootSrc, + '@oak-general-business': paths.oakGeneralBusinessAppPath, }, extensions: paths.moduleFileExtensions.map((ext) => `.${ext}`), symlinks: true, @@ -138,6 +141,22 @@ module.exports = function (webpackEnv) { // 第二种方式选查找自己的loaders文件中有没有这个loader再查找node_modules文件 // modules: [path.resolve(__dirname, 'loaders'), 'node_modules'], }, + cache: { + type: 'filesystem', + version: createEnvironmentHash(env.raw), + cacheDirectory: paths.appWebpackCache, + store: 'pack', + buildDependencies: { + defaultWebpack: ['webpack/lib/'], + config: [__filename], + tsconfig: [paths.appTsConfig, paths.appJsConfig].filter((f) => + fs.existsSync(f) + ), + }, + }, + infrastructureLogging: { + level: 'none', + }, optimization: { // 标记未被使用的代码 usedExports: true, @@ -166,7 +185,7 @@ module.exports = function (webpackEnv) { }, { test: /\.wxs$/, - include: paths.appOutSrc, + include: paths.appRootSrc, type: 'javascript/auto', use: [oakFileLoader('wxs')], }, @@ -185,7 +204,7 @@ module.exports = function (webpackEnv) { }, { test: /\.(png|jpg|gif|svg)$/, - include: paths.appOutSrc, + include: paths.appRootSrc, type: 'javascript/auto', use: localFileLoader(), }, @@ -223,7 +242,7 @@ module.exports = function (webpackEnv) { }, { test: /\.less$/, - include: paths.appOutSrc, + include: paths.appRootSrc, type: 'javascript/auto', use: [ localFileLoader('wxss'), @@ -243,7 +262,7 @@ module.exports = function (webpackEnv) { }, { test: /\.js$/, - include: [paths.appSrc, paths.appOutSrc].concat( + include: [paths.appSrc, paths.appRootSrc].concat( getOakInclude() ), exclude: /node_modules/, @@ -251,7 +270,7 @@ module.exports = function (webpackEnv) { }, { test: /\.((?!tsx)ts)$/, - include: [paths.appSrc, paths.appOutSrc].concat( + include: [paths.appSrc, paths.appRootSrc].concat( getOakInclude() ), exclude: /node_modules/, @@ -305,7 +324,7 @@ module.exports = function (webpackEnv) { }, { test: /\.(xml|wxml)$/, - include: paths.appOutSrc, + include: paths.appRootSrc, type: 'javascript/auto', use: [ localFileLoader('wxml'), @@ -365,7 +384,7 @@ module.exports = function (webpackEnv) { // }, // }, configFile: paths.appTsConfig, - context: paths.appOutPath, + context: paths.appRootPath, diagnosticOptions: { // semantic: true, syntactic: true, @@ -378,8 +397,8 @@ module.exports = function (webpackEnv) { // as micromatch doesn't match // otherwise. include: [ - { file: '../**/app/**/*.{ts,tsx}' }, - { file: '**/app/**/*.{ts,tsx}' }, + { file: '../**/app/**/*.ts' }, + { file: '**/app/**/*.ts' }, { file: '../**/src/**/*.ts' }, { file: '**/src/**/*.ts' }, ], diff --git a/config/mp/webpack/persistentCache/createEnvironmentHash.js b/config/mp/webpack/persistentCache/createEnvironmentHash.js new file mode 100644 index 0000000..4487e85 --- /dev/null +++ b/config/mp/webpack/persistentCache/createEnvironmentHash.js @@ -0,0 +1,9 @@ +'use strict'; +const { createHash } = require('crypto'); + +module.exports = env => { + const hash = createHash('md5'); + hash.update(JSON.stringify(env)); + + return hash.digest('hex'); +}; diff --git a/config/web/paths.js b/config/web/paths.js index 9ce3e23..bb4ae1f 100644 --- a/config/web/paths.js +++ b/config/web/paths.js @@ -72,9 +72,12 @@ module.exports = { appTsBuildInfoFile: resolveRoot('node_modules/.cache/tsconfig.tsbuildinfo'), swSrc: resolveModule(resolveApp, 'src/service-worker'), publicUrlOrPath, - appOutSrc: resolveRoot('src'), - appOutPath: resolveRoot('.'), + appRootSrc: resolveRoot('src'), + appRootPath: resolveRoot('.'), oakConfigJson: resolveRoot('oak.config.json'), + oakGeneralBusinessAppPath: resolveRoot( + 'node_modules/oak-general-business/app' + ), }; diff --git a/config/web/webpack.config.js b/config/web/webpack.config.js index c4d0564..9d0ebf4 100644 --- a/config/web/webpack.config.js +++ b/config/web/webpack.config.js @@ -352,6 +352,9 @@ module.exports = function (webpackEnv) { 'scheduler/tracing': 'scheduler/tracing-profiling', }), ...(modules.webpackAliases || {}), + '@': paths.appSrc, + '@project': paths.appRootSrc, + '@oak-general-business': paths.oakGeneralBusinessAppPath, }, plugins: [ // Prevents users from importing files from outside of src/ (or node_modules/). @@ -453,7 +456,7 @@ module.exports = function (webpackEnv) { // The preset includes JSX, Flow, TypeScript, and some ESnext features. { test: /\.(js|mjs|jsx|ts|tsx)$/, - include: [paths.appOutSrc, paths.appSrc].concat( + include: [paths.appRootSrc, paths.appSrc].concat( getOakInclude() ), use: [ @@ -801,7 +804,7 @@ module.exports = function (webpackEnv) { // }, // }, configFile: paths.appTsConfig, - context: paths.appOutPath, + context: paths.appRootPath, diagnosticOptions: { syntactic: true, }, diff --git a/plugins/WechatMpPlugin.js b/plugins/WechatMpPlugin.js index 1937ec9..75ba3bd 100644 --- a/plugins/WechatMpPlugin.js +++ b/plugins/WechatMpPlugin.js @@ -113,11 +113,7 @@ class OakWeChatMpPlugin { compiler.hooks.emit.tapPromise( pluginName, catchError(async (compilation) => { - const { clear } = this.options; - if (clear && !this.firstClean) { - this.firstClean = true; - await OakWeChatMpPlugin.clearOutPut(compilation); - } + // 输出outpath前 }) ); } @@ -869,11 +865,6 @@ class OakWeChatMpPlugin { ...exclude, ]; } - - static async clearOutPut(compilation) { - const { path } = compilation.options.output; - await fsExtra.remove(path); - } } module.exports = OakWeChatMpPlugin;