This repository has been archived by the owner on Apr 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
213 lines (201 loc) · 5.4 KB
/
webpack.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const path = require('path');
const webpack = require('webpack');
const webpackMerge = require('webpack-merge');
const LoadablePlugin = require('@loadable/webpack-plugin');
const NodeExternals = require('webpack-node-externals');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const SpriteLoaderPlugin = require('svg-sprite-loader/plugin');
const { InjectManifest } = require('workbox-webpack-plugin');
require('dotenv-safe').config({
example: './.example.env',
});
const envVarNameRegExp = /^APP_/i;
const rawEnv = Object.keys(process.env).reduce(
(result, varName) => {
if (envVarNameRegExp.test(varName)) {
return {
...result,
[varName]: process.env[varName],
};
}
return result;
},
{
NODE_ENV: process.env.NODE_ENV || 'production',
APP_VERSION: Date.now(),
}
);
const stringifiedEnv = Object.keys(rawEnv).reduce((result, varName) => {
return {
...result,
[`process.env.${varName}`]: JSON.stringify(rawEnv[varName]),
};
}, {});
const envVars = {
raw: rawEnv,
stringified: stringifiedEnv,
};
console.log({
envVars,
});
const getCommonConfig = (target, mode) => {
return {
bail: true,
name: target,
target,
mode,
resolve: {
extensions: ['.js', '.jsx'],
modules: ['src/common', 'node_modules'],
},
module: {
rules: [
{
test: /\.svg$/,
use: [
{
loader: 'svg-sprite-loader',
options: {
extract: true,
spriteFilename: `sprite.${envVars.raw.APP_VERSION}.svg`,
},
},
{
loader: 'svgo-loader',
options: {
plugins: [
{ removeDimensions: false },
{ removeViewBox: false },
],
},
},
],
include: /icons/,
exclude: /node_modules/,
},
{
test: /\.(svg)$/,
use: [
{
loader: 'file-loader',
options: {
emitFile: target === 'web',
publicPath: '/',
},
},
],
exclude: [/node_modules/, /icons/],
},
{
enforce: 'pre',
test: /\.jsx?$/,
use: 'eslint-loader',
exclude: /node_modules/,
},
{
test: /\.jsx?$/,
use: ['babel-loader', 'stylelint-custom-processor-loader'],
exclude: /node_modules/,
},
],
},
plugins: [
new webpack.DefinePlugin(envVars.stringified),
new webpack.LoaderOptionsPlugin({ options: {} }),
new SpriteLoaderPlugin({
plainSprite: false,
}),
],
stats: 'minimal',
};
};
const getClientConfig = mode => {
const isDev = mode === 'development';
const isProd = mode === 'production';
const isWithHMR = isDev && envVars.raw.APP_HMR === 'true';
const isWithAnalyze = envVars.raw.APP_BUNDLE_ANALYZER_ENABLED === 'true';
return webpackMerge(getCommonConfig('web', mode), {
entry: [
...(isWithHMR
? ['webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000']
: []),
'./src/client',
],
output: {
filename: `[name]${isWithHMR ? '' : '.[contenthash]'}.js`,
chunkFilename: `[id]${isWithHMR ? '' : '.[contenthash]'}.js`,
path: path.resolve(__dirname, 'public'),
publicPath: '/',
},
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: isWithHMR,
},
},
'css-loader',
],
},
],
},
plugins: [
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [path.resolve(__dirname, 'public')],
}),
new CopyWebpackPlugin([
{
from: 'src/common/static',
},
]),
new LoadablePlugin(),
new MiniCssExtractPlugin({
filename: `main.${rawEnv.APP_VERSION}.css`,
}),
new InjectManifest({
swSrc: './src/client/serviceWorker.js',
}),
...(isProd ? [new OptimizeCSSAssetsPlugin({})] : []),
...(isWithHMR ? [new webpack.HotModuleReplacementPlugin()] : []),
...(isWithAnalyze ? [new BundleAnalyzerPlugin()] : []),
],
devtool: isDev && 'source-maps',
});
};
const getServerConfig = mode =>
webpackMerge(getCommonConfig('node', mode), {
entry: './src/server',
output: {
libraryTarget: 'umd',
filename: 'index.js',
chunkFilename: '[name].[contenthash].js',
path: path.resolve(__dirname, 'functions/getSSRApp'),
publicPath: '/',
},
externals: [new NodeExternals()],
plugins: [
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [
path.resolve(__dirname, 'functions/getSSRApp'),
],
}),
new CopyWebpackPlugin([
{
from: 'src/server/views',
to: 'views',
},
]),
],
});
module.exports = (env, { mode = 'production' } = {}) => [
getClientConfig(mode),
getServerConfig(mode),
];