UNPKG

3.1 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 'hot'
21 ]) : {};
22 const { mode, serverEntry, serverOutput, serverExternals, resolve, rules, noParse, plugins, serverDevtool, serverChainWebpack } = sweetConfigCopy;
23 const isDevelopment = mode === 'development';
24 // 格式化ecmascript配置
25 if (sweetConfigCopy.js && _.isPlainObject(sweetConfigCopy.js)) {
26 sweetConfigCopy.js.ecmascript = true;
27 }
28 else {
29 sweetConfigCopy.js = { ecmascript: true };
30 }
31 // 合并配置
32 config
33 .merge({
34 mode,
35 devtool: serverDevtool !== null && serverDevtool !== void 0 ? serverDevtool : (isDevelopment ? 'eval-source-map' : 'source-map'),
36 resolve: { extensions: utils_1.extensions },
37 target: ['node', 'node10'],
38 performance: { hints: false },
39 node: {
40 __filename: true,
41 __dirname: true
42 },
43 // 文件缓存
44 cache: isDevelopment ? {
45 type: 'filesystem',
46 cacheDirectory: path.join(sweetOptions.basicPath, cacheConfig_1.webpackServerCache)
47 } : false
48 });
49 // 设置文件输出
50 config
51 .output
52 .path(path.join(sweetOptions.basicPath, 'dist-server'))
53 .publicPath('')
54 .filename('[name].js')
55 .library('[name]')
56 .libraryTarget('umd')
57 .globalObject('this');
58 // loaders
59 loaders_1.default(sweetConfigCopy, sweetOptions, config);
60 // plugins
61 plugins_1.default(sweetConfigCopy, sweetOptions, config);
62 // optimization
63 optimization_1.default(sweetConfigCopy, sweetOptions, config, true);
64 /* serverChainWebpack: 通过webpack-chain的API扩展或修改webpack配置 */
65 if (serverChainWebpack) {
66 serverChainWebpack(config);
67 }
68 const mergeConfiguration = {
69 entry: serverEntry,
70 output: serverOutput,
71 externals: serverExternals,
72 resolve,
73 // 添加其他的rules
74 module: {
75 noParse,
76 rules
77 },
78 // 添加自定义的plugins
79 plugins,
80 experiments: {
81 topLevelAwait: true
82 }
83 };
84 /* @ts-ignore 合并自定义配置 */
85 return webpack_merge_1.merge(config.toConfig(), mergeConfiguration);
86}
87exports.default = default_1;