Skip to content

Commit 6475e77

Browse files
authored
Switch from babel to swc and sass to sass-embedded (#21)
* Switch to swc and sass-embedded * Exclude all other chunks by default * Add devServerOnly option * Fix resolveAliases for tsc
1 parent 50c392c commit 6475e77

File tree

5 files changed

+963
-473
lines changed

5 files changed

+963
-473
lines changed

config/eslintrc.template.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,15 @@ module.exports = ({ tsconfigRootDir }) => ({
6060
'max-classes-per-file': 'off',
6161
'no-param-reassign': ['warn', { props: true, ignorePropertyModificationsFor: ['state'] }], // Exclude state as required by redux-toolkit: https://redux-toolkit.js.org/usage/immer-reducers#linting-state-mutations
6262
'import/no-extraneous-dependencies': 'off',
63-
// Disable the following 2 lines because to allow webpack file-loaders syntax
64-
'import/no-webpack-loader-syntax': 'off',
65-
'import/no-unresolved': 'off',
63+
'import/no-webpack-loader-syntax': 'off', // Disable to allow webpack file-loaders syntax
64+
'import/no-unresolved': 'off', // Disable to allow webpack file-loaders syntax
6665
'import/prefer-default-export': 'off',
6766
'import/order': 'error',
6867
'prefer-destructuring': ['warn', { object: true, array: false }],
6968
'prefer-promise-reject-errors': 'warn',
7069
'prefer-spread': 'warn',
7170
'@typescript-eslint/ban-ts-comment': 'warn',
72-
'react/destructuring-assignment': 'warn',
71+
'react/destructuring-assignment': 'off',
7372
'react/jsx-props-no-spreading': 'off',
7473
'react/no-unused-class-component-methods': 'warn',
7574
'react/prop-types': 'off',

config/webpack.config.js

Lines changed: 42 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,24 @@ const CopyPlugin = require('copy-webpack-plugin');
1717
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
1818
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
1919
const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent');
20+
const dotenv = require('dotenv');
21+
const dotenvExpand = require('dotenv-expand');
22+
// const { TimeAnalyticsPlugin } = require('time-analytics-webpack-plugin');
23+
24+
// Load the current .env and expand it
25+
const parsedEnv = dotenvExpand.expand(dotenv.config());
2026

2127
// style files regexes
2228
const cssRegex = /\.css$/;
2329
const cssModuleRegex = /\.module\.css$/;
2430
const sassRegex = /\.(scss|sass)$/;
2531
const sassModuleRegex = /\.module\.(scss|sass)$/;
2632

27-
module.exports = (env, argv) => {
33+
module.exports = (webpackEnv, argv) => {
34+
const env = {
35+
...(parsedEnv.parsed || {}),
36+
...(webpackEnv || {}),
37+
};
2838
const { mode } = argv;
2939
const isEnvDevelopment = mode === 'development';
3040
const isEnvProduction = mode === 'production';
@@ -105,6 +115,15 @@ module.exports = (env, argv) => {
105115
* }
106116
*/
107117

118+
try {
119+
// If a visynWebpackOverride.js file exists in the default app, it will be used to override the visyn configuration.
120+
const visynWebpackOverride = require(path.join(defaultAppPath, 'visynWebpackOverride.js'))({ env }) || {};
121+
console.log('Using visynWebpackOverride.js file to override visyn configuration.');
122+
Object.assign(appPkg.visyn, visynWebpackOverride);
123+
} catch (e) {
124+
// ignore if file does not exist
125+
}
126+
108127
let {
109128
// eslint-disable-next-line prefer-const
110129
entries, registry, copyFiles, historyApiFallback,
@@ -255,6 +274,7 @@ module.exports = (env, argv) => {
255274
return loaders;
256275
};
257276

277+
// Use TimeAnalyticsPlugin.wrap to anaylze the build time
258278
return {
259279
mode,
260280
// Webpack noise constrained to errors and warnings
@@ -343,42 +363,11 @@ module.exports = (env, argv) => {
343363
minimizer: [
344364
// This is only used in production mode
345365
new TerserPlugin({
346-
terserOptions: {
347-
parse: {
348-
// We want terser to parse ecma 8 code. However, we don't want it
349-
// to apply any minification steps that turns valid ecma 5 code
350-
// into invalid ecma 5 code. This is why the 'compress' and 'output'
351-
// sections only apply transformations that are ecma 5 safe
352-
// https://github.com/facebook/create-react-app/pull/4234
353-
// ecma: 8, TODO:
354-
},
355-
compress: {
356-
ecma: 5,
357-
// Disabled because of an issue with Uglify breaking seemingly valid code:
358-
// https://github.com/facebook/create-react-app/issues/2376
359-
// Pending further investigation:
360-
// https://github.com/mishoo/UglifyJS2/issues/2011
361-
comparisons: false,
362-
// Disabled because of an issue with Terser breaking valid code:
363-
// https://github.com/facebook/create-react-app/issues/5250
364-
// Pending further investigation:
365-
// https://github.com/terser-js/terser/issues/120
366-
inline: 2,
367-
},
368-
mangle: {
369-
safari10: true,
370-
},
371-
// Added for profiling in devtools
372-
keep_classnames: true,
373-
keep_fnames: false,
374-
output: {
375-
ecma: 5,
376-
comments: false,
377-
// Turned on because emoji and regex is not minified properly using default
378-
// https://github.com/facebook/create-react-app/issues/2488
379-
ascii_only: true,
380-
},
381-
},
366+
// Use swcMinify instead of terser: https://webpack.js.org/plugins/terser-webpack-plugin/#swc
367+
minify: TerserPlugin.swcMinify,
368+
// `terserOptions` options will be passed to `swc` (`@swc/core`)
369+
// Link to options - https://swc.rs/docs/config-js-minify
370+
terserOptions: {},
382371
}),
383372
// This is only used in production mode
384373
// TODO: Somehow this breaks with lineup: /media/LineUpJS.d518227895a66b92cfd7.css:1:1: Unknown word [media/LineUpJS.d518227895a66b92cfd7.css:1,1]
@@ -477,37 +466,19 @@ module.exports = (env, argv) => {
477466
esModule: false,
478467
},
479468
},
480-
// Process application JS with Babel.
481-
// The preset includes JSX, Flow, TypeScript, and some ESnext features.
469+
// Process application JS with swc-loader as it is much faster than babel.
482470
{
483471
test: /\.(js|mjs|jsx|ts|tsx)$/,
484472
exclude: [/node_modules/, customResolveAliasRegex].filter(Boolean),
485-
loader: 'babel-loader',
473+
loader: 'swc-loader',
486474
options: {
487-
presets: [['@babel/preset-env', { targets: { browsers: 'last 2 Chrome versions' } }], '@babel/preset-typescript', '@babel/preset-react'],
488-
plugins: [
489-
// https://exerror.com/babel-referenceerror-regeneratorruntime-is-not-defined/#Solution_3_For_Babel_7_User
490-
[
491-
'@babel/transform-runtime',
492-
{
493-
regenerator: true,
494-
},
495-
],
496-
// plugin-proposal-decorators is only needed if you're using experimental decorators in TypeScript
497-
['@babel/plugin-proposal-decorators', { legacy: true }],
498-
// ["@babel/plugin-proposal-class-properties", { loose: false }],
499-
// Enable hmr for react components in dev mode
500-
isEnvDevelopment && 'react-refresh/babel',
501-
].filter(Boolean),
502-
babelrc: false,
503-
configFile: false,
504-
// This is a feature of `babel-loader` for webpack (not Babel itself).
505-
// It enables caching results in ./node_modules/.cache/babel-loader/
506-
// directory for faster rebuilds.
507-
cacheDirectory: true,
508-
// See create-react-app#6846 for context on why cacheCompression is disabled
509-
cacheCompression: false,
510-
compact: isEnvProduction,
475+
jsc: {
476+
parser: {
477+
syntax: 'typescript',
478+
decorators: true,
479+
// TODO: Check what other settings should be supported: https://swc.rs/docs/configuration/swcrc#compilation
480+
},
481+
},
511482
},
512483
},
513484
// "postcss" loader applies autoprefixer to our CSS.
@@ -640,13 +611,14 @@ module.exports = (env, argv) => {
640611
&& new ReactRefreshWebpackPlugin({
641612
overlay: false,
642613
}),
643-
...Object.values(entries).map(
644-
(entry) => new HtmlWebpackPlugin({
614+
...Object.entries(entries).map(
615+
([chunkName, entry]) => new HtmlWebpackPlugin({
645616
inject: true,
646617
template: path.join(defaultAppPath, entry.template),
647618
filename: entry.html,
648619
title: libName,
649-
excludeChunks: entry.excludeChunks,
620+
// By default, exclude all other chunks
621+
excludeChunks: entry.excludeChunks || Object.keys(entries).filter((entryKey) => entryKey !== chunkName),
650622
meta: {
651623
description: libDesc,
652624
},
@@ -707,8 +679,7 @@ module.exports = (env, argv) => {
707679
syntactic: true,
708680
},
709681
// Build the repo and type-check
710-
build: true,
711-
// Recommended for use with babel-loader
682+
build: Object.keys(resolveAliases).length === 0,
712683
mode: 'write-references',
713684
// Use the corresponding config file of the repo folder
714685
configFile: path.join(workspacePath, repo, 'tsconfig.json'),
@@ -725,7 +696,8 @@ module.exports = (env, argv) => {
725696
incremental: true,
726697
paths: Object.assign(
727698
{},
728-
resolveAliases,
699+
// Map the aliases to the same path, but within an array like tsc requires it
700+
Object.fromEntries(Object.entries(resolveAliases).map(([alias, aliasPath]) => [alias, [aliasPath]])),
729701
...(!isSingleRepoMode
730702
? workspaceRepos.map((r) => ({
731703
[`${workspaceRepoToName[r]}/dist`]: [path.join(workspacePath, r, 'src/*')],

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"@babel/preset-typescript": "^7.6.0",
4242
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.3",
4343
"@svgr/webpack": "^5.5.0",
44+
"@swc/core": "^1.3.23",
4445
"@types/jest": "~27.4.1",
4546
"@types/jquery": "^3.5.5",
4647
"@types/node": "^17.0.24",
@@ -67,7 +68,7 @@
6768
"css-loader": "^6.5.1",
6869
"css-minimizer-webpack-plugin": "^3.2.0",
6970
"dotenv": "^10.0.0",
70-
"dotenv-expand": "^5.1.0",
71+
"dotenv-expand": "^10.0.0",
7172
"dotenv-webpack": "^7.1.0",
7273
"eslint": "^8.3.0",
7374
"eslint-config-airbnb": "^19.0.4",
@@ -118,16 +119,18 @@
118119
"resolve": "^1.20.0",
119120
"resolve-url-loader": "^4.0.0",
120121
"rimraf": "~3.0.2",
121-
"sass": "^1.50.0",
122+
"sass-embedded": "^1.56.2",
122123
"sass-loader": "^12.3.0",
123124
"semver": "^7.3.7",
124125
"semver-intersect": "^1.4.0",
125126
"shx": "~0.3.3",
126127
"source-map-loader": "^3.0.0",
127128
"speed-measure-webpack-plugin": "~1.5.0",
128129
"style-loader": "^3.3.1",
130+
"swc-loader": "^0.2.3",
129131
"tailwindcss": "^3.0.2",
130132
"terser-webpack-plugin": "^5.2.5",
133+
"time-analytics-webpack-plugin": "^0.1.19",
131134
"ts-jest": "~27.1.3",
132135
"ts-node": "^10.7.0",
133136
"tslib": "~2.3.1",

0 commit comments

Comments
 (0)