forked from withspectrum/spectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-overrides.js
156 lines (145 loc) · 5.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
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
/**
* 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 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');
// 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.NODE_ENV === 'development') {
config.output.path = path.join(__dirname, './build');
}
config.plugins.push(
new ReactLoadablePlugin({
filename: './build/react-loadable.json',
})
);
if (process.env.NODE_ENV === 'production') {
removeEslint(config);
}
config = injectBabelPlugin('react-loadable/babel', config);
config = transpileShared(config);
config.plugins.push(
new webpack.optimize.CommonsChunkPlugin({
names: ['bootstrap'],
filename: './static/js/[name].js',
minChunks: Infinity,
})
);
// 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 => {
externals.push(file.replace(/public/, ''));
});
config.plugins.push(
new OfflinePlugin({
// Don't cache anything in dev
caches: process.env.NODE_ENV === 'development' ? {} : 'all',
updateStrategy: 'all', // Update all files on update, seems safer than trying to only update changed files since we didn't write the webpack config
externals, // These files should be cached, but they're not emitted by webpack, so we gotta tell OfflinePlugin about 'em.
excludes: ['**/*.map'], // Don't cache any source maps, they're huge and unnecessary for clients
autoUpdate: true, // Automatically check for updates every hour
rewrites: arg => arg,
cacheMaps: [
{
match: url => {
// Don't return the cached index.html for API requests or /auth pages
if (url.pathname.indexOf('/api') === 0) return;
if (url.pathname.indexOf('/auth') === 0) return;
try {
return new URL('/index.html', url);
// TODO: Fix this properly instead of ignoring errors
} catch (err) {
return;
}
},
requestType: ['navigate'],
},
],
ServiceWorker: {
entry: './public/push-sw.js', // Add the push notification ServiceWorker
events: true, // Emit events from the ServiceWorker
prefetchRequest: {
credentials: 'include', // Include credentials when fetching files, just to make sure we don't get into any issues
},
},
AppCache: false, // Don't cache using AppCache, too buggy that thing
})
);
if (process.env.ANALYZE_BUNDLE === 'true') {
debug('Bundle analyzer enabled');
config.plugins.push(
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
})
);
}
if (process.env.NODE_ENV === 'development') {
config.plugins.push(
WriteFilePlugin({
// Don't match hot-update files
test: /^((?!(hot-update)).)*$/g,
})
);
}
return rewireStyledComponents(config, env, { ssr: true });
};