-
Notifications
You must be signed in to change notification settings - Fork 913
Expand file tree
/
Copy pathwebpack.config.js
More file actions
393 lines (375 loc) · 16.2 KB
/
webpack.config.js
File metadata and controls
393 lines (375 loc) · 16.2 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
const {exec} = require('node:child_process');
const path = require('node:path');
const defaults = require('lodash.defaults');
const webpack = require('webpack');
// Plugins
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const CopyWebpackPlugin = require('copy-webpack-plugin');
const EmitFilePlugin = require('emit-file-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const TerserPlugin = require('terser-webpack-plugin');
// PostCss
const autoprefixer = require('autoprefixer');
/** @type {Array} */
let routes = require('./src/routes.json');
const templateConfig = require('./src/template-config.js');
if (process.env.NODE_ENV !== 'production') {
routes = routes.concat(require('./src/routes-dev.json')); // eslint-disable-line global-require
}
routes = routes.filter(route => !process.env.VIEW || process.env.VIEW === route.view);
const pageRoutes = routes.filter(route => !route.redirect);
/**
* Load the package.json for an installed npm package.
* Uses {@link require.resolve} to locate the package via Node's module resolution algorithm,
* which handles hoisting and workspaces. The resolved value is a file path, so the subsequent
* {@link require} call is a direct file access that bypasses the package's `exports` restriction
* on `./package.json`.
* @param {string} packageName The package name to look up.
* @returns {object} The parsed package.json contents.
*/
const requirePackageJson = packageName => {
let dir = path.dirname(require.resolve(packageName));
do {
try {
const pkg = require(path.join(dir, 'package.json')); // eslint-disable-line global-require
if (pkg.name === packageName) return pkg;
} catch { /* no package.json here, keep walking up */ }
dir = path.dirname(dir);
} while (dir !== path.dirname(dir));
throw new Error(`Could not find package.json for ${packageName}`);
};
const guiVersion = requirePackageJson('@scratch/scratch-gui').version;
// Resolved once at module load time so both version files share the same git call.
// git rev-parse's abbreviated hash uses a dynamic unique-prefix algorithm, matching GitHub's web UI.
const gitHashesPromise = process.env.WWW_VERSION ?
Promise.resolve({fullHash: process.env.WWW_VERSION, shortHash: process.env.WWW_VERSION}) :
new Promise((resolve, reject) => {
exec('git log -1 --format="%H %h"', (err, stdout) => {
if (err) {
reject(err);
} else {
const [fullHash, shortHash] = stdout.trim().split(' ');
resolve({fullHash, shortHash});
}
});
});
/** @returns {Promise<string>} Abbreviated git hash for display in version.txt. */
const makeVersionTxt = () => gitHashesPromise.then(({shortHash}) => shortHash);
/** @returns {Promise<string>} JSON string with full git hash and package versions for version.json. */
const makeVersionJson = () => gitHashesPromise.then(({fullHash}) => JSON.stringify({
'scratch-www': fullHash,
'scratch-gui': guiVersion
}, null, 4));
// Prepare all entry points
const entry = {};
pageRoutes.forEach(route => {
entry[route.name] = [
'./src/init.js',
`./src/views/${route.view}.jsx`
];
});
// HtmlWebpackPlugin v4 removed 'chunks' info that we need for our custom template.
// This plugin is a quick-and-dirty partial implementation of that information.
// Adapted from https://github.com/jantimon/html-webpack-plugin/issues/1369#issuecomment-1049968234
// Thanks, @daniel-nagy!
class HtmlWebpackBackwardsCompatibilityPlugin {
apply (compiler) {
compiler
.hooks
.compilation
.tap('HtmlWebpackBackwardsCompatibilityPlugin', compilation => {
HtmlWebpackPlugin
.getHooks(compilation)
.beforeAssetTagGeneration
.tapAsync(
'HtmlWebpackBackwardsCompatibilityPlugin',
(data, callback) => {
const {publicPath} = data.assets;
const chunks = {};
for (const entryPoint of compilation.entrypoints.values()) {
for (const chunk of entryPoint.chunks) {
const files = Array.from(chunk.files); // convert from Set
chunks[chunk.name] = {
entry: publicPath + files.find(file => file.endsWith('.js')),
css: files
.filter(file => file.endsWith('.css'))
.map(file => publicPath + file)
};
}
}
data.assets.chunks = chunks;
callback(null, data);
}
);
});
}
}
// Config
module.exports = {
entry: entry,
devtool: process.env.NODE_ENV === 'production' ? false : 'eval',
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
output: {
path: path.resolve(__dirname, 'build'),
filename: 'js/[name].bundle.js',
publicPath: '/'
},
resolve: {
fallback: {
// Node modules are no longer polyfilled by default in Webpack 5, so we need to add these here
Buffer: require.resolve('buffer/'),
stream: require.resolve('stream-browserify') // jszip
},
symlinks: false // Fix local development with `npm link` packages
},
module: {
rules: [
{
test: /\.(?:js|mjs|cjs)x?$/,
loader: 'babel-loader',
include: [
path.resolve(__dirname, 'src'),
/node_modules[\\/]scratch-[^\\/]+[\\/]src/,
/node_modules[\\/]pify/,
/node_modules[\\/]async/
],
options: {
presets: ['@babel/preset-env', '@babel/preset-react']
}
},
{
test: /\.hex$/,
type: 'asset/resource',
use: [{
loader: 'url-loader',
options: {
limit: 16 * 1024
}
}]
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
url: false
}
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: function () {
return [autoprefixer()];
}
}
}
},
{
loader: 'sass-loader',
// Silencing deprecation warnings for using legacy-js-api
// https://sass-lang.com/documentation/breaking-changes/legacy-js-api/
// The legacy api will be removed entirely in Dart Sass 2.0.0
options: {
sassOptions: {
silenceDeprecations: ['legacy-js-api']
}
}
}
]
},
{
test: /\.css$/,
use: [
{
loader: 'style-loader'
},
{
loader: 'css-loader',
options: {
modules: {
auto: true,
localIdentName: '[name]_[local]_[hash:base64:5]',
exportLocalsConvention: 'camelCase'
},
importLoaders: 1,
esModule: false
}
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [
'postcss-import',
'postcss-simple-vars',
'autoprefixer'
]
}
}
}
]
},
{
test: /\.(png|jpg|gif|eot|svg|ttf|woff)$/,
loader: 'url-loader'
}
],
noParse: /node_modules\/google-libphonenumber\/dist/
},
optimization: {
splitChunks: {
cacheGroups: {
common: {
chunks: 'all',
name: 'common',
minSize: 1024,
minChunks: pageRoutes.length // Extract only chunks common to all html pages
}
}
},
minimizer: [
new TerserPlugin({
parallel: 4
})
]
},
plugins: [
new MiniCssExtractPlugin(),
new HtmlWebpackBackwardsCompatibilityPlugin(),
new EmitFilePlugin({
filename: 'version.txt',
content: makeVersionTxt
}),
new EmitFilePlugin({
filename: 'version.json',
content: makeVersionJson
}),
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer']
})
].concat(pageRoutes
.map(route => new HtmlWebpackPlugin(defaults({}, {
title: route.title,
filename: `${route.name}.html`,
route: route,
dynamicMetaTags: route.dynamicMetaTags
}, templateConfig)))
).concat([
new CopyWebpackPlugin({
patterns: [
{from: 'static'},
{from: 'intl', to: 'js'},
{
from: 'node_modules/@scratch/scratch-gui/dist/static/blocks-media',
to: 'static/blocks-media'
},
{
from: 'node_modules/@scratch/scratch-gui/dist/chunks/mediapipe/face_detection/',
to: 'chunks/mediapipe/face_detection'
},
{
context: 'node_modules/@scratch/scratch-gui/dist/',
from: 'chunks/fetch-worker.*.{js,js.map}'
},
{
context: 'node_modules/@scratch/scratch-gui/dist/',
// Copy the scripts for loading the translated images
// Their chunks are expected to be on the same path as the assets themselves.
from: 'chunks/*-steps.*.{js,js.map}',
to: 'js'
},
{
from: 'node_modules/@scratch/scratch-gui/dist/extension-worker.js'
},
{
from: 'node_modules/@scratch/scratch-gui/dist/extension-worker.js.map'
},
{
from: 'node_modules/@scratch/scratch-gui/dist/static/assets',
// `publicPath: auto` in scratch-gui is searching for the assets in `/js`, because the
// bundles (hence entrypoints) are located inside `/js`. If we want this to be on base level,
// we'd have to change where the bundles are output as well.
to: 'js/static/assets'
},
{
from: 'node_modules/@scratch/scratch-gui/dist/*.hex',
to: 'static',
flatten: true
}
]
}),
new webpack.DefinePlugin({
'process.env.NODE_ENV': `"${process.env.NODE_ENV || 'development'}"`,
'process.env.API_HOST': `"${process.env.API_HOST || 'https://api.scratch.mit.edu'}"`,
'process.env.ROOT_URL': `"${process.env.ROOT_URL || 'https://scratch.mit.edu'}"`,
'process.env.RECAPTCHA_SITE_KEY': `"${
process.env.RECAPTCHA_SITE_KEY || '6Lf6kK4UAAAAABKTyvdSqgcSVASEnMrCquiAkjVW'}"`,
'process.env.ASSET_HOST': `"${process.env.ASSET_HOST || 'https://assets.scratch.mit.edu'}"`,
'process.env.BACKPACK_HOST': `"${process.env.BACKPACK_HOST || 'https://backpack.scratch.mit.edu'}"`,
'process.env.CLOUDDATA_HOST': `"${process.env.CLOUDDATA_HOST || 'clouddata.scratch.mit.edu'}"`,
'process.env.PROJECT_HOST': `"${process.env.PROJECT_HOST || 'https://projects.scratch.mit.edu'}"`,
'process.env.STATIC_HOST': `"${process.env.STATIC_HOST || 'https://uploads.scratch.mit.edu'}"`,
'process.env.SCRATCH_ENV': `"${process.env.SCRATCH_ENV || 'development'}"`,
'process.env.THUMBNAIL_URI': `"${process.env.THUMBNAIL_URI || '/internalapi/project/thumbnail/{}/set/'}"`,
'process.env.THUMBNAIL_HOST': `"${process.env.THUMBNAIL_HOST || ''}"`,
'process.env.DEBUG': Boolean(process.env.DEBUG),
'process.env.GA_ID': `"${process.env.GA_ID || 'UA-000000-01'}"`,
'process.env.GTM_ENV_AUTH': `"${process.env.GTM_ENV_AUTH || ''}"`,
'process.env.GTM_ID': process.env.GTM_ID ? `"${process.env.GTM_ID}"` : null,
'process.env.ONBOARDING_TEST_ACTIVE': `"${
process.env.ONBOARDING_TEST_ACTIVE || false
}"`,
'process.env.ONBOARDING_TEST_PROJECT_IDS': `'${process.env.ONBOARDING_TEST_PROJECT_IDS || JSON.stringify(
{
clicker: '10128368',
pong: '10128515',
animateCharacter: '10128067',
makeItFly: '114019829',
recordSound: '1031325137',
makeMusic: '10012676'
}
)}'`,
'process.env.ONBOARDING_TESTING_STARTING_DATE': `"${
process.env.ONBOARDING_TESTING_STARTING_DATE || '2024-01-20'
}"`,
'process.env.ONBOARDING_TESTING_ENDING_DATE': `"${
process.env.ONBOARDING_TESTING_ENDING_DATE || '2030-11-20'
}"`,
'process.env.QUALITATIVE_FEEDBACK_ACTIVE': `"${
process.env.QUALITATIVE_FEEDBACK_ACTIVE || false
}"`,
'process.env.QUALITATIVE_FEEDBACK_STARTING_DATE': `"${
process.env.QUALITATIVE_FEEDBACK_STARTING_DATE || '2024-01-20'
}"`,
'process.env.QUALITATIVE_FEEDBACK_ENDING_DATE': `"${
process.env.QUALITATIVE_FEEDBACK_ENDING_DATE || '2024-11-20'
}"`,
// Given user frequency X, show qualitative feedback to 1 in X users
'process.env.QUALITATIVE_FEEDBACK_IDEAS_GENERATOR_USER_FREQUENCY': `"${
process.env.QUALITATIVE_FEEDBACK_IDEAS_GENERATOR_USER_FREQUENCY || 2
}"`,
'process.env.QUALITATIVE_FEEDBACK_STARTER_PROJECTS_USER_FREQUENCY': `"${
process.env.QUALITATIVE_FEEDBACK_STARTER_PROJECTS_USER_FREQUENCY || 2
}"`,
'process.env.QUALITATIVE_FEEDBACK_DEBUGGING_USER_FREQUENCY': `"${
process.env.QUALITATIVE_FEEDBACK_DEBUGGING_USER_FREQUENCY || 2
}"`,
'process.env.QUALITATIVE_FEEDBACK_TUTORIALS_USER_FREQUENCY': `"${
process.env.QUALITATIVE_FEEDBACK_TUTORIALS_USER_FREQUENCY || 2
}"`,
'process.env.IDEAS_GENERATOR_SOURCE': `"${
process.env.IDEAS_GENERATOR_SOURCE || 'https://scratch.mit.edu/projects/1108790117'
}"`,
'process.env.MANUALLY_SAVE_THUMBNAILS': `"${
process.env.MANUALLY_SAVE_THUMBNAILS || 'true'
}"`
})
])
.concat(process.env.ANALYZE_BUNDLE === 'true' ? [
new BundleAnalyzerPlugin()
] : [])
};