2018-01-31 00:53:12 +08:00
|
|
|
const defaults = require('lodash.defaults');
|
|
|
|
const gitsha = require('git-bundle-sha');
|
|
|
|
const path = require('path');
|
|
|
|
const webpack = require('webpack');
|
2015-10-14 03:40:51 +08:00
|
|
|
|
2021-02-20 01:51:39 +08:00
|
|
|
// Plugins
|
|
|
|
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
|
|
|
|
const CopyWebpackPlugin = require('copy-webpack-plugin');
|
2024-01-19 04:37:11 +08:00
|
|
|
const EmitFilePlugin = require('emit-file-webpack-plugin');
|
2021-02-20 01:51:39 +08:00
|
|
|
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
2021-07-09 23:09:51 +08:00
|
|
|
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
2022-01-29 05:56:46 +08:00
|
|
|
const TerserPlugin = require('terser-webpack-plugin');
|
2021-02-20 01:51:39 +08:00
|
|
|
|
|
|
|
// PostCss
|
|
|
|
const autoprefixer = require('autoprefixer');
|
|
|
|
|
2024-01-18 04:40:27 +08:00
|
|
|
/** @type {Array} */
|
2018-01-31 00:53:12 +08:00
|
|
|
let routes = require('./src/routes.json');
|
2024-01-18 04:40:27 +08:00
|
|
|
const templateConfig = require('./src/template-config.js');
|
2016-04-12 20:23:43 +08:00
|
|
|
|
2016-05-12 06:44:54 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2018-01-31 00:53:12 +08:00
|
|
|
routes = routes.concat(require('./src/routes-dev.json')); // eslint-disable-line global-require
|
2016-05-12 06:44:54 +08:00
|
|
|
}
|
|
|
|
|
2018-03-08 04:16:46 +08:00
|
|
|
routes = routes.filter(route => !process.env.VIEW || process.env.VIEW === route.view);
|
|
|
|
|
2024-01-18 04:40:27 +08:00
|
|
|
const pageRoutes = routes.filter(route => !route.redirect);
|
2018-01-31 00:53:12 +08:00
|
|
|
|
2024-01-19 04:37:11 +08:00
|
|
|
/**
|
|
|
|
* Retrieve a version ID string for the current build, to be emitted into `version.txt`.
|
|
|
|
* @returns {Promise<string>} A promise that resolves to a version ID string.
|
|
|
|
*/
|
|
|
|
const getVersionId = () => {
|
|
|
|
if (process.env.WWW_VERSION) {
|
|
|
|
return Promise.resolve(process.env.WWW_VERSION);
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
gitsha({length: 5}, (err, sha) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(sha);
|
2018-01-31 00:53:12 +08:00
|
|
|
}
|
2024-01-19 04:37:11 +08:00
|
|
|
});
|
2016-04-29 03:40:50 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-09-04 13:26:56 +08:00
|
|
|
// Prepare all entry points
|
2024-01-18 04:40:27 +08:00
|
|
|
const entry = {};
|
2021-02-20 01:51:39 +08:00
|
|
|
|
2024-01-18 04:40:27 +08:00
|
|
|
pageRoutes.forEach(route => {
|
2021-02-20 01:51:39 +08:00
|
|
|
entry[route.name] = [
|
|
|
|
'./src/init.js',
|
|
|
|
`./src/views/${route.view}.jsx`
|
|
|
|
];
|
2015-09-04 13:26:56 +08:00
|
|
|
});
|
|
|
|
|
2024-01-19 03:54:47 +08:00
|
|
|
// 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) {
|
2024-01-19 08:03:30 +08:00
|
|
|
const files = Array.from(chunk.files); // convert from Set
|
2024-01-19 03:54:47 +08:00
|
|
|
chunks[chunk.name] = {
|
2024-01-19 08:03:30 +08:00
|
|
|
entry: publicPath + files.find(file => file.endsWith('.js')),
|
|
|
|
css: files
|
2024-01-19 03:54:47 +08:00
|
|
|
.filter(file => file.endsWith('.css'))
|
|
|
|
.map(file => publicPath + file)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data.assets.chunks = chunks;
|
|
|
|
|
|
|
|
callback(null, data);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-04 13:26:56 +08:00
|
|
|
// Config
|
2015-09-03 03:08:58 +08:00
|
|
|
module.exports = {
|
2015-09-04 13:26:56 +08:00
|
|
|
entry: entry,
|
2024-01-19 05:03:34 +08:00
|
|
|
devtool: process.env.NODE_ENV === 'production' ? false : 'eval',
|
2021-02-20 01:51:39 +08:00
|
|
|
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
|
2015-09-03 03:08:58 +08:00
|
|
|
output: {
|
2015-11-11 02:38:28 +08:00
|
|
|
path: path.resolve(__dirname, 'build'),
|
2021-07-09 21:35:50 +08:00
|
|
|
filename: 'js/[name].bundle.js',
|
|
|
|
publicPath: '/'
|
2015-09-03 03:08:58 +08:00
|
|
|
},
|
2021-02-19 23:43:15 +08:00
|
|
|
resolve: {
|
2024-01-19 05:03:34 +08:00
|
|
|
fallback: {
|
2024-02-27 12:21:30 +08:00
|
|
|
// Node modules are no longer polyfilled by default in Webpack 5, so we need to add these here
|
2024-06-08 00:44:36 +08:00
|
|
|
Buffer: require.resolve('buffer/'),
|
2024-02-27 12:21:30 +08:00
|
|
|
stream: require.resolve('stream-browserify') // jszip
|
2024-01-19 05:03:34 +08:00
|
|
|
},
|
2021-02-19 23:43:15 +08:00
|
|
|
symlinks: false // Fix local development with `npm link` packages
|
|
|
|
},
|
2015-09-03 03:08:58 +08:00
|
|
|
module: {
|
2018-01-31 00:53:12 +08:00
|
|
|
rules: [
|
2015-09-03 03:08:58 +08:00
|
|
|
{
|
2024-01-23 02:43:07 +08:00
|
|
|
test: /\.(?:js|mjs|cjs)x?$/,
|
2018-01-31 00:53:12 +08:00
|
|
|
loader: 'babel-loader',
|
2019-03-07 22:22:53 +08:00
|
|
|
include: [
|
|
|
|
path.resolve(__dirname, 'src'),
|
|
|
|
/node_modules[\\/]scratch-[^\\/]+[\\/]src/,
|
2019-07-12 23:56:34 +08:00
|
|
|
/node_modules[\\/]pify/,
|
|
|
|
/node_modules[\\/]async/
|
2024-07-04 15:35:03 +08:00
|
|
|
],
|
|
|
|
options: {
|
|
|
|
presets: ['@babel/preset-env', '@babel/preset-react']
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.hex$/,
|
|
|
|
type: 'asset/resource',
|
|
|
|
use: [{
|
|
|
|
loader: 'url-loader',
|
|
|
|
options: {
|
|
|
|
limit: 16 * 1024
|
|
|
|
}
|
|
|
|
}]
|
2015-09-03 03:08:58 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.scss$/,
|
2018-01-31 00:53:12 +08:00
|
|
|
use: [
|
2021-07-09 23:09:51 +08:00
|
|
|
MiniCssExtractPlugin.loader,
|
2022-03-02 23:55:33 +08:00
|
|
|
{
|
|
|
|
loader: 'css-loader',
|
|
|
|
options: {
|
|
|
|
url: false
|
|
|
|
}
|
|
|
|
},
|
2018-01-31 00:53:12 +08:00
|
|
|
{
|
|
|
|
loader: 'postcss-loader',
|
|
|
|
options: {
|
2022-01-27 04:50:40 +08:00
|
|
|
postcssOptions: {
|
|
|
|
plugins: function () {
|
2022-02-14 23:17:39 +08:00
|
|
|
return [autoprefixer()];
|
2022-01-27 04:50:40 +08:00
|
|
|
}
|
2018-01-31 00:53:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'sass-loader'
|
|
|
|
]
|
2015-09-09 02:56:28 +08:00
|
|
|
},
|
2016-06-03 03:25:02 +08:00
|
|
|
{
|
|
|
|
test: /\.css$/,
|
2018-01-31 00:53:12 +08:00
|
|
|
use: [
|
2024-07-04 15:35:03 +08:00
|
|
|
{
|
|
|
|
loader: 'style-loader'
|
|
|
|
},
|
2022-03-02 23:55:33 +08:00
|
|
|
{
|
|
|
|
loader: 'css-loader',
|
|
|
|
options: {
|
2024-07-04 15:35:03 +08:00
|
|
|
modules: {
|
2024-10-09 16:24:20 +08:00
|
|
|
auto: true,
|
2024-07-04 15:35:03 +08:00
|
|
|
localIdentName: '[name]_[local]_[hash:base64:5]',
|
|
|
|
exportLocalsConvention: 'camelCase'
|
|
|
|
},
|
|
|
|
importLoaders: 1,
|
|
|
|
esModule: false
|
2022-03-02 23:55:33 +08:00
|
|
|
}
|
|
|
|
},
|
2018-01-31 00:53:12 +08:00
|
|
|
{
|
|
|
|
loader: 'postcss-loader',
|
|
|
|
options: {
|
2022-01-27 04:50:40 +08:00
|
|
|
postcssOptions: {
|
2024-07-04 15:35:03 +08:00
|
|
|
plugins: [
|
|
|
|
'postcss-import',
|
|
|
|
'postcss-simple-vars',
|
|
|
|
'autoprefixer'
|
|
|
|
]
|
2018-01-31 00:53:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2016-06-03 03:25:02 +08:00
|
|
|
},
|
2015-09-09 02:56:28 +08:00
|
|
|
{
|
|
|
|
test: /\.(png|jpg|gif|eot|svg|ttf|woff)$/,
|
|
|
|
loader: 'url-loader'
|
2015-09-03 03:08:58 +08:00
|
|
|
}
|
2016-06-03 03:25:02 +08:00
|
|
|
],
|
|
|
|
noParse: /node_modules\/google-libphonenumber\/dist/
|
2015-09-03 04:33:31 +08:00
|
|
|
},
|
2021-02-20 01:51:39 +08:00
|
|
|
optimization: {
|
|
|
|
splitChunks: {
|
|
|
|
cacheGroups: {
|
|
|
|
common: {
|
|
|
|
chunks: 'all',
|
|
|
|
name: 'common',
|
2024-01-19 05:03:34 +08:00
|
|
|
minSize: 1024,
|
2021-02-20 01:51:39 +08:00
|
|
|
minChunks: pageRoutes.length // Extract only chunks common to all html pages
|
|
|
|
}
|
|
|
|
}
|
2022-01-29 05:56:46 +08:00
|
|
|
},
|
|
|
|
minimizer: [
|
|
|
|
new TerserPlugin({
|
|
|
|
parallel: 4
|
|
|
|
})
|
|
|
|
]
|
2021-02-20 01:51:39 +08:00
|
|
|
},
|
2015-09-03 04:33:31 +08:00
|
|
|
plugins: [
|
2021-07-09 23:09:51 +08:00
|
|
|
new MiniCssExtractPlugin(),
|
2024-01-19 03:54:47 +08:00
|
|
|
new HtmlWebpackBackwardsCompatibilityPlugin(),
|
2024-01-19 04:37:11 +08:00
|
|
|
new EmitFilePlugin({
|
|
|
|
filename: 'version.txt',
|
|
|
|
content: getVersionId
|
2024-07-04 15:35:03 +08:00
|
|
|
}),
|
|
|
|
new webpack.ProvidePlugin({
|
|
|
|
Buffer: ['buffer', 'Buffer']
|
2024-01-19 04:37:11 +08:00
|
|
|
})
|
2021-02-20 01:51:39 +08:00
|
|
|
].concat(pageRoutes
|
2024-01-18 04:40:27 +08:00
|
|
|
.map(route => new HtmlWebpackPlugin(defaults({}, {
|
|
|
|
title: route.title,
|
|
|
|
filename: `${route.name}.html`,
|
|
|
|
route: route,
|
|
|
|
dynamicMetaTags: route.dynamicMetaTags
|
|
|
|
}, templateConfig)))
|
2018-03-19 23:58:03 +08:00
|
|
|
).concat([
|
2022-02-03 05:58:48 +08:00
|
|
|
new CopyWebpackPlugin({
|
|
|
|
patterns: [
|
|
|
|
{from: 'static'},
|
2023-06-27 00:12:05 +08:00
|
|
|
{from: 'intl', to: 'js'},
|
2022-02-03 05:58:48 +08:00
|
|
|
{
|
|
|
|
from: 'node_modules/scratch-gui/dist/static/blocks-media',
|
|
|
|
to: 'static/blocks-media'
|
2023-06-27 00:12:05 +08:00
|
|
|
},
|
2022-02-03 05:58:48 +08:00
|
|
|
{
|
|
|
|
from: 'node_modules/scratch-gui/dist/chunks',
|
|
|
|
to: 'static/chunks'
|
2023-06-27 00:12:05 +08:00
|
|
|
},
|
2022-02-03 05:58:48 +08:00
|
|
|
{
|
2024-09-06 00:45:03 +08:00
|
|
|
from: 'node_modules/scratch-gui/dist/extension-worker.js',
|
|
|
|
to: 'js'
|
2023-06-27 00:12:05 +08:00
|
|
|
},
|
2022-02-03 05:58:48 +08:00
|
|
|
{
|
2024-09-06 00:45:03 +08:00
|
|
|
from: 'node_modules/scratch-gui/dist/extension-worker.js.map',
|
|
|
|
to: 'js'
|
2023-06-27 00:12:05 +08:00
|
|
|
},
|
2022-02-03 05:58:48 +08:00
|
|
|
{
|
|
|
|
from: 'node_modules/scratch-gui/dist/static/assets',
|
2024-09-06 00:45:03 +08:00
|
|
|
|
|
|
|
// TODO: why do tutorials and extension icons expect these files in `js/`?
|
|
|
|
to: 'js/static/assets'
|
2023-06-27 00:12:46 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
from: 'node_modules/scratch-gui/dist/*.hex',
|
|
|
|
to: 'static',
|
|
|
|
flatten: true
|
2022-02-03 05:58:48 +08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}),
|
2021-02-20 01:51:39 +08:00
|
|
|
new webpack.DefinePlugin({
|
2024-01-18 04:40:27 +08:00
|
|
|
'process.env.NODE_ENV': `"${process.env.NODE_ENV || 'development'}"`,
|
|
|
|
'process.env.API_HOST': `"${process.env.API_HOST || 'https://api.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'}"`,
|
2024-10-16 20:45:49 +08:00
|
|
|
'process.env.PROJECT_HOST': `"${process.env.PROJECT_HOST || 'https://projects.scratch.mit.edu'}"`,
|
2024-01-18 04:40:27 +08:00
|
|
|
'process.env.STATIC_HOST': `"${process.env.STATIC_HOST || 'https://uploads.scratch.mit.edu'}"`,
|
2024-05-10 17:23:07 +08:00
|
|
|
'process.env.SCRATCH_ENV': `"${process.env.SCRATCH_ENV || 'development'}"`,
|
2024-10-16 20:45:49 +08:00
|
|
|
'process.env.THUMBNAIL_URI': `"${process.env.THUMBNAIL_URI || '/internalapi/project/thumbnail/{}/set/'}"`,
|
|
|
|
'process.env.THUMBNAIL_HOST': `"${process.env.THUMBNAIL_HOST || ''}"`,
|
2024-07-04 15:35:03 +08:00
|
|
|
'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 || ''}"`,
|
2024-08-20 22:13:23 +08:00
|
|
|
'process.env.GTM_ID': process.env.GTM_ID ? `"${process.env.GTM_ID}"` : null,
|
2024-11-06 17:19:50 +08:00
|
|
|
'process.env.ONBOARDING_TEST_ACTIVE': `"${
|
2024-11-08 20:25:33 +08:00
|
|
|
process.env.ONBOARDING_TEST_ACTIVE || false
|
2024-10-23 16:02:39 +08:00
|
|
|
}"`,
|
2024-11-08 19:43:40 +08:00
|
|
|
'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'
|
|
|
|
}
|
|
|
|
)}'`,
|
2024-10-23 16:02:39 +08:00
|
|
|
'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'
|
|
|
|
}"`
|
2021-02-20 01:51:39 +08:00
|
|
|
})
|
2018-02-09 01:01:07 +08:00
|
|
|
])
|
2021-02-20 01:51:39 +08:00
|
|
|
.concat(process.env.ANALYZE_BUNDLE === 'true' ? [
|
|
|
|
new BundleAnalyzerPlugin()
|
2018-03-19 23:58:03 +08:00
|
|
|
] : [])
|
2015-09-03 03:08:58 +08:00
|
|
|
};
|