generated from ajzamora/webpack-and-chill
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.config.js
197 lines (185 loc) · 5.8 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
186
187
188
189
190
191
192
193
194
195
196
197
/* -----------------------------------------------
* webpack-and-chill v1.0.0
* (c) 2019-present Alden Zamora
* Released under MIT license[http://opensource.org/licenses/MIT]
* =============================================== */
/************************************************
-- WEBPACK CONFIG JS: variables / package imports
************************************************/
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const OptimizeCssAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const {CleanWebpackPlugin} = require("clean-webpack-plugin");
/************************************************
-- 1. ADD YOUR PAGES in commonPages; 2. (optional) : edit arrPages/path in entryJS and entryPug if needed
************************************************/
const commonPages = ['index'];
const entryJS = {
arrPages: commonPages, // Add more jsEntries: const arrPages = [...commonPages, addJSfiles]; or use ES6 .filter() to exclude
path: './src/js/',
config: function() {
let configEntry = this.arrPages.map((page) =>
({[page]: `${this.path}${page}.js`})
);
return Object.assign({}, ...configEntry);
}
};
const entryPug = {
arrPages: commonPages, // Add more pugEntries: const arrPages = [...commonPages, addPugfiles]; or use ES6 .filter() to exclude
path: `./src/html/`,
config: function(mode) {
const isProd = mode == "production";
return this.arrPages.map((page) =>
new HtmlWebpackPlugin({
filename: isProd ? `${page}.html` : `html/${page}.html`,
template: `${this.path}${page}.pug`,
inject: true,
chunks: [`${page}`],
minify: {
removeAttributeQuotes: isProd,
collapseWhitespace: isProd,
removeComments: isProd,
},
})
)
}
}
/************************************************
-- configured rules variables
************************************************/
const rules = {
pugLoader(mode) {
return {
test: /\.pug$/,
loader: 'pug-loader',
options: {
pretty: mode=="development",
attrs: ['img:src', 'link:href'],
},
}
},
fileLoaderImage(mode) {
return {
test: /\.(png|jpe?g|gif|svg)$/i,
use: [
{
loader: 'file-loader',
options: {
name: mode=="production" ? `[name].[contenthash:11].[ext]` : `[name].[ext]` ,
publicPath: '../images',
outputPath: 'images',
},
},
],
};
},
fileLoaderFont(mode) {
return {
test: /\.(woff(2)?|ttf|eot|otf)(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'file-loader',
options: {
name: mode=="production" ? `[name].[contenthash:11].[ext]` : `[name].[ext]`,
outputPath: '../fonts',
publicPath: '../',
},
},
],
};
},
babelLoader() {
return {
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-proposal-object-rest-spread'],
},
},
}
},
CSSloaders() {
return {
test: /\.s[ac]ss$/i,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: (resourcePath, context) => {
// publicPath is the relative path of the resource to the context
// e.g. for ./css/admin/main.css the publicPath will be ../../
// while for ./css/main.css the publicPath will be ../
return path.relative(path.dirname(resourcePath), context) + '../../';
},
}
}, //3. Extract css into files
// "style-loader", //3. Inject styles into DOM
"css-loader", //2. Turns css into commonjs
"sass-loader" //1. Turns sass into css
],
}
},
};
/************************************************
-- DEVELOPMENT & PRODUCTION common config
************************************************/
let config = {
devtool: "none", // disables eval that webpack uses in js files (optional)
context: __dirname,
entry: entryJS.config(),
module: {
rules: [
rules.babelLoader(), // Babel: returns rules for babel
rules.CSSloaders(), // MiniCssExtractPlugin: uses MiniCssExtractPlugin.loader, css-loader, sass-loader
],
},
};
module.exports = (env, argv) => {
/************************************************
-- MODE-based (production/development): dynamic config
************************************************/
config.mode = argv.mode;
config.module.rules.push(
rules.pugLoader(config.mode),
rules.fileLoaderImage(config.mode),
rules.fileLoaderFont(config.mode)
);
const isProd = config.mode=="production";
config.plugins = [
new MiniCssExtractPlugin({ filename: isProd ? "css/[name].[contentHash:11].css" : "css/[name].css" }),
...entryPug.config(config.mode),
new CleanWebpackPlugin(),
];
config.output = {
path: path.resolve(__dirname, isProd ? "docs" : "dev"),
filename: isProd ? "js/[name].[contentHash:11].js" : "js/[name].js",
chunkFilename: 'js/[id].bundle.js',
publicPath: isProd ? '' : '../',
};
/************************************************
-- DEVELOPMENT-specific config
************************************************/
if (argv.mode === 'development') {
config.optimization = {
splitChunks: {chunks: 'async'},
};
}
/************************************************
-- PRODUCTION-specific config
************************************************/
else if (argv.mode === 'production') {
config.optimization = {
splitChunks: {chunks: 'async'},
minimizer: [
new OptimizeCssAssetsPlugin(),
new TerserPlugin(),
],
};
}
return config;
};