forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.prod.js
More file actions
369 lines (359 loc) · 12.3 KB
/
webpack.prod.js
File metadata and controls
369 lines (359 loc) · 12.3 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* eslint-disable global-require */
// const SentryWebpackPlugin = require('@sentry/webpack-plugin');
const merge = require('webpack-merge');
const webpack = require('webpack');
const ImageminPlugin = require('imagemin-webpack-plugin').default;
const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin');
const TerserJSPlugin = require('terser-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const { normalizeName } = require('webpack/lib/optimize/SplitChunksPlugin');
const ManifestPlugin = require('webpack-manifest-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const VERSION = require('@codesandbox/common/lib/version').default;
// const childProcess = require('child_process');
const commonConfig = require('./webpack.common');
const publicPath = '/';
// const isMaster =
// childProcess
// .execSync(`git branch | grep \\* | cut -d ' ' -f2`)
// .toString()
// .trim() === 'master';
const normalize = normalizeName({ name: true, automaticNameDelimiter: '~' });
module.exports = merge(commonConfig, {
devtool: 'source-map',
output: {
filename: 'static/js/[name].[contenthash:9].js',
chunkFilename: 'static/js/[name].[contenthash:9].chunk.js',
},
mode: 'production',
stats: 'verbose',
// optimization: {
// minimize: false,
// },
optimization: {
minimize: true,
minimizer: [
new TerserJSPlugin({
terserOptions: {
parse: {
// We want terser to parse ecma 8 code. However, we don't want it
// to apply any minification steps that turns valid ecma 5 code
// into invalid ecma 5 code. This is why the 'compress' and 'output'
// sections only apply transformations that are ecma 5 safe
// https://github.com/facebook/create-react-app/pull/4234
ecma: 8,
},
compress: {
ecma: 5,
warnings: false,
// Disabled because of an issue with Uglify breaking seemingly valid code:
// https://github.com/facebook/create-react-app/issues/2376
// Pending further investigation:
// https://github.com/mishoo/UglifyJS2/issues/2011
comparisons: false,
// Disabled because of an issue with Terser breaking valid code:
// https://github.com/facebook/create-react-app/issues/5250
// Pending further investigation:
// https://github.com/terser-js/terser/issues/120
inline: 2,
},
mangle: {
safari10: true,
},
output: {
ecma: 5,
comments: false,
// Turned on because emoji and regex is not minified properly using default
// https://github.com/facebook/create-react-app/issues/2488
ascii_only: true,
},
},
// Use multi-process parallel running to improve the build speed
// Default number of concurrent runs: os.cpus().length - 1
// Disabled on WSL (Windows Subsystem for Linux) due to an issue with Terser
// https://github.com/webpack-contrib/terser-webpack-plugin/issues/21
parallel: 2,
cache: true,
sourceMap: true,
}),
],
concatenateModules: true, // ModuleConcatenationPlugin
namedModules: true, // NamedModulesPlugin()
noEmitOnErrors: true, // NoEmitOnErrorsPlugin
splitChunks: {
chunks: 'all',
maxInitialRequests: 20, // for HTTP2
maxAsyncRequests: 20, // for HTTP2
name(module, chunks, cacheGroup) {
const name = normalize(module, chunks, cacheGroup);
if (name === 'vendors~app~embed~sandbox') {
return 'common-sandbox';
}
if (name === 'vendors~app~embed') {
return 'common';
}
// generate a chunk name using default strategy...
return name;
},
},
},
plugins: [
process.env.ANALYZE && new BundleAnalyzerPlugin(),
new webpack.DefinePlugin({ VERSION: JSON.stringify(VERSION) }),
// Generate a service worker script that will precache, and keep up to date,
// the HTML & assets that are part of the Webpack build.
new SWPrecacheWebpackPlugin({
// By default, a cache-busting query parameter is appended to requests
// used to populate the caches, to ensure the responses are fresh.
// If a URL is already hashed by Webpack, then there is no concern
// about it being stale, and the cache-busting can be skipped.
dontCacheBustUrlsMatching: /\.\w{8}\./,
filename: 'service-worker.js',
cacheId: 'code-sandbox',
logger(message) {
if (message.indexOf('Total precache size is') === 0) {
// This message occurs for every build and is a bit too noisy.
return;
}
if (message.indexOf('Skipping static resource') === 0) {
// This message obscures real errors so we ignore it.
// https://github.com/facebookincubator/create-react-app/issues/2612
return;
}
// eslint-disable-next-line no-console
console.log(message);
},
minify: true,
// For unknown URLs, fallback to the index page
navigateFallback: publicPath + 'app.html',
navigateFallbackWhitelist: [/\/s\//],
// Don't precache sourcemaps (they're large) and build asset manifest:
staticFileGlobsIgnorePatterns: [
/\.map$/,
/vscode/,
/\/vs/,
/asset-manifest\.json$/,
],
maximumFileSizeToCacheInBytes: 1024 * 1024 * 20, // 20mb
runtimeCaching: [
// Don't add this runtime cache as this causes us to give back *old*
// API responses, this will lead people to believe that they lost work
// when they can't connect to our servers.
// {
// urlPattern: /api\/v1\/sandboxes/,
// handler: 'networkFirst',
// options: {
// cache: {
// maxEntries: 50,
// name: 'sandboxes-cache',
// },
// },
// },
{
urlPattern: /^https:\/\/unpkg\.com/,
handler: 'cacheFirst',
options: {
cache: {
maxEntries: 300,
name: 'unpkg-cache',
},
},
},
{
urlPattern: /cloudflare\.com/,
handler: 'cacheFirst',
options: {
cache: {
maxEntries: 20,
name: 'cloudflare-cache',
},
},
},
{
urlPattern: /\/vscode26/,
handler: 'cacheFirst',
options: {
cache: {
maximumFileSizeToCacheInBytes: 1024 * 1024 * 100, // 100mb
name: 'vscode',
},
},
},
{
urlPattern: /vscode-extensions\//,
handler: 'cacheFirst',
options: {
cache: {
maximumFileSizeToCacheInBytes: 1024 * 1024 * 100, // 100mb
name: 'vscode-extensions',
},
},
},
],
}),
// Generate a service worker script that will precache, and keep up to date,
// the HTML & assets that are part of the Webpack build.
new SWPrecacheWebpackPlugin({
// By default, a cache-busting query parameter is appended to requests
// used to populate the caches, to ensure the responses are fresh.
// If a URL is already hashed by Webpack, then there is no concern
// about it being stale, and the cache-busting can be skipped.
dontCacheBustUrlsMatching: /\.\w{8}\./,
filename: 'sandbox-service-worker.js',
cacheId: 'code-sandbox-sandbox',
logger(message) {
if (message.indexOf('Total precache size is') === 0) {
// This message occurs for every build and is a bit too noisy.
return;
}
if (message.indexOf('Skipping static resource') === 0) {
// This message obscures real errors so we ignore it.
// https://github.com/facebookincubator/create-react-app/issues/2612
return;
}
// eslint-disable-next-line no-console
console.log(message);
},
minify: true,
// For unknown URLs, fallback to the index page
navigateFallback: 'https://new.codesandbox.io/frame.html',
staticFileGlobs: process.env.SANDBOX_ONLY
? ['www/index.html']
: ['www/frame.html'],
stripPrefix: 'www/',
// Ignores URLs starting from /__ (useful for Firebase):
// https://github.com/facebookincubator/create-react-app/issues/2237#issuecomment-302693219
navigateFallbackWhitelist: [/^(?!\/__).*/],
// Don't precache sourcemaps (they're large) and build asset manifest:
staticFileGlobsIgnorePatterns: [/\.map$/, /asset-manifest\.json$/],
maximumFileSizeToCacheInBytes: 1024 * 1024 * 20, // 20mb
runtimeCaching: [
{
urlPattern: /api\/v1\/sandboxes/,
handler: 'networkFirst',
options: {
cache: {
maxEntries: 50,
name: 'sandboxes-cache',
},
},
},
{
urlPattern: /api\/v1\/dependencies/,
handler: 'fastest',
options: {
cache: {
maxAgeSeconds: 60 * 60 * 24,
name: 'dependency-version-cache',
},
},
},
{
// These should be dynamic, since it's not loaded from this domain
// But from the root domain
urlPattern: /codesandbox\.io\/static\/js\//,
handler: 'fastest',
options: {
cache: {
// A day
maxAgeSeconds: 60 * 60 * 24,
name: 'static-root-cache',
},
},
},
{
urlPattern: /\.amazonaws\.com\/prod\/package/,
handler: 'fastest',
options: {
cache: {
// a week
maxAgeSeconds: 60 * 60 * 24 * 7,
name: 'dependency-url-generator-cache',
},
},
},
{
urlPattern: /prod-packager-packages\.csb\.dev/,
handler: 'fastest',
options: {
cache: {
maxAgeSeconds: 60 * 60 * 24 * 7,
name: 'dependency-files-cache',
},
},
},
{
urlPattern: /^https:\/\/unpkg\.com/,
handler: 'cacheFirst',
options: {
cache: {
maxEntries: 300,
name: 'unpkg-dep-cache',
maxAgeSeconds: 60 * 60 * 24 * 7,
},
},
},
{
urlPattern: /^https:\/\/cdn\.rawgit\.com/,
handler: 'cacheFirst',
options: {
cache: {
maxEntries: 300,
name: 'rawgit-cache',
maxAgeSeconds: 60 * 60 * 24 * 7,
},
},
},
{
urlPattern: /jsdelivr\.(com|net)/,
handler: 'cacheFirst',
options: {
cache: {
maxEntries: 300,
name: 'jsdelivr-dep-cache',
maxAgeSeconds: 60 * 60 * 24 * 7,
},
},
},
{
urlPattern: /cloudflare\.com/,
handler: 'cacheFirst',
options: {
cache: {
maxEntries: 50,
name: 'cloudflare-cache',
maxAgeSeconds: 60 * 60 * 24 * 7,
},
},
},
],
}),
// Moment.js is an extremely popular library that bundles large locale files
// by default due to how Webpack interprets its code. This is a practical
// solution that requires the user to opt into importing specific locales.
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
// You can remove this if you don't use Moment.js:
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
new ManifestPlugin({
fileName: 'file-manifest.json',
publicPath: commonConfig.output.publicPath,
}),
new CopyWebpackPlugin([
{
from: '../sse-hooks/dist',
to: 'public/sse-hooks',
},
]),
new ImageminPlugin({
pngquant: {
quality: '95-100',
},
}),
// isMaster &&
// new SentryWebpackPlugin({
// include: 'src',
// ignore: ['node_modules', 'webpack.config.js'],
// release: VERSION,
// }),
].filter(Boolean),
});