UNPKG

3.98 kBJavaScriptView Raw
1'use strict'
2
3const webpack = require('webpack')
4const config = require('../config')
5const { isObject } = require('@mara/devkit')
6const TerserPlugin = require('terser-webpack-plugin')
7const { babelLoader } = require('./loaders/babel-loader')
8const library = '[name]_lib'
9
10// 支持两种格式配置
11// 数组 vendor: ['react', 'react-dom']
12// 对象 vendor: {libs: ['react', 'react-dom']}
13const vendor = isObject(config.vendor) ? config.vendor.libs : config.vendor
14// 为多页面准备,生成 xxx_vender 文件夹
15const namespace = config.vendor.name ? `${config.vendor.name}_` : ''
16
17/**
18 * dll 配置
19 * @param {String} context 构建上下文
20 * @return {Object} webpack 配置对象
21 */
22module.exports = function buildDll(context) {
23 const webpackBaseConf = require('./webpack.base.conf')(context)
24
25 return {
26 mode: 'production',
27 entry: {
28 vendor
29 },
30 output: {
31 filename: '[name].dll.js',
32 path: `${config.paths.dist}/${namespace}vendor`,
33 library
34 },
35 optimization: {
36 minimize: config.debug !== true,
37 minimizer: [
38 new TerserPlugin({
39 terserOptions: {
40 parse: {
41 // we want terser to parse ecma 8 code. However, we don't want it
42 // to apply any minfication steps that turns valid ecma 5 code
43 // into invalid ecma 5 code. This is why the 'compress' and 'output'
44 // sections only apply transformations that are ecma 5 safe
45 // https://github.com/facebook/create-react-app/pull/4234
46 ecma: 8
47 },
48 compress: {
49 ecma: 5,
50 warnings: false,
51 // Disabled because of an issue with Uglify breaking seemingly valid code:
52 // https://github.com/facebook/create-react-app/issues/2376
53 // Pending further investigation:
54 // https://github.com/mishoo/UglifyJS2/issues/2011
55 comparisons: false,
56 // Disabled because of an issue with Terser breaking valid code:
57 // https://github.com/facebook/create-react-app/issues/5250
58 // Pending futher investigation:
59 // https://github.com/terser-js/terser/issues/120
60 inline: 2
61 },
62 mangle: {
63 safari10: true
64 },
65 output: {
66 ecma: 5,
67 comments: false,
68 // Turned on because emoji and regex is not minified properly using default
69 // https://github.com/facebook/create-react-app/issues/2488
70 ascii_only: true
71 }
72 },
73 // Use multi-process parallel running to improve the build speed
74 // Default number of concurrent runs: os.cpus().length - 1
75 parallel: true,
76 // Enable file caching
77 cache: true,
78 sourceMap: false
79 })
80 ],
81 // Keep the runtime chunk seperated to enable long term caching
82 // https://twitter.com/wSokra/status/969679223278505985
83 // set false until https://github.com/webpack/webpack/issues/6598 be resolved
84 runtimeChunk: false
85 },
86 resolve: webpackBaseConf.resolve,
87 module: {
88 rules: [
89 {
90 // Process JS with Babel.
91 oneOf: babelLoader(true)
92 }
93 ]
94 },
95 plugins: [
96 new webpack.DefinePlugin(context.buildEnv.stringified),
97 new webpack.DllPlugin({
98 path: `${config.paths.dll}/${namespace}manifest.json`,
99 // This must match the output.library option above
100 name: library
101 })
102 ],
103 node: {
104 // prevent webpack from injecting useless setImmediate polyfill because Vue
105 // source contains it (although only uses it if it's native).
106 setImmediate: false,
107 module: 'empty',
108 dgram: 'empty',
109 fs: 'empty',
110 net: 'empty',
111 tls: 'empty',
112 child_process: 'empty'
113 },
114 performance: {
115 hints: false
116 }
117 }
118}