-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
130 lines (115 loc) · 3.16 KB
/
webpack.config.js
File metadata and controls
130 lines (115 loc) · 3.16 KB
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
// webpack.config.js
const currentTask = process.env.npm_lifecycle_event; // 'dev', 'serve', or 'build'
const isDev = currentTask === 'dev' || currentTask === 'serve';
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const fse = require('fs-extra');
const postCSSPlugins = [
require('postcss-import'),
require('postcss-mixins'),
require('postcss-simple-vars'),
require('postcss-nested'),
require('autoprefixer'),
];
class RunAfterCompile {
apply(compiler) {
compiler.hooks.done.tap('Copy Images', function () {
// Copy images used by the site into the production output
fse.copySync('./app/assets/images', './docs/assets/images');
});
}
}
// Base CSS rule; we’ll prepend the right loader per mode below
const cssRule = {
test: /\.css$/i,
use: [
// dev: 'style-loader' | prod: MiniCssExtractPlugin.loader (added below)
'css-loader?url=false',
{
loader: 'postcss-loader',
options: { postcssOptions: { plugins: postCSSPlugins } },
},
],
};
// Auto-generate an HtmlWebpackPlugin for each .html file in ./app
const pages = fse
.readdirSync('./app')
.filter((file) => file.endsWith('.html'))
.map(
(page) =>
new HtmlWebpackPlugin({
filename: page,
template: `./app/${page}`,
})
);
const config = {
entry: './app/assets/scripts/App.js',
mode: isDev ? 'development' : 'production',
plugins: [...pages],
module: {
rules: [cssRule],
},
// Helpful source maps in dev; smaller output in prod
devtool: isDev ? 'eval-source-map' : 'source-map',
};
if (isDev) {
// Inject CSS via <style> for fast HMR
cssRule.use.unshift('style-loader');
config.output = {
filename: 'bundled.js',
path: path.resolve(__dirname, 'app'),
};
config.devServer = {
static: {
directory: path.join(__dirname, 'app'),
},
hot: true,
compress: true,
port: 8080,
open: true,
watchFiles: ['app/**/*'],
};
}
if (currentTask === 'build') {
// Extract CSS to a file in production
cssRule.use.unshift(MiniCssExtractPlugin.loader);
// Transpile modern JS for broader browser support in prod
config.module.rules.push({
test: /\.js$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
},
});
config.output = {
filename: '[name].[contenthash:8].js',
path: path.resolve(__dirname, 'docs'),
clean: true, // clean output dir between builds
};
config.optimization = {
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
enforce: true,
chunks: 'all',
},
},
},
minimize: true,
minimizer: ['...', new CssMinimizerPlugin()],
};
config.plugins.push(
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({ filename: 'styles.[contenthash:8].css' }),
new RunAfterCompile()
);
}
module.exports = config;