-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
185 lines (166 loc) · 4.23 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const path = require('path');
const setPublicPath = require('@microsoft/set-webpack-public-path-plugin');
const webpack = require('webpack');
// Only embed assets under 10 KB
const embedLimit = 10240;
const devServerPort = 8080;
const pathRoot = 'frontend';
const pathDist = `powerwiki/static/powerwiki`;
/*
* Rules which change depending on mode
*/
// Image optimisation rule
const moduleRuleOptimiseImages = {
test: /\.(gif|png|jpe?g|svg)$/iu,
loader: 'image-webpack-loader',
options: {
// Apply the loader before url-loader and svg-url-loader
enforce: 'pre',
// Disabled in dev, enable in production
disable: true,
},
};
// Style building rule
const moduleRuleScss = {
test: /\.s?css$/u,
use: [
// MiniCssExtractPlugin doesn't support HMR, so will replace style-loader
// in production
'style-loader',
'css-loader',
{
loader: 'resolve-url-loader',
options: {
'sourceMap': true,
},
},
{
loader: 'sass-loader',
options: {
sassOptions: {
includePaths: ['./node_modules'],
sourceMap: true,
sourceMapContents: false
},
},
},
],
};
/*
** Main config
*/
const config = {
entry: {
index: [
`./${pathRoot}/index.js`,
`./${pathRoot}/index.scss`,
],
},
output: {
filename: '[name].js',
path: path.resolve(
__dirname,
pathDist,
),
publicPath: '/static/powerwiki/',
},
// Enable sourcemaps for debugging webpack's output.
devtool: 'source-map',
devServer: {
contentBase: path.resolve(__dirname, pathDist),
contentBasePublicPath: '/static/powerwiki/',
publicPath: '/static/powerwiki/',
hot: true,
disableHostCheck: true,
port: devServerPort,
headers: {
'Access-Control-Allow-Origin': '*'
}
},
resolve: {
alias: {
'~': `${__dirname}/${pathRoot}`,
},
extensions: ['.js', '.json'],
},
plugins: [
new CleanWebpackPlugin({
// Remove built js and css from the dist folder before building
cleanOnceBeforeBuildPatterns: ['**/*', '!.gitkeep'],
}),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: '[name].css',
}),
],
module: {
rules: [
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{
test: /\.js$/u,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
},
},
// Optimise images
moduleRuleOptimiseImages,
// Embed small images and fonts
{
test: /\.(png|jpg|gif|eot|ttf|woff|woff2)$/u,
loader: 'url-loader',
options: {
limit: embedLimit,
},
},
{
test: /\.svg$/u,
loader: 'svg-url-loader',
options: {
limit: embedLimit,
noquotes: true,
}
},
// SCSS
moduleRuleScss,
],
},
};
module.exports = (env, argv) => {
if (argv.mode === 'production') {
console.log(`Running in production mode:
* optimising images
* extracting and minifying CSS
`);
// Switch from style-loader to generate separate css files
moduleRuleScss.use[0] = MiniCssExtractPlugin.loader;
// Activate image optimisation
moduleRuleOptimiseImages.options.disable = false;
// Optimise CSS assets
config.plugins.push(new OptimizeCssAssetsPlugin());
} else if (process.argv[1].indexOf('webpack-dev-server') > -1) {
console.log('Running in development mode with HMR');
// Add HMR and change the public path to pick up the correct hostname
config.plugins.push(
new webpack.HotModuleReplacementPlugin(),
new setPublicPath.SetPublicPathPlugin({
scriptName: {
name: '[name].js',
isTokenized: true,
},
}),
);
} else {
console.log(`Running in development mode:
* no optimisation or minification
* single bundle.js
`);
}
return config;
};