forked from ipfs/ipfs-webui
-
Notifications
You must be signed in to change notification settings - Fork 3
/
config-overrides.js
73 lines (64 loc) · 2.18 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
/**
* upgrading to react-scripts v5 with any sort of decent sized app causes all kinds of errors
*
* @see https://github.com/facebook/create-react-app/issues/11756#issuecomment-1184657437
* @see https://alchemy.com/blog/how-to-polyfill-node-core-modules-in-webpack-5
*/
const webpack = require('webpack')
/**
*
* @param {import('webpack').RuleSetRule[]} rules
*/
function modifyBabelLoaderRule (rules, root = true) {
const foundRules = []
rules.forEach((rule, i) => {
if (rule.loader != null) {
if (rule.loader.includes('babel-loader')) {
foundRules.push(rule)
}
} else if (rule.use?.loader != null) {
if (typeof rule.use.loader !== 'string') {
if (rule.use.loader.find(loader => loader.indexOf('babel-loader') >= 0)) {
foundRules.push(rule)
}
} else if (rule.use.loader.indexOf('babel-loader') >= 0) {
foundRules.push(rule)
}
} else if (rule.oneOf) {
const nestedRules = modifyBabelLoaderRule(rule.oneOf, false)
foundRules.push(...nestedRules)
}
})
if (root) {
foundRules.forEach((rule, index) => {
if (rule.include?.indexOf('src') >= 0) {
console.log('Found CRA babel-loader rule for source files. Modifying it to instrument for code coverage.')
console.log('rule: ', rule)
rule.options.plugins.push('istanbul')
}
})
}
return foundRules
}
module.exports = function webpackOverride (config) {
const fallback = config.resolve.fallback || {}
Object.assign(fallback, {
assert: require.resolve('./src/webpack-fallbacks/assert'),
stream: require.resolve('./src/webpack-fallbacks/stream'),
os: require.resolve('./src/webpack-fallbacks/os'),
path: require.resolve('./src/webpack-fallbacks/path')
})
config.resolve.fallback = fallback
config.plugins = (config.plugins || []).concat([
new webpack.ProvidePlugin({
process: 'process/browser',
Buffer: ['buffer', 'Buffer']
})
])
// Instrument for code coverage in development mode
const REACT_APP_ENV = process.env.REACT_APP_ENV ?? process.env.NODE_ENV ?? 'production'
if (REACT_APP_ENV === 'test') {
modifyBabelLoaderRule(config.module.rules)
}
return config
}