-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (62 loc) · 2.16 KB
/
index.js
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
const fs = require('fs');
const path = require('path');
const { paths } = require('react-app-rewired');
const { ESBuildMinifyPlugin } = require('esbuild-loader');
module.exports = rewiredEsbuild;
/**
* replace babel to esbuild
* @link https://github.com/privatenumber/esbuild-loader
* @param ESBuildMinifyOptions
* @param ESBuildLoaderOptions
* @param onlyMinimizer
* @return {function(*, *): *}
*/
function rewiredEsbuild({ ESBuildMinifyOptions, ESBuildLoaderOptions, onlyMinimizer } = {}) {
return function (config) {
const useTypeScript = fs.existsSync(paths.appTsConfig);
if (!onlyMinimizer) {
// replace babel-loader to esbuild-loader
for (const { oneOf } of config.module.rules) {
if (oneOf) {
let babelLoaderIndex = -1;
const rules = Object.entries(oneOf);
for (const [index, rule] of rules.slice().reverse()) {
if (rule.loader && rule.loader.includes(path.sep + 'babel-loader' + path.sep)) {
oneOf.splice(index, 1);
babelLoaderIndex = index;
}
}
if (~babelLoaderIndex) {
oneOf.splice(babelLoaderIndex, 0, {
test: /\.(js|mjs|jsx|ts|tsx)$/,
include: [paths.appSrc],
loader: require.resolve('esbuild-loader'),
options: ESBuildLoaderOptions || {
loader: useTypeScript ? 'tsx' : 'jsx',
target: 'es2015',
},
});
}
}
}
}
// replace minimizer
for (const [index, minimizer] of Object.entries(config.optimization.minimizer)
.slice()
.reverse()) {
const options = ESBuildMinifyOptions || {
target: 'es2015',
css: true,
};
// replace TerserPlugin to ESBuildMinifyPlugin
if (minimizer.constructor.name === 'TerserPlugin') {
config.optimization.minimizer.splice(index, 1, new ESBuildMinifyPlugin(options));
}
// remove OptimizeCssAssetsWebpackPlugin
if (options.css && minimizer.constructor.name === 'OptimizeCssAssetsWebpackPlugin') {
config.optimization.minimizer.splice(index, 1);
}
}
return config;
};
}