-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
147 lines (139 loc) · 3.79 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
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const isDev = process.env.NODE_ENV !== 'production';
// Webpack entry points used in both dev and prod
const commonEntries = ['babel-polyfill', './src/client/app/index.js'];
// Extract prebuilt (vendor) CSS into file if needed
const extractCss = new ExtractTextPlugin({
filename: 'vendor.[hash].css'
});
// Extract the Sass output into a CSS file
const extractSass = new ExtractTextPlugin({
filename: 'style.[hash].css',
disable: false,
allChunks: true
});
// Config used by all envs
const commonConfig = {
output: {
filename: 'main.[hash].js',
path: path.resolve(__dirname, 'dist/client'),
publicPath: '/',
},
// When symlinking a node_module package, make imports resolve to symlink
resolve: {
symlinks: false
},
// Enable source maps
devtool: 'source-map',
module: {
rules: [
// Bundle JS using babel; include custom component library if needed; *always* exclude /node_modules
{
test: /\.js$/,
exclude: /node_modules\/(?!custom-component-library)/,
use: {
loader: 'babel-loader'
}
},
// Extract prebuilt CSS as a separate file if needed
{
test: /\.css$/,
include: path.resolve(__dirname, 'src/client/styles/vendor/vendor.css'),
loader: extractCss.extract({
fallback: 'style-loader',
use: ['css-loader']
})
},
// Extract all assets as separate files
{
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
use: ['file-loader']
}
]
},
plugins: [
// Interpolate env vars into bundle if needed
// Adds SOME_ENV_VAR to global namespace
new webpack.DefinePlugin({
SOME_ENV_VAR: JSON.stringify(process.env.SOME_ENV_VAR)
}),
// Generate index html with built bundle paths injected
new HtmlWebPackPlugin({
template: 'src/client/index.html',
filename: 'index.html'
}),
extractCss
]
};
// Dev-only config
const devConfig = {
mode: 'development',
entry: ['webpack-hot-middleware/client', ...commonEntries],
module: {
rules: [
// In dev, keep built Sass modules in the JS bundle to allow HMR for styles
{
test: /\.(scss|sass)$/,
use: [
'style-loader',
'css-loader',
'postcss-loader',
'resolve-url-loader', // resolve url import paths in node_modules package Sass modules
{
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]
}
]
},
plugins: [
// Support HMR
new webpack.HotModuleReplacementPlugin()
]
};
// Prod-only config
const prodConfig = {
mode: 'production',
entry: commonEntries,
module: {
rules: [
// Build Sass modules into a separate css file
{
test: /\.scss$/,
use: extractSass.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
minimize: true
}
},
'postcss-loader',
'resolve-url-loader', // resolve url import paths in node_modules package Sass modules
{
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]
})
}
]
},
plugins: [
// Clean the dist folder on each build
new CleanWebpackPlugin('dist'),
extractSass
]
};
module.exports = isDev ? merge(commonConfig, devConfig) : merge(commonConfig, prodConfig);