-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.babel.js
180 lines (162 loc) · 5.51 KB
/
webpack.config.babel.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
import path from 'path';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
const isProd = process.env.NODE_ENV === 'production';
export default (function makeWebpackConfig() {
const config = {};
/**
* Configuration begins here
* Reference: https://webpack.js.org/configuration/
*/
// Reference: https://webpack.js.org/concepts/mode/
config.mode = 'none';
// Cache generated modules and chunks to improve performance for multiple incremental builds.
// Reference: https://webpack.js.org/configuration/other-options/#cache
config.cache = true;
// Mapping entry point
// More: https://github.com/webpack/webpack/issues/1189
config.entry = {
// This will map entry point to destination path related to output.path
'dist/app': './src/app/app.js'
};
// Options affecting the output of the compilation.
// Reference: http://webpack.github.io/docs/configuration.html#output
// Additional notes:
// http://webpack.github.io/docs/long-term-caching.html
// https://medium.com/@okonetchnikov/long-term-caching-of-static-assets-with-webpack-1ecb139adb95
config.output = {
path: path.resolve(__dirname, 'dist'),
publicPath: './',
filename: isProd ? '[name].[chunkhash].js' : '[name].bundle.js',
// This is probably a bug, since chunks doesn't respect entry mapping in config
chunkFilename: isProd
? 'dist/[name].[chunkhash].js'
: 'dist/[name].bundle.js'
};
// Choose a developer tool to enhance debugging.
// Reference: https://webpack.js.org/configuration/devtool/#devtool
config.devtool = 'source-map';
/**
* Loaders definition
* Reference: http://webpack.github.io/docs/configuration.html#module-loaders
* List: http://webpack.github.io/docs/list-of-loaders.html
* This handles most of the magic responsible for converting modules
*/
config.module = {
rules: [
{
// JS LOADER
// Reference: https://github.com/babel/babel-loader
// Transpile .js files using babel-loader (compile ES6 and ES7 into ES5 code)
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
// By default Babel is injecting helpers into each file that requires it.
// Require the babel runtime as a separate module to avoid the duplication.
// Reference: https://github.com/babel/babel-loader#babel-is-injecting-helpers-into-each-file-and-bloating-my-code
plugins: ['@babel/plugin-transform-runtime']
}
}
},
{
test: /\.s?css$/,
use: [
// MiniCssExtractPlugin
// https://github.com/webpack-contrib/mini-css-extract-plugin
// STYLE LOADER
// Reference: https://github.com/webpack-contrib/style-loader
isProd ? MiniCssExtractPlugin.loader : 'style-loader',
// CSS LOADER
// Reference: https://github.com/webpack-contrib/css-loader
{
loader: 'css-loader',
options: {
importLoaders: 1
// Enable CSS minification, this option has no connection to config.devtool
// Reference: https://github.com/webpack/webpack/issues/189
// minimize: true,
// sourceMap: true
}
},
// SASS LOADER
// Reference: https://github.com/webpack-contrib/sass-loader
{
loader: 'sass-loader'
},
// POSTCSS LOADER
// Reference: https://github.com/postcss/postcss-loader
{
loader: 'postcss-loader'
}
]
},
{
// HTML LOADER
// Reference: https://github.com/webpack/raw-loader
// Allow loading html through js
test: /\.html$/,
exclude: /node_modules/,
loader: 'raw-loader'
}
]
};
/**
* Add additional plugins to the compiler
* Reference: https://webpack.js.org/configuration/plugins/
* List: https://webpack.js.org/plugins/
* More: https://github.com/webpack-contrib/awesome-webpack#webpack-plugins
*/
config.plugins = [];
// HtmlWebpackPlugin
// Injects bundles in your main file instead of wiring all manually.
// Reference: https://github.com/jantimon/html-webpack-plugin
config.plugins.push(
new HtmlWebpackPlugin({
filename: 'index.html',
inject: 'head'
})
);
// MiniCssExtractPlugin
// This plugin extracts CSS into separate files.
// It creates a CSS file per JS file which contains CSS. It supports On-Demand-Loading of CSS and SourceMaps.
// Reference: https://github.com/webpack-contrib/mini-css-extract-plugin
config.plugins.push(
new MiniCssExtractPlugin({
filename: isProd ? '[name].[hash].css' : '[name].css',
chunkFilename: isProd ? '[id].[hash].css' : '[id].css'
})
);
// Automatically move all modules defined outside of application directory to vendor bundle.
// If you are using more complicated project structure, consider to specify common chunks manually.
// Reference: https://webpack.js.org/plugins/split-chunks-plugin/
// Migration questions: https://stackoverflow.com/questions/49017682/webpack-4-migration-commonschunkplugin
config.optimization = {
splitChunks: {
cacheGroups: {
vendor: {
name: 'vendor',
chunks: (chunk) => chunk.name == 'dist/app',
reuseExistingChunk: true,
priority: 1,
test: (module) => /[\\/]node_modules[\\/]/.test(module.context),
minChunks: 1,
minSize: 0
}
}
}
};
if (isProd) {
// Reference: https://webpack.js.org/configuration/optimization/#optimization-minimize
config.optimization.minimize = true;
}
// Dev server configuration
// Reference: https://webpack.js.org/configuration/dev-server/#devserver
config.devServer = {
contentBase: config.output.path,
compress: true,
port: 3000
};
return config;
})();