forked from react-static/react-static
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.prod.js
executable file
路189 lines (179 loc) 路 5.37 KB
/
webpack.config.prod.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import webpack from 'webpack'
import path from 'path'
import CaseSensitivePathsPlugin from 'case-sensitive-paths-webpack-plugin'
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'
import TerserPlugin from 'terser-webpack-plugin'
import nodeExternals from 'webpack-node-externals'
import ExtractCssChunks from 'extract-css-chunks-webpack-plugin'
import OptimizeCSSAssetsPlugin from 'optimize-css-assets-webpack-plugin'
import resolveFrom from 'resolve-from'
//
import rules from './rules'
function common(state) {
const { analyze, config, debug } = state
const { ROOT, DIST, NODE_MODULES, SRC, ASSETS } = config.paths
process.env.REACT_STATIC_ENTRY_PATH = config.entry
process.env.REACT_STATIC_SITE_ROOT = config.siteRoot
process.env.REACT_STATIC_BASE_PATH = config.basePath
process.env.REACT_STATIC_PUBLIC_PATH = config.publicPath
process.env.REACT_STATIC_ASSETS_PATH = config.assetsPath
if (!DIST.startsWith(ROOT)) {
// we build outside of project dir, so reset some paths
process.env.REACT_STATIC_ASSETS_PATH = config.assetsPath.replace(DIST, '')
}
const splitChunks = {
chunks: 'all',
minSize: 10000,
minChunks: 1,
maxAsyncRequests: 5,
maxInitialRequests: 5,
automaticNameDelimiter: '~',
name: true,
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10,
chunks: 'all',
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true,
},
},
}
let extrackCSSChunks = new ExtractCssChunks({
filename: '[name].[chunkHash:8].css',
chunkFilename: '[id].[chunkHash:8].css',
})
if (!config.extractCssChunks) {
splitChunks.cacheGroups = {
styles: {
name: 'styles',
test: /\.css$/,
chunks: 'all',
enforce: true,
},
}
extrackCSSChunks = new ExtractCssChunks({
filename: '[name].[chunkHash:8].css',
})
}
return {
mode: 'production',
context: path.resolve(__dirname, '../../../node_modules'),
entry: config.disableRuntime
? config.entry
: [
require.resolve('../../bootstrapPlugins'),
require.resolve('../../bootstrapTemplates'),
require.resolve('../../bootstrapApp'),
],
output: {
filename: '[name].[hash:8].js', // dont use chunkhash, its not a chunk
chunkFilename: 'templates/[name].[chunkHash:8].js',
path: ASSETS,
publicPath: process.env.REACT_STATIC_ASSETS_PATH || '/',
},
optimization: {
sideEffects: true,
minimize: true,
minimizer: [
new TerserPlugin({
cache: true,
parallel: true,
exclude: /\.min\.js/,
...config.terser,
sourceMap:
config.productionSourceMaps || config.terser.sourceMap || debug,
terserOptions: {
ie8: false,
...config.terser.terserOptions,
mangle: { safari10: true, ...config.terser.terserOptions.mangle },
parse: { ecma: 8, ...config.terser.terserOptions.parse },
compress: { ecma: 5, ...config.terser.terserOptions.compress },
output: { ecma: 5, ...config.terser.terserOptions.output },
},
}),
new OptimizeCSSAssetsPlugin({}),
],
splitChunks,
},
performance: {
maxEntrypointSize: 300000,
},
module: {
rules: rules({ config, stage: 'prod', isNode: false }),
strictExportPresence: true,
},
resolve: {
modules: [
NODE_MODULES,
SRC,
DIST,
...[NODE_MODULES, SRC, DIST].map(d =>
DIST.startsWith(ROOT) ? path.relative(__dirname, d) : path.resolve(d)
),
'node_modules',
],
extensions: ['.wasm', '.mjs', '.js', '.json', '.jsx'],
alias: {
react: resolveFrom(config.paths.NODE_MODULES, 'react'),
'react-dom': resolveFrom(config.paths.NODE_MODULES, 'react-dom'),
'react-universal-component': resolveFrom(
__dirname,
'react-universal-component'
),
},
},
externals: [],
target: undefined,
plugins: [
new webpack.EnvironmentPlugin(process.env),
extrackCSSChunks,
new CaseSensitivePathsPlugin(),
analyze && new BundleAnalyzerPlugin(),
].filter(d => d),
devtool: debug || config.productionSourceMaps ? 'source-map' : false,
}
}
export default function(state) {
const {
stage,
config: { paths },
} = state
const result = common(state)
if (stage !== 'node') return result
// Node only!!!
result.output.filename = 'static-app.js'
result.output.path = paths.ARTIFACTS
result.output.libraryTarget = 'umd'
result.optimization.minimize = false
result.optimization.minimizer = []
result.target = 'node'
result.devtool = false
result.externals = [
new RegExp(`${paths.PLUGINS}`),
(context, request, callback) => {
const resolved = path.resolve(context, request)
if (
[/react-static(\\|\/)lib(\\|\/)browser/, /webpack-flush-chunks/].some(
d => d.test(resolved)
)
) {
return callback(null, `commonjs ${resolved}`)
}
callback()
},
nodeExternals(),
]
result.module.rules = rules(state)
result.plugins = [
new webpack.EnvironmentPlugin(process.env),
new CaseSensitivePathsPlugin(),
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1,
}),
]
return result
}