forked from withspectrum/spectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-overrides.js
182 lines (170 loc) · 5.61 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Override things of the default create-react-app configuration here
*
* This is using react-app-rewired by @timarney
*/
const debug = require('debug')('build:config-overrides');
const webpack = require('webpack');
const { injectBabelPlugin } = require('react-app-rewired');
const rewireStyledComponents = require('react-app-rewire-styled-components');
const rewireReactHotLoader = require('react-app-rewire-hot-loader');
const swPrecachePlugin = require('sw-precache-webpack-plugin');
const fs = require('fs');
const path = require('path');
const match = require('micromatch');
const WriteFilePlugin = require('write-file-webpack-plugin');
const { ReactLoadablePlugin } = require('react-loadable/webpack');
const OfflinePlugin = require('offline-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const BundleBuddyWebpackPlugin = require('bundle-buddy-webpack-plugin');
const CircularDependencyPlugin = require('circular-dependency-plugin');
// Recursively walk a folder and get all file paths
function walkFolder(currentDirPath, callback) {
fs.readdirSync(currentDirPath).forEach(name => {
// Skip dot files
if (name.indexOf('.') === 0) return;
var filePath = path.join(currentDirPath, name);
var stat = fs.statSync(filePath);
if (stat.isFile()) {
callback(filePath, stat);
} else if (stat.isDirectory()) {
walkFolder(filePath, callback);
}
});
}
const isServiceWorkerPlugin = plugin => plugin instanceof swPrecachePlugin;
const removeEslint = config => {
config.module.rules = config.module.rules.filter(rule => {
// Filter the eslint loader based on its options
if (rule.use) {
return rule.use.every(use => {
if (!use.options) return true;
return !use.options.eslintPath;
});
}
return true;
});
};
// Make sure webpack transpiles files in the shared folder
const transpileShared = config => {
config.module.rules.forEach(rule => {
if (!rule.oneOf) return;
rule.oneOf.forEach(loader => {
if (!loader.include) return;
if (!loader.options) return;
// The loader config we're looking for is for JS files,
// so we look whether the config has a babelrc option
if (!Object.keys(loader.options).includes('babelrc')) return;
// Add the shared folder to the locations
// that should be transpiled
loader.include = [loader.include, path.resolve(__dirname, './shared')];
});
});
return config;
};
module.exports = function override(config, env) {
if (process.env.REACT_APP_MAINTENANCE_MODE === 'enabled') {
console.error('\n\n⚠️ ----MAINTENANCE MODE ENABLED----⚠️\n\n');
}
if (process.env.NODE_ENV === 'development') {
config.output.path = path.join(__dirname, './build');
config = rewireReactHotLoader(config, env);
config.plugins.push(
WriteFilePlugin({
log: true,
useHashIndex: false,
})
);
}
config.plugins.push(
new ReactLoadablePlugin({
filename: './build/react-loadable.json',
})
);
config = injectBabelPlugin('react-loadable/babel', config);
config = transpileShared(config);
// Filter the default serviceworker plugin, add offline plugin instead
config.plugins = config.plugins.filter(
plugin => !isServiceWorkerPlugin(plugin)
);
// Get all public files so they're cached by the SW
let externals = [];
walkFolder('./public/', file => {
if (file.indexOf('index.html') > -1) return;
externals.push(file.replace(/public/, ''));
});
config.plugins.push(
new OfflinePlugin({
// We don't want to cache anything
caches: {},
externals,
autoUpdate: true,
// NOTE(@mxstbr): Normally this is handled by setting
// appShell: './index.html'
// but we don't want to serve the app shell for the `/api` and `/auth` routes
// which means we have to manually do this and filter any of those routes out
cacheMaps: [
{
match: function() {
return false;
},
requestTypes: ['navigate'],
},
],
rewrites: arg => arg,
ServiceWorker: {
entry: './public/push-sw.js',
events: true,
prefetchRequest: {
mode: 'cors',
credentials: 'include',
},
},
AppCache: false,
})
);
if (process.env.ANALYZE_BUNDLE === 'true') {
debug('Bundle analyzer enabled');
config.plugins.push(
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
})
);
}
if (process.env.BUNDLE_BUDDY === 'true') {
config.plugins.push(new BundleBuddyWebpackPlugin());
}
config.plugins.unshift(
new webpack.optimize.CommonsChunkPlugin({
names: ['bootstrap'],
filename: 'static/js/[name].[hash].js',
minChunks: Infinity,
})
);
if (process.env.NODE_ENV === 'production') {
removeEslint(config);
config.plugins.push(new webpack.optimize.ModuleConcatenationPlugin());
config.plugins.push(
new webpack.DefinePlugin({
'process.env': {
SENTRY_DSN_CLIENT: `"${process.env.SENTRY_DSN_CLIENT}"`,
AMPLITUDE_API_KEY: `"${process.env.AMPLITUDE_API_KEY}"`,
},
})
);
}
// NOTE(@mxstbr): This works around an issue where webpack was resolving the "module" path of b2a
// which breaks when imported on the fronted. We need to import the built version.
config.resolve.alias.b2a = path.resolve(
__dirname,
'node_modules/b2a/lib/index.js'
);
config.plugins.push(
new CircularDependencyPlugin({
cwd: process.cwd(),
failOnError: true,
})
);
return rewireStyledComponents(config, env, { ssr: true });
};