UNPKG

3.12 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const path = require("path");
4const _ = require("lodash");
5const Config = require("webpack-chain");
6const webpack_merge_1 = require("webpack-merge");
7const loaders_1 = require("./loaders/loaders");
8const plugins_1 = require("./plugins/plugins");
9const optimization_1 = require("./optimization/optimization");
10const utils_1 = require("./utils/utils");
11const cacheConfig_1 = require("./config/cacheConfig");
12/**
13 * webpack 配置
14 * @param { SweetConfig | null | undefined } sweetConfig: 获取到的外部配置
15 * @param { SweetOptions } sweetOptions: 内部挂载的一些配置
16 */
17function default_1(sweetConfig, sweetOptions) {
18 const config = new Config();
19 const sweetConfigCopy = _.isPlainObject(sweetConfig) ? _.omit({ ...sweetConfig }, [
20 'serverRender',
21 'serverEntry',
22 'serverOutput',
23 'serverExternals',
24 'serverDevtool',
25 'serverChainWebpack'
26 ]) : {};
27 const { mode, entry, output, externals, resolve, rules, noParse, plugins, devtool, chainWebpack, js, webpackLog = 'progress' } = sweetConfigCopy;
28 const ecmascript = js === null || js === void 0 ? void 0 : js.ecmascript;
29 const isDevelopment = mode === 'development';
30 // webpack配置
31 const filename = isDevelopment ? '[name].js' : '[name]_[chunkhash:15].js';
32 // 合并配置
33 const mergeConfig = {
34 mode,
35 devtool: devtool !== null && devtool !== void 0 ? devtool : (isDevelopment ? 'eval-source-map' : false),
36 resolve: { extensions: utils_1.extensions },
37 target: ['web', ecmascript ? 'es2020' : 'es5'],
38 performance: { hints: false }
39 };
40 // 文件缓存
41 if (isDevelopment) {
42 mergeConfig.cache = {
43 type: 'filesystem',
44 cacheDirectory: path.join(sweetOptions.basicPath, cacheConfig_1.webpackCache)
45 };
46 }
47 // 日志
48 if (webpackLog === 'progress') {
49 mergeConfig.infrastructureLogging = { level: 'warn' };
50 }
51 config.merge(mergeConfig);
52 // 设置文件输出
53 config
54 .output
55 .publicPath('')
56 .path(path.join(sweetOptions.basicPath, 'dist'))
57 .filename(filename)
58 .globalObject('this');
59 // loaders
60 loaders_1.default(sweetConfigCopy, sweetOptions, config);
61 // plugins
62 plugins_1.default(sweetConfigCopy, sweetOptions, config);
63 // optimization
64 optimization_1.default(sweetConfigCopy, sweetOptions, config, false);
65 /* chainWebpack: 通过webpack-chain的API扩展或修改webpack配置 */
66 if (chainWebpack) {
67 chainWebpack(config);
68 }
69 const mergeConfiguration = {
70 entry,
71 output,
72 externals,
73 resolve,
74 // 添加其他的rules
75 module: {
76 noParse,
77 rules
78 },
79 // 添加自定义的plugins
80 plugins,
81 experiments: {
82 topLevelAwait: true
83 }
84 };
85 /* @ts-ignore 合并自定义配置 */
86 return webpack_merge_1.merge(config.toConfig(), mergeConfiguration);
87}
88exports.default = default_1;