2023-04-10 20:05:22 +03:00
|
|
|
/* eslint-env node */
|
2022-08-11 02:38:23 +03:00
|
|
|
const { VanillaExtractPlugin } = require('@vanilla-extract/webpack-plugin')
|
2023-05-16 00:07:05 +03:00
|
|
|
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin')
|
2023-04-13 22:43:47 +03:00
|
|
|
const { execSync } = require('child_process')
|
2022-08-11 02:38:23 +03:00
|
|
|
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
|
2023-05-16 00:07:05 +03:00
|
|
|
const path = require('path')
|
|
|
|
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin')
|
2023-08-08 20:38:37 +03:00
|
|
|
const TerserPlugin = require('terser-webpack-plugin')
|
|
|
|
const { IgnorePlugin, ProvidePlugin } = require('webpack')
|
2023-07-07 00:54:49 +03:00
|
|
|
const { RetryChunkLoadPlugin } = require('webpack-retry-chunk-load-plugin')
|
2022-09-22 22:29:29 +03:00
|
|
|
|
2023-04-13 22:43:47 +03:00
|
|
|
const commitHash = execSync('git rev-parse HEAD').toString().trim()
|
2023-04-17 19:28:00 +03:00
|
|
|
const isProduction = process.env.NODE_ENV === 'production'
|
|
|
|
|
2023-08-08 20:38:37 +03:00
|
|
|
process.env.REACT_APP_GIT_COMMIT_HASH = commitHash
|
|
|
|
|
2023-04-17 19:28:00 +03:00
|
|
|
// Linting and type checking are only necessary as part of development and testing.
|
|
|
|
// Omit them from production builds, as they slow down the feedback loop.
|
|
|
|
const shouldLintOrTypeCheck = !isProduction
|
2022-08-11 02:38:23 +03:00
|
|
|
|
2023-05-16 00:07:05 +03:00
|
|
|
function getCacheDirectory(cacheName) {
|
|
|
|
// Include the trailing slash to denote that this is a directory.
|
|
|
|
return `${path.join(__dirname, 'node_modules/.cache/', cacheName)}/`
|
|
|
|
}
|
|
|
|
|
2022-08-11 02:38:23 +03:00
|
|
|
module.exports = {
|
2023-04-17 19:28:00 +03:00
|
|
|
eslint: {
|
|
|
|
enable: shouldLintOrTypeCheck,
|
|
|
|
pluginOptions(eslintConfig) {
|
|
|
|
return Object.assign(eslintConfig, {
|
|
|
|
cache: true,
|
2023-05-16 00:07:05 +03:00
|
|
|
cacheLocation: getCacheDirectory('eslint'),
|
2023-04-20 23:44:57 +03:00
|
|
|
ignorePath: '.gitignore',
|
2023-05-16 00:07:05 +03:00
|
|
|
// Use our own eslint/plugins/config, as overrides interfere with caching.
|
|
|
|
// This ensures that `yarn start` and `yarn lint` share one cache.
|
|
|
|
eslintPath: require.resolve('eslint'),
|
|
|
|
resolvePluginsRelativeTo: null,
|
|
|
|
baseConfig: null,
|
2023-04-17 19:28:00 +03:00
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
typescript: {
|
|
|
|
enableTypeChecking: shouldLintOrTypeCheck,
|
|
|
|
},
|
2023-01-10 00:54:48 +03:00
|
|
|
jest: {
|
|
|
|
configure(jestConfig) {
|
2023-04-17 19:28:00 +03:00
|
|
|
return Object.assign(jestConfig, {
|
2023-05-16 00:07:05 +03:00
|
|
|
cacheDirectory: getCacheDirectory('jest'),
|
2023-08-08 20:38:37 +03:00
|
|
|
transform: {
|
|
|
|
...Object.entries(jestConfig.transform).reduce((transform, [key, value]) => {
|
|
|
|
if (value.match(/babel/)) return transform
|
|
|
|
return { ...transform, [key]: value }
|
|
|
|
}, {}),
|
2023-05-16 00:07:05 +03:00
|
|
|
// Transform vanilla-extract using its own transformer.
|
|
|
|
// See https://sandroroth.com/blog/vanilla-extract-cra#jest-transform.
|
|
|
|
'\\.css\\.ts$': '@vanilla-extract/jest-transform',
|
2023-08-08 20:38:37 +03:00
|
|
|
'\\.(t|j)sx?$': '@swc/jest',
|
|
|
|
},
|
2023-08-18 23:26:08 +03:00
|
|
|
// Use d3-arrays's build directly, as jest does not support its exports.
|
|
|
|
transformIgnorePatterns: ['d3-array'],
|
2023-01-10 00:54:48 +03:00
|
|
|
moduleNameMapper: {
|
2023-08-18 23:26:08 +03:00
|
|
|
'd3-array': 'd3-array/dist/d3-array.min.js',
|
2023-01-10 00:54:48 +03:00
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
2022-08-11 02:38:23 +03:00
|
|
|
},
|
|
|
|
webpack: {
|
2023-05-16 00:07:05 +03:00
|
|
|
plugins: [
|
|
|
|
// Webpack 5 does not polyfill node globals, so we do so for those necessary:
|
|
|
|
new ProvidePlugin({
|
|
|
|
// - react-markdown requires process.cwd
|
2023-08-08 20:38:37 +03:00
|
|
|
process: 'process/browser.js',
|
2023-05-16 00:07:05 +03:00
|
|
|
}),
|
2023-08-08 20:38:37 +03:00
|
|
|
new VanillaExtractPlugin(),
|
2023-07-07 00:54:49 +03:00
|
|
|
new RetryChunkLoadPlugin({
|
|
|
|
cacheBust: `function() {
|
|
|
|
return 'cache-bust=' + Date.now();
|
|
|
|
}`,
|
|
|
|
// Retries with exponential backoff (500ms, 1000ms, 2000ms).
|
|
|
|
retryDelay: `function(retryAttempt) {
|
|
|
|
return 2 ** (retryAttempt - 1) * 500;
|
|
|
|
}`,
|
|
|
|
maxRetries: 3,
|
|
|
|
}),
|
2023-05-16 00:07:05 +03:00
|
|
|
],
|
2022-08-11 02:38:23 +03:00
|
|
|
configure: (webpackConfig) => {
|
2023-05-16 00:07:05 +03:00
|
|
|
// Configure webpack plugins:
|
2023-04-21 00:12:43 +03:00
|
|
|
webpackConfig.plugins = webpackConfig.plugins
|
|
|
|
.map((plugin) => {
|
|
|
|
// CSS ordering is mitigated through scoping / naming conventions, so we can ignore order warnings.
|
|
|
|
// See https://webpack.js.org/plugins/mini-css-extract-plugin/#remove-order-warnings.
|
|
|
|
if (plugin instanceof MiniCssExtractPlugin) {
|
|
|
|
plugin.options.ignoreOrder = true
|
|
|
|
}
|
2023-04-13 22:43:47 +03:00
|
|
|
|
2023-05-16 00:07:05 +03:00
|
|
|
// Disable TypeScript's config overwrite, as it interferes with incremental build caching.
|
|
|
|
// This ensures that `yarn start` and `yarn typecheck` share one cache.
|
|
|
|
if (plugin.constructor.name == 'ForkTsCheckerWebpackPlugin') {
|
|
|
|
delete plugin.options.typescript.configOverwrite
|
|
|
|
}
|
|
|
|
|
2023-04-21 00:12:43 +03:00
|
|
|
return plugin
|
|
|
|
})
|
|
|
|
.filter((plugin) => {
|
2023-05-16 00:07:05 +03:00
|
|
|
// Case sensitive paths are already enforced by TypeScript.
|
2023-04-21 00:12:43 +03:00
|
|
|
// See https://www.typescriptlang.org/tsconfig#forceConsistentCasingInFileNames.
|
|
|
|
if (plugin instanceof CaseSensitivePathsPlugin) return false
|
|
|
|
|
|
|
|
// IgnorePlugin is used to tree-shake moment locales, but we do not use moment in this project.
|
|
|
|
if (plugin instanceof IgnorePlugin) return false
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
2023-04-13 22:43:47 +03:00
|
|
|
|
2023-05-16 00:07:05 +03:00
|
|
|
// Configure webpack resolution:
|
|
|
|
webpackConfig.resolve = Object.assign(webpackConfig.resolve, {
|
|
|
|
plugins: webpackConfig.resolve.plugins.map((plugin) => {
|
|
|
|
// Allow vanilla-extract in production builds.
|
|
|
|
// This is necessary because create-react-app guards against external imports.
|
|
|
|
// See https://sandroroth.com/blog/vanilla-extract-cra#production-build.
|
|
|
|
if (plugin instanceof ModuleScopePlugin) {
|
|
|
|
plugin.allowedPaths.push(path.join(__dirname, 'node_modules/@vanilla-extract/webpack-plugin'))
|
|
|
|
}
|
|
|
|
|
|
|
|
return plugin
|
|
|
|
}),
|
|
|
|
// Webpack 5 does not resolve node modules, so we do so for those necessary:
|
|
|
|
fallback: {
|
|
|
|
// - react-markdown requires path
|
|
|
|
path: require.resolve('path-browserify'),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-09-16 05:41:38 +03:00
|
|
|
// Retain source maps for node_modules packages:
|
|
|
|
webpackConfig.module.rules[0] = {
|
|
|
|
...webpackConfig.module.rules[0],
|
|
|
|
exclude: /node_modules/,
|
|
|
|
}
|
|
|
|
|
2023-05-16 00:07:05 +03:00
|
|
|
// Configure webpack transpilation (create-react-app specifies transpilation rules in a oneOf):
|
|
|
|
webpackConfig.module.rules[1].oneOf = webpackConfig.module.rules[1].oneOf.map((rule) => {
|
2023-08-08 20:38:37 +03:00
|
|
|
if (rule.loader && rule.loader.match(/babel-loader/)) {
|
|
|
|
rule.loader = 'swc-loader'
|
|
|
|
delete rule.options
|
2023-05-16 00:07:05 +03:00
|
|
|
}
|
|
|
|
return rule
|
|
|
|
})
|
2022-11-17 21:59:40 +03:00
|
|
|
|
2023-05-02 18:46:32 +03:00
|
|
|
// Configure webpack optimization:
|
2023-05-16 00:07:05 +03:00
|
|
|
webpackConfig.optimization = Object.assign(
|
|
|
|
webpackConfig.optimization,
|
2023-08-08 20:38:37 +03:00
|
|
|
{
|
|
|
|
minimize: isProduction,
|
|
|
|
minimizer: [
|
|
|
|
new TerserPlugin({
|
|
|
|
minify: TerserPlugin.swcMinify,
|
|
|
|
parallel: require('os').cpus().length,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
},
|
2023-05-16 00:07:05 +03:00
|
|
|
isProduction
|
|
|
|
? {
|
|
|
|
splitChunks: {
|
|
|
|
// Cap the chunk size to 5MB.
|
|
|
|
// react-scripts suggests a chunk size under 1MB after gzip, but we can only measure maxSize before gzip.
|
|
|
|
// react-scripts also caps cacheable chunks at 5MB, which gzips to below 1MB, so we cap chunk size there.
|
|
|
|
// See https://github.com/facebook/create-react-app/blob/d960b9e/packages/react-scripts/config/webpack.config.js#L713-L716.
|
|
|
|
maxSize: 5 * 1024 * 1024,
|
|
|
|
// Optimize over all chunks, instead of async chunks (the default), so that initial chunks are also optimized.
|
|
|
|
chunks: 'all',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
: {}
|
|
|
|
)
|
|
|
|
|
2023-08-08 20:38:37 +03:00
|
|
|
// Configure webpack resolution. webpackConfig.cache is unused with swc-loader, but the resolver can still cache:
|
|
|
|
webpackConfig.resolve = Object.assign(webpackConfig.resolve, { unsafeCache: true })
|
2023-05-02 18:46:32 +03:00
|
|
|
|
2022-08-11 02:38:23 +03:00
|
|
|
return webpackConfig
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|