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

View File

@ -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' },
],

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

View File

@ -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,
},

View File

@ -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;