-
Notifications
You must be signed in to change notification settings - Fork 7
/
webpack.server.js
160 lines (149 loc) · 3.45 KB
/
webpack.server.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
const autoprefixer = require('autoprefixer');
const path = require('path');
const webpack = require('webpack');
const webpackMerge = require('webpack-merge');
const nodeExternals = require('webpack-node-externals');
const WebpackBarPlugin = require('webpackbar');
const commonWebpackConfig = require('./webpack.common.js');
const NODE_ENV =
process.env.NODE_ENV === 'production' ? 'production' : 'development';
const IS_PRODUCTION = NODE_ENV === 'production';
const include = [path.resolve(__dirname)];
const exclude = [/node_modules/, path.resolve(__dirname, 'public/dist')];
const babelSettings = {
configFile: path.join(__dirname, './.babelrc.js'),
envName: IS_PRODUCTION ? 'web-server-production' : 'web-server-development',
cacheDirectory: !IS_PRODUCTION,
babelrc: false,
};
const stripUselessLoaderOptions = value => value || undefined;
const getStylesLoaders = (enableCSSModules, additionalLoaders = 0) => [
{
loader: 'css-loader/locals',
options: {
modules: enableCSSModules,
importLoaders: 1 + additionalLoaders,
localIdentName: IS_PRODUCTION
? '[local]_[hash:base64:5]'
: '[name]_[local]-[hash:base64:5]',
},
},
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
autoprefixer({
env: NODE_ENV,
flexbox: 'no-2009',
}),
],
sourceMap: stripUselessLoaderOptions(!IS_PRODUCTION),
},
},
];
const rules = [
{
test: /\.(j|t)s$/,
include,
exclude,
use: [
{
loader: 'babel-loader',
options: babelSettings,
},
],
},
{
test: /\.css$/,
include,
exclude,
use: getStylesLoaders(true),
},
{
test: /\.css$/,
include: exclude,
use: getStylesLoaders(false),
},
{
test: /\.scss$/,
include,
exclude,
use: [
...getStylesLoaders(true, 1),
{
loader: 'sass-loader',
options: {
sourceMap: stripUselessLoaderOptions(!IS_PRODUCTION),
},
},
],
},
{
test: /\.(jpe?g|png|svg|gif)$/i,
use: [
{
loader: '@brigad/ideal-image-loader',
options: {
name: 'images/[name].[hash].[ext]',
base64: IS_PRODUCTION,
webp: IS_PRODUCTION ? undefined : false,
warnOnMissingSrcset: !IS_PRODUCTION,
emitFile: false,
},
},
],
},
{
test: /\.(jpe?g|png|svg|gif)$/i,
include: exclude,
use: [
{
loader: 'file-loader',
options: {
name: 'images/[name].[hash].[ext]',
emitFile: false,
},
},
],
},
];
const devPlugins = [
new WebpackBarPlugin({
name: 'server',
color: '#2ab5f9',
compiledIn: false,
}),
];
const prodPlugins = [];
const plugins = [
...(!IS_PRODUCTION ? devPlugins : prodPlugins),
new webpack.LoaderOptionsPlugin({
debug: !IS_PRODUCTION,
minimize: IS_PRODUCTION,
}),
new webpack.DefinePlugin({
__NODE_ENV__: JSON.stringify(NODE_ENV),
}),
];
module.exports = webpackMerge(commonWebpackConfig, {
name: 'server',
target: 'node',
entry: ['./src/entry/js/polyfills', './src/entry/js/server'],
output: {
filename: 'server/[name].js',
sourceMapFilename: 'server/[name].map.js',
libraryTarget: 'commonjs2',
},
module: {
rules,
},
plugins,
externals: [
nodeExternals({
modulesDir: 'node_modules',
whitelist: [/\.css$/],
}),
],
});