-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
158 lines (130 loc) · 3.67 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
148
149
150
151
152
153
154
155
156
157
158
const DEV = process.env.WEBPACK_DEV_SERVER === 'true';
// Paths to make this work!
const src = `${__dirname}/app`;
const path = `${__dirname}/web/assets/dist`;
const publicPath = '/assets/dist/';
// Format of filenames (without extension)
const filename = DEV ? '[name]' : '[name]-[chunkhash:8]';
const chunkFileName = DEV ? '[id]' : '[id]-[chunkhash:8]';
// Build config object literal
const config = {
target: 'web',
bail: !DEV,
mode: DEV ? 'development' : 'production',
devtool: DEV ? 'cheap-eval-source-map' : 'source-map',
// Which files to start looking at
entry: {
head: `${src}/head.js`,
body: `${src}/body.js`,
blocks: `${src}/blocks.js`,
editor: `${src}/editor.js`,
admin: `${src}/admin.js`,
},
// Where to save build files to disk
output: {
path: `${path}`,
filename: `${filename}.js`,
chunkFilename: `${chunkFileName}.js`,
publicPath: `${publicPath}`,
},
// How to handle different file extensions
module: {
rules: [
// Convert JS
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: [{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react']
},
}],
},
// Convert CSS
{
test: /\.css$/,
exclude: /node_modules/,
use: [
// 3: inject styles (dev), or save to files (production)
((DEV) ? 'style-loader' : require("mini-css-extract-plugin").loader),
// 2. Convert from JS strings to CSS
{
loader: 'css-loader',
options: { importLoaders: 1 }
},
// 1. Convert future CSS to current CSS
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: (loader) => [
require('postcss-import')({ root: loader.resourcePath }),
require('postcss-preset-env')({ stage: 2, features: { 'nesting-rules': true } }),
require('postcss-pxtorem')({ propList: ['*'] }),
],
}
}
},
],
},
// Images
{
test: /\.(png|svg|jpg|jpeg|gif)$/,
use: [
'file-loader',
],
},
],
},
// Make CSS and JS tiny
optimization: {
minimize: !DEV,
minimizer: [
new (require('optimize-css-assets-webpack-plugin'))(),
new (require('terser-webpack-plugin'))()
]
},
// HMR goodness
devServer: {
index: '',
host: '0.0.0.0',
port: 443,
https: true,
disableHostCheck: true,
overlay: true,
contentBase: `${path}`,
contentBasePublicPath: `${publicPath}`,
publicPath: `${publicPath}`,
// writeToDisk: true,
proxy: {
context: () => true,
target: process.env.UPSTREAM,
secure: false,
}
},
plugins: [
// Save CSS files (imported by entry js) to the build directory
new (require("mini-css-extract-plugin"))({
filename: `${filename}.css`,
chunkFilename: `${chunkFileName}.css`,
}),
// Keep build directory tidy without old files
!DEV &&
new (require('clean-webpack-plugin').CleanWebpackPlugin)({
cleanOnceBeforeBuildPatterns: ['**/*', '!*.json'],
}),
// Wordpress can use this json file to know which assets to enqueue
new (require('assets-webpack-plugin'))({
path: `${path}`,
includeAllFileTypes: false,
filename: DEV ? `development.json` : `production.json`,
}),
// Maybe this helps me understand?
DEV &&
new (require('friendly-errors-webpack-plugin'))({
clearConsole: false,
}),
].filter(Boolean),
}
module.exports = config;