webpack 微调

This commit is contained in:
Wang Kejun 2022-07-02 18:11:53 +08:00
parent e13d1e2578
commit 06f7c557f4
6 changed files with 54 additions and 26 deletions

View File

@ -48,9 +48,12 @@ module.exports = {
appWebpackCache: resolveRoot('node_modules/.cache'), appWebpackCache: resolveRoot('node_modules/.cache'),
appTsBuildInfoFile: resolveRoot('node_modules/.cache/tsconfig.tsbuildinfo'), appTsBuildInfoFile: resolveRoot('node_modules/.cache/tsconfig.tsbuildinfo'),
publicUrlOrPath: '/', publicUrlOrPath: '/',
appOutSrc: resolveRoot('src'), appRootSrc: resolveRoot('src'),
appOutPath: resolveRoot('.'), appRootPath: resolveRoot('.'),
oakConfigJson: resolveRoot('oak.config.json'), oakConfigJson: resolveRoot('oak.config.json'),
oakGeneralBusinessAppPath: resolveRoot(
'node_modules/oak-general-business/app'
),
}; };

View File

@ -20,6 +20,7 @@ const pkg = require(paths.appPackageJson);
// Check if TypeScript is setup // Check if TypeScript is setup
const useTypeScript = fs.existsSync(paths.appTsConfig); const useTypeScript = fs.existsSync(paths.appTsConfig);
const createEnvironmentHash = require('./webpack/persistentCache/createEnvironmentHash');
const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false'; const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
const copyPatterns = [].concat(pkg.copyWebpack || []).map((pattern) => const copyPatterns = [].concat(pkg.copyWebpack || []).map((pattern) =>
@ -116,8 +117,10 @@ module.exports = function (webpackEnv) {
}, },
resolve: { resolve: {
alias: { alias: {
'@': paths.appSrc,
assert: require.resolve('assert'), assert: require.resolve('assert'),
'@': paths.appSrc,
'@project': paths.appRootSrc,
'@oak-general-business': paths.oakGeneralBusinessAppPath,
}, },
extensions: paths.moduleFileExtensions.map((ext) => `.${ext}`), extensions: paths.moduleFileExtensions.map((ext) => `.${ext}`),
symlinks: true, symlinks: true,
@ -138,6 +141,22 @@ module.exports = function (webpackEnv) {
// 第二种方式选查找自己的loaders文件中有没有这个loader再查找node_modules文件 // 第二种方式选查找自己的loaders文件中有没有这个loader再查找node_modules文件
// modules: [path.resolve(__dirname, 'loaders'), '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: { optimization: {
// 标记未被使用的代码 // 标记未被使用的代码
usedExports: true, usedExports: true,
@ -166,7 +185,7 @@ module.exports = function (webpackEnv) {
}, },
{ {
test: /\.wxs$/, test: /\.wxs$/,
include: paths.appOutSrc, include: paths.appRootSrc,
type: 'javascript/auto', type: 'javascript/auto',
use: [oakFileLoader('wxs')], use: [oakFileLoader('wxs')],
}, },
@ -185,7 +204,7 @@ module.exports = function (webpackEnv) {
}, },
{ {
test: /\.(png|jpg|gif|svg)$/, test: /\.(png|jpg|gif|svg)$/,
include: paths.appOutSrc, include: paths.appRootSrc,
type: 'javascript/auto', type: 'javascript/auto',
use: localFileLoader(), use: localFileLoader(),
}, },
@ -223,7 +242,7 @@ module.exports = function (webpackEnv) {
}, },
{ {
test: /\.less$/, test: /\.less$/,
include: paths.appOutSrc, include: paths.appRootSrc,
type: 'javascript/auto', type: 'javascript/auto',
use: [ use: [
localFileLoader('wxss'), localFileLoader('wxss'),
@ -243,7 +262,7 @@ module.exports = function (webpackEnv) {
}, },
{ {
test: /\.js$/, test: /\.js$/,
include: [paths.appSrc, paths.appOutSrc].concat( include: [paths.appSrc, paths.appRootSrc].concat(
getOakInclude() getOakInclude()
), ),
exclude: /node_modules/, exclude: /node_modules/,
@ -251,7 +270,7 @@ module.exports = function (webpackEnv) {
}, },
{ {
test: /\.((?!tsx)ts)$/, test: /\.((?!tsx)ts)$/,
include: [paths.appSrc, paths.appOutSrc].concat( include: [paths.appSrc, paths.appRootSrc].concat(
getOakInclude() getOakInclude()
), ),
exclude: /node_modules/, exclude: /node_modules/,
@ -305,7 +324,7 @@ module.exports = function (webpackEnv) {
}, },
{ {
test: /\.(xml|wxml)$/, test: /\.(xml|wxml)$/,
include: paths.appOutSrc, include: paths.appRootSrc,
type: 'javascript/auto', type: 'javascript/auto',
use: [ use: [
localFileLoader('wxml'), localFileLoader('wxml'),
@ -365,7 +384,7 @@ module.exports = function (webpackEnv) {
// }, // },
// }, // },
configFile: paths.appTsConfig, configFile: paths.appTsConfig,
context: paths.appOutPath, context: paths.appRootPath,
diagnosticOptions: { diagnosticOptions: {
// semantic: true, // semantic: true,
syntactic: true, syntactic: true,
@ -378,8 +397,8 @@ module.exports = function (webpackEnv) {
// as micromatch doesn't match // as micromatch doesn't match
// otherwise. // otherwise.
include: [ include: [
{ file: '../**/app/**/*.{ts,tsx}' }, { file: '../**/app/**/*.ts' },
{ file: '**/app/**/*.{ts,tsx}' }, { file: '**/app/**/*.ts' },
{ file: '../**/src/**/*.ts' }, { file: '../**/src/**/*.ts' },
{ file: '**/src/**/*.ts' }, { file: '**/src/**/*.ts' },
], ],

View File

@ -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');
};

View File

@ -72,9 +72,12 @@ module.exports = {
appTsBuildInfoFile: resolveRoot('node_modules/.cache/tsconfig.tsbuildinfo'), appTsBuildInfoFile: resolveRoot('node_modules/.cache/tsconfig.tsbuildinfo'),
swSrc: resolveModule(resolveApp, 'src/service-worker'), swSrc: resolveModule(resolveApp, 'src/service-worker'),
publicUrlOrPath, publicUrlOrPath,
appOutSrc: resolveRoot('src'), appRootSrc: resolveRoot('src'),
appOutPath: resolveRoot('.'), appRootPath: resolveRoot('.'),
oakConfigJson: resolveRoot('oak.config.json'), oakConfigJson: resolveRoot('oak.config.json'),
oakGeneralBusinessAppPath: resolveRoot(
'node_modules/oak-general-business/app'
),
}; };

View File

@ -352,6 +352,9 @@ module.exports = function (webpackEnv) {
'scheduler/tracing': 'scheduler/tracing-profiling', 'scheduler/tracing': 'scheduler/tracing-profiling',
}), }),
...(modules.webpackAliases || {}), ...(modules.webpackAliases || {}),
'@': paths.appSrc,
'@project': paths.appRootSrc,
'@oak-general-business': paths.oakGeneralBusinessAppPath,
}, },
plugins: [ plugins: [
// Prevents users from importing files from outside of src/ (or node_modules/). // 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. // The preset includes JSX, Flow, TypeScript, and some ESnext features.
{ {
test: /\.(js|mjs|jsx|ts|tsx)$/, test: /\.(js|mjs|jsx|ts|tsx)$/,
include: [paths.appOutSrc, paths.appSrc].concat( include: [paths.appRootSrc, paths.appSrc].concat(
getOakInclude() getOakInclude()
), ),
use: [ use: [
@ -801,7 +804,7 @@ module.exports = function (webpackEnv) {
// }, // },
// }, // },
configFile: paths.appTsConfig, configFile: paths.appTsConfig,
context: paths.appOutPath, context: paths.appRootPath,
diagnosticOptions: { diagnosticOptions: {
syntactic: true, syntactic: true,
}, },

View File

@ -113,11 +113,7 @@ class OakWeChatMpPlugin {
compiler.hooks.emit.tapPromise( compiler.hooks.emit.tapPromise(
pluginName, pluginName,
catchError(async (compilation) => { catchError(async (compilation) => {
const { clear } = this.options; // 输出outpath前
if (clear && !this.firstClean) {
this.firstClean = true;
await OakWeChatMpPlugin.clearOutPut(compilation);
}
}) })
); );
} }
@ -869,11 +865,6 @@ class OakWeChatMpPlugin {
...exclude, ...exclude,
]; ];
} }
static async clearOutPut(compilation) {
const { path } = compilation.options.output;
await fsExtra.remove(path);
}
} }
module.exports = OakWeChatMpPlugin; module.exports = OakWeChatMpPlugin;