This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
config-overrides.js
137 lines (129 loc) · 4.14 KB
/
config-overrides.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
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
const { addBabelPlugin, override, overrideDevServer, useBabelRc, watchAll, disableChunk } = require('customize-cra')
const webpack = require('webpack')
// const UserScript = require('./user-script')
const TerserPlugin = require('terser-webpack-plugin')
const minimizer = (minJsOnly = false) => (config) => {
if (process.env.NODE_ENV === 'production') {
Object.assign(config.optimization, {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: false,
// extractComments: {
// condition: /eslint-disable/i,
// banner: () => {
// return UserScript
// },
// },
terserOptions: {
ecma: 6,
compress: true,
output: {
comments: false,
beautify: false,
},
},
}),
],
})
// minimize only the .min.js files
if (minJsOnly) {
for (const plugin of config.optimization.minimizer) {
if (!plugin || !plugin.constructor) {
// do nothing if the plugin is null
continue
}
if (plugin.constructor.name === 'TerserPlugin') {
Object.assign(plugin.options, { include: /\.min\.js$/ })
}
if (plugin.constructor.name === 'OptimizeCssAssetsWebpackPlugin') {
Object.assign(plugin.options, { assetNameRegExp: /\.min\.css$/ })
}
}
// add user-script banner for Tampermonkey
config.plugins.push(
new webpack.BannerPlugin({
banner: UserScript,
raw: true,
entryOnly: true,
})
)
}
}
return config
}
const preventJsHash = () => (config) => {
Object.assign(config.output, { filename: 'static/js/[name].js', globalObject: 'self' })
return config
}
const preventCssHash = () => (config) => {
// prevent hashes for the CSS files
// search for the "MiniCssExtractPlugin" so we can remove the hashing in the filename
for (const plugin of config.plugins) {
if (!plugin || !plugin.constructor) {
// do nothing if the plugin is null
continue
}
if (plugin.constructor.name === 'MiniCssExtractPlugin') {
Object.assign(plugin.options, {
filename: 'static/css/[name].css',
})
delete plugin.options.chunkFilename
}
}
return config
}
const disableHotModuleReloading = () => (config) => {
// disable hot module reloading because Greasemonkey cannot handle it
// Delete any entries to the "HotDev" client
if (process.env.NODE_ENV === 'production') {
config.entry = config.entry.filter((x) => !x.toLowerCase().includes('hotdev'))
config.plugins = config.plugins.filter((x) => !x || x.constructor.name !== 'HotModuleReplacementPlugin')
}
return config
}
const cssInjector = () => (config) => {
// Even in production mode, we want the CSS inlined instead of put in a different file
// Remove the CSS extract plugin because we want CSS injected directly in the greasemonkey script
config.plugins = config.plugins.filter((x) => !x || x.constructor.name !== 'MiniCssExtractPlugin')
;(config.module.rules.find((x) => !!x.oneOf).oneOf || []).forEach((x) => {
if (x.test && x.test.constructor === RegExp && 'test.css'.match(x.test)) {
try {
x.use = x.use.filter((y) => !y.loader.includes('css-extract'))
x.use.unshift(require.resolve('style-loader'))
} catch (e) {
// If we fail to replace a `css-extract` move on silently
// This will happen if, for example, it has already been replaced
}
}
})
return config
}
module.exports = {
webpack: override(
addBabelPlugin([
'babel-plugin-styled-components',
{
displayName: true,
ssr: false,
fileName: false,
pure: true,
// minify: false,
// transpileTemplateLiterals: false,
// any extra config from babel-plugin-styled-components
},
]),
useBabelRc(),
disableChunk(),
preventJsHash(),
preventCssHash(),
minimizer(false),
// useEslintRc(),
disableHotModuleReloading(),
cssInjector(),
(config) => {
return config
}
),
devServer: overrideDevServer(watchAll()),
}