ko
Version:
build & lint library
124 lines (123 loc) • 4.18 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const fs_1 = require("fs");
const dynamic_resolve_webpack_plugin_1 = __importDefault(require("dynamic-resolve-webpack-plugin"));
const lodash_1 = require("lodash");
const loaders_1 = __importDefault(require("./loaders"));
const plugins_1 = __importDefault(require("./plugins"));
const utils_1 = require("../utils");
class WebpackConfig {
constructor(service) {
this.extensions = [
'.ts',
'.tsx',
'.js',
'.jsx',
'.css',
'.scss',
'.sass',
'.less',
'.json',
'.html',
];
this.opts = { ...service.config, ...service.cliOpts };
this.env =
process.env.NODE_ENV === 'production' ? 'production' : 'development';
}
merge(...opts) {
return (0, lodash_1.merge)(this.base, ...opts);
}
get cache() {
const { experiment } = this.opts;
const type = experiment?.speedUp
? 'filesystem'
: this.isProd
? 'filesystem'
: 'memory';
const cache = {
type,
};
if (type === 'filesystem') {
cache.version = this.projectVersion;
cache.maxAge = 7 * 24 * 3600000;
}
else {
cache.maxGenerations = 1;
}
return cache;
}
get projectVersion() {
const pkgPath = (0, path_1.join)(this.opts.cwd, 'package.json');
(0, utils_1.assert)((0, fs_1.existsSync)(pkgPath), 'project package.json file not found');
return require(pkgPath).version;
}
get base() {
const { cwd, publicPath, entry, outputPath, alias, hash, analyzer } = this.opts;
const webpackBaseConf = {
mode: this.env,
target: 'web',
context: cwd,
entry,
output: {
path: outputPath,
filename: `js/[name].${hash ? '[contenthash].' : ''}js`,
publicPath,
},
module: {
rules: (0, loaders_1.default)({
isProd: this.isProd,
...this.opts,
}),
},
plugins: (0, plugins_1.default)({
isProd: this.isProd,
analyzer,
...this.opts,
}),
resolve: {
plugins: [
this.opts.dynamicResolve &&
new dynamic_resolve_webpack_plugin_1.default({
dynamic: this.opts.dynamicResolve,
}),
].filter(Boolean),
extensions: this.extensions,
alias: {
...alias,
// 只 alias core-js v3 的 modules 路径,不影响 v2 的 library 路径
'core-js/modules': require
.resolve('core-js')
.replace(/\/index\.js$/, '/modules'),
},
fallback: {
fs: false,
path: false,
events: false,
os: require.resolve('os-browserify/browser'),
crypto: require.resolve('crypto-browserify'),
stream: require.resolve('stream-browserify'),
buffer: require.resolve('buffer/'),
string_decoder: require.resolve('string_decoder/'),
querystring: require.resolve('querystring-es3'),
},
},
performance: {
hints: false,
},
cache: this.cache,
stats: 'none',
infrastructureLogging: {
level: 'error',
},
};
return webpackBaseConf;
}
get isProd() {
return this.env === 'production';
}
}
exports.default = WebpackConfig;