-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.config.js
102 lines (90 loc) · 2.29 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
const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const FixStyleOnlyEntriesPlugin = require('webpack-fix-style-only-entries');
// see https://webpack.js.org/plugins/mini-css-extract-plugin/#extracting-css-based-on-entry
const recursiveIssuer = (m, c) => {
const issuer = m.issuer;
if (issuer) {
return recursiveIssuer(issuer, c);
}
const chunks = m._chunks;
for (const chunk of chunks) {
return chunk.name;
}
return false;
}
// CSS files to load into assets/css
const cssFiles = [
{
name : 'editor',
path : './blocks/book-details/editor.css'
},
{
name: 'style',
path: './blocks/book-details/style.css'
}
]
const entryPoints = {
// default entry point (main JS file)
'editor.blocks': './blocks/index.js'
}
// add cssFiles as entry points
cssFiles.map(cssFile => entryPoints[cssFile.name] = cssFile.path)
// set up empty cache groups object
const cacheGroups = {}
// add cssFiles to cache groups
cssFiles.map(cssFile => cacheGroups[cssFile.name] = {
name: cssFile.name,
test: (m,c,entry = cssFile.name) => m.constructor.name === 'CssModule' && recursiveIssuer(m) === entry,
chunks: 'all',
enforce: true
})
module.exports = {
entry: entryPoints,
output: {
path: path.resolve(__dirname),
filename: './assets/js/[name].js'
},
watch: 'production' !== process.env.NODE_ENV,
devtool: 'cheap-eval-source-map',
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.css$/,
use: [
{ loader: MiniCssExtractPlugin.loader },
'css-loader',
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [ require('autoprefixer') ]
}
}
}
]
}
]
},
optimization: {
splitChunks: {
cacheGroups: cacheGroups
}
},
plugins: [
// remove erroneous JS files generated from CSS entry points
new FixStyleOnlyEntriesPlugin(),
// place extracted CSS files in assets/css directory
new MiniCssExtractPlugin({
filename: './assets/css/blocks.[name].css'
})
]
};