-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.config.js
146 lines (128 loc) · 4.22 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
const path = require('path');
const HtmlBundlerPlugin = require('html-bundler-webpack-plugin');
const isProd = !process.argv.find((str) => str.includes('development'));
module.exports = {
mode: isProd ? 'production' : 'development',
devtool: isProd ? 'source-map' : 'inline-source-map',
stats: 'minimal',
output: {
path: path.join(__dirname, 'dist'),
clean: true,
},
resolve: {
// use aliases used in sources instead of relative paths like ../../
alias: {
'@views': path.join(__dirname, 'src/views/'),
'@images': path.join(__dirname, 'src/assets/images/'),
'@fonts': path.join(__dirname, 'src/assets/fonts/'),
'@styles': path.join(__dirname, 'src/assets/styles/'),
'@scripts': path.join(__dirname, 'src/assets/scripts/'),
},
},
plugins: [
new HtmlBundlerPlugin({
// verbose: 'auto', // output information about the process to console in development mode only
entry: {
// define HTML templates here
// define the simple template
index: 'src/views/pages/home/index.html', // => dist/index.html
// define the template with passed external variables
demo: {
import: 'src/views/pages/demo/index.html', // => dist/demo.html
// pass data into template as an object
data: {
myVar: 'The string passed as the `myVar` variable from Webpack configuration.',
},
// -OR- define here a relative path to the JS/JSON file with data used in the template
// data: './src/data/demo.json'
},
about: 'src/views/pages/about/index.html', // => dist/about.html
404: './src/views/pages/404/index.html', // => dist/404.html
},
js: {
// output filename of compiled JavaScript, used if `inline` option is false (defaults)
filename: 'assets/js/[name].[contenthash:8].js',
// inline: true; // inlines JS into HTML
},
css: {
// output filename of extracted CSS, used if `inline` option is false (defaults)
filename: 'assets/css/[name].[contenthash:8].css',
// inline: true; // inlines CSS into HTML
},
// supports template engines: eta, ejs, handlebars, nunjucks, twig
preprocessor: 'nunjucks', // use the Nunjucks template engine
}),
],
module: {
rules: [
// load styles
{
test: /\.(css|sass|scss)$/,
use: ['css-loader', 'sass-loader'],
},
// load fonts from `fonts` or `node_modules` directory only
{
test: /[\\/]fonts|node_modules[\\/].+(woff(2)?|ttf|otf|eot|svg)$/,
type: 'asset/resource',
generator: {
// keep original directory structure
filename: ({ filename }) => {
const srcPath = 'src/assets/fonts';
const regExp = new RegExp(`[\\\\/]?(?:${path.normalize(srcPath)}|node_modules)[\\\\/](.+?)$`);
const assetPath = path.dirname(regExp.exec(filename)[1].replace('@', '').replace(/\\/g, '/'));
return `assets/fonts/${assetPath}/[name][ext][query]`;
},
},
},
// load images from `images` directory only
{
test: /[\\/]images[\\/].+(png|jpe?g|svg|webp|ico)$/,
oneOf: [
// inline image using `?inline` query
{
resourceQuery: /inline/,
type: 'asset/inline',
},
// auto inline by image size
{
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 1024,
},
},
generator: {
filename: 'assets/img/[name].[hash:8][ext]',
},
},
],
},
],
},
performance: {
// don't show the size limit warning when a bundle is bigger than 250 KB
hints: false,
},
devServer: {
// open browser
open: true,
compress: true,
static: {
directory: path.join(__dirname, './dist'),
},
// enable live reload
watchFiles: {
paths: ['src/**/*.*'],
options: {
usePolling: true,
},
},
// rewrite rules
historyApiFallback: {
rewrites: [
{ from: /^\/$/, to: '/index.html' },
{ from: /./, to: '/404.html' },
],
},
},
};