-
Notifications
You must be signed in to change notification settings - Fork 1
/
razzle.config.js
195 lines (173 loc) · 5.73 KB
/
razzle.config.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
183
184
185
186
187
188
189
190
191
192
193
194
195
const paths = require('razzle/config/paths');
const makeLoaderFinder = require('razzle-dev-utils/makeLoaderFinder');
const { ReactLoadablePlugin } = require('react-loadable/webpack');
const logger = require('razzle-dev-utils/logger');
const CircularDependencyPlugin = require('circular-dependency-plugin');
const getBabelConfig = require('./internals/babel');
const packageJson = require('./package.json');
module.exports = {
modify: (config, { target, dev }, webpack) => {
let newConfig = config;
if (!dev) {
// razzle do not set public path in node environment
// https://github.com/jaredpalmer/razzle/issues/654
newConfig.output.publicPath = process.env.PUBLIC_PATH;
// disable source maps for now
newConfig.devtool = undefined;
}
// TODO: fix source maps support
if (!newConfig.node) {
newConfig.node = {};
}
// https://github.com/webpack-contrib/css-loader/issues/447
newConfig.node = {
...newConfig.node,
fs: 'empty',
};
// Add appSrc directory to modules
// NOTE: it enables global imports for modules in `appSrc`
newConfig.resolve.modules.unshift(paths.appSrc);
// do something to config
const fileLoader = newConfig.module.rules.find(makeLoaderFinder('file-loader'));
fileLoader.exclude.push(/\.svg$/);
const babelLoader = newConfig.module.rules.find(makeLoaderFinder('babel-loader'));
babelLoader.use[0].options.presets = getBabelConfig(target, dev);
newConfig = {
...newConfig,
module: {
...newConfig.module,
rules: [
babelLoader,
fileLoader,
newConfig.module.rules.find(makeLoaderFinder('css-loader')),
{
test: /\.svg$/,
exclude: [/graphql-mocks/, /images\/svg/],
use: [{
loader: '@svgr/webpack',
options: {
icon: true,
},
}],
},
{
test: /\.svg$/,
include: [/images\/svg/],
use: [{
loader: 'file-loader',
options: {
name: 'static/media/[name].[hash:8].[ext]',
emitFile: true,
},
}],
},
],
},
};
// Create urlLoader
const urlLoader = {
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
use: [
{
loader: require.resolve('url-loader'),
options: {
limit: 10000,
name: 'static/media/[name].[hash:8].[ext]',
emitFile: true,
},
},
],
};
if (!dev) {
urlLoader.use.push({
loader: require.resolve('image-webpack-loader'),
options: {
mozjpeg: {
progressive: true,
},
gifsicle: {
interlaced: false,
},
optipng: {
optimizationLevel: 7,
},
pngquant: {
quality: '65-90',
speed: 4,
},
},
});
}
// Add urlLoader to config
newConfig.module.rules.push(urlLoader);
// Compilation time environments
// WARN: we do not use any razzle environment variables here
const envVariables = {
'process.env.IS_SERVER': JSON.stringify(target === 'node'),
'process.env.IS_BROWSER': JSON.stringify(target === 'web'),
'process.env.IS_DEV': JSON.stringify(dev),
'process.env.IS_PRODUCTION': JSON.stringify(!dev),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'),
'process.env.VERSION': JSON.stringify(packageJson.version),
};
if (dev) {
// Required by dev server
envVariables['process.env.HOST'] = JSON.stringify(process.env.HOST);
envVariables['process.env.PORT'] = JSON.stringify(process.env.PORT);
}
// Remove PORT from DefinePlugin definitions
const definePluginIndex = newConfig.plugins.findIndex((plugin) => plugin.constructor.name === 'DefinePlugin');
if (definePluginIndex >= 0) {
// Override define plugin to remove some variables
newConfig.plugins[definePluginIndex] = new webpack.DefinePlugin(envVariables);
} else {
newConfig.plugins.push(new webpack.DefinePlugin(envVariables));
}
newConfig.plugins.push(new webpack.ProvidePlugin({
// make fetch available
fetch: 'exports-loader?self.fetch!whatwg-fetch',
}));
if (target === 'web') {
if (dev) {
// Html-webpack-plugin do not work with HMR plugin with multiStep true option
// https://github.com/jantimon/html-webpack-plugin/issues/716
// https://github.com/webpack/webpack/issues/6693
// Also with multiStep true server HOC is failing some times
// find the HMR plugin
const HMRPluginIndex = newConfig.plugins.findIndex(
(plugin) => plugin.constructor.name === 'HotModuleReplacementPlugin',
);
if (HMRPluginIndex >= 0) {
newConfig.plugins[HMRPluginIndex] = new webpack.HotModuleReplacementPlugin({
multiStep: false,
});
}
}
newConfig.plugins = [
...newConfig.plugins,
new ReactLoadablePlugin({
filename: './build/react-loadable.json',
}),
];
newConfig.optimization.splitChunks = {
chunks: 'async',
minSize: 30000,
minChunks: 1,
maxAsyncRequests: 2,
maxInitialRequests: 3,
name: true,
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks: 'all',
priority: -10,
},
},
};
}
newConfig.plugins.push(new CircularDependencyPlugin());
logger.debug(`Webpack config for ${target}`, newConfig);
return newConfig;
},
};