-
Notifications
You must be signed in to change notification settings - Fork 3
/
razzle.config.js
169 lines (148 loc) · 4.32 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
const path = require('path');
const fs = require('fs');
const autoprefixer = require('autoprefixer');
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
// const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const PostCssFlexBugFixes = require('postcss-flexbugs-fixes');
const envDefinition = (target) => {
const env = {
__SERVER__: true,
__CLIENT__: true,
__DEVELOPMENT__: true,
__DEVTOOLS__: true,
};
if (target === 'web') {
return Object.assign({}, env, {
__SERVER__: false,
});
}
return Object.assign({}, env, {
__CLIENT__: false,
});
};
// const babelrcGenerator = (target) => {
// const babelrc = fs.readFileSync('./.babelrc');
// let babelrcObject = {};
// try {
// babelrcObject = JSON.parse(babelrc);
// } catch (err) {
// // console.error('==> ERROR: Error parsing your .babelrc.');
// throw err;
// }
// const babelrcObjectTarget =
// (babelrcObject.env && babelrcObject.env[target]) || {};
// // merge global and dev-only plugins
// let combinedPlugins = babelrcObject.plugins || [];
// combinedPlugins = combinedPlugins
// .concat(babelrcObjectTarget.plugins)
// .filter(Boolean);
// const babelLoaderQuery = Object.assign(
// {},
// babelrcObjectTarget,
// babelrcObject,
// { plugins: combinedPlugins }
// );
// delete babelLoaderQuery.env;
// return babelLoaderQuery;
// };
module.exports = {
modify(defaultConfig, { target, dev }, webpack) {
const config = Object.assign({}, defaultConfig);
const isServer = target !== 'web';
config.plugins = [
...config.plugins,
new webpack.DefinePlugin(envDefinition(target)),
];
// config.module.rules[1].use[0].options = {
// babelrc: false,
// cacheDirectory: true,
// ...babelrcGenerator(target),
// };
// console.log(JSON.stringify(config.module.rules[1], null, 2));
const postCssLoader = {
loader: require.resolve('postcss-loader'),
options: {
ident: 'postcss',
plugins: () => [
PostCssFlexBugFixes,
autoprefixer({
browsers: ['>1%', 'last 4 versions', 'Firefox ESR', 'not ie < 9'],
flexbox: 'no-2009',
}),
],
},
};
const sassLoader = {
loader: require.resolve('sass-loader'),
options: {
includePaths: [path.resolve(__dirname, 'node_modules')],
},
};
/* eslint-disable */
config.module.rules = [
...config.module.rules,
{
test: /\.scss$/,
use: isServer
? [require.resolve('css-loader'), sassLoader]
: dev
? [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: { modules: false, sourceMap: true },
},
postCssLoader,
sassLoader,
]
: [
MiniCssExtractPlugin.loader,
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
modules: false,
minimize: true,
},
},
postCssLoader,
sassLoader,
],
},
];
/* eslint-enable */
config.resolve.modules = [...config.resolve.modules, 'src'];
if (!isServer) {
if (!dev) {
config.optimization.minimizer = [
...config.optimization.minimizer,
new OptimizeCSSAssetsPlugin({}),
];
} else {
config.plugins = [
...config.plugins,
// new BundleAnalyzerPlugin(),
];
}
}
if (!dev) {
config.plugins = [
...config.plugins,
new MiniCssExtractPlugin({
filename: `styles/[name].css`,
allChunks: true,
}),
new LodashModuleReplacementPlugin({
collections: true,
shorthands: true,
}),
new webpack.IgnorePlugin(/^\.\/locale$/, [/moment$/]),
];
}
return Object.assign({}, config, {
devtool: dev ? 'eval-source-map' : 'none',
});
},
};