-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
137 lines (120 loc) · 3.77 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
const path = require('path');
const HtmlBundlerPlugin = require('html-bundler-webpack-plugin');
const data = require("./data.json")
module.exports = (env, argv) => {
const isDev = argv.mode === 'development';
const config = {
mode: isDev ? 'development' : 'production',
devtool: isDev ? 'inline-source-map' : '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/'),
'@js': path.join(__dirname, 'src/assets/js/'),
},
},
plugins: [
new HtmlBundlerPlugin({
// verbose: 'auto', // output information about the process to console in development mode only
entry: {
// define HTML templates here
index: {
import: 'src/views/page/index.html',
data: { ...data }
}, // => dist/index.html
},
js: {
// output filename of extracted JS from source script loaded in HTML via `<script>` tag
filename: 'assets/js/[name].[contenthash:8].js',
},
css: {
// output filename of extracted CSS from source style loaded in HTML via `<link>` tag
filename: 'assets/css/[name].[contenthash:8].css',
},
// you can define all loader options here to avoid explicitly defining the template loader in module rules
loaderOptions: {
// render template with the Nunjucks template engine
preprocessor: 'eta',
// or you can use any template engine as a function like following:
//preprocessor: (content, { data }) => Nunjucks.renderString(content, data),
},
}),
],
module: {
rules: [
// styles
{
test: /\.(css|sass|scss)$/,
use: ['css-loader', 'sass-loader'],
},
// fonts (load from `fonts` or `node_modules` directory only)
{
test: /[\\/]fonts|node_modules[\\/].+(woff(2)?|ttf|otf|eot|svg)$/,
type: 'asset/resource',
generator: {
// group fonts by name
filename: (pathData) => `assets/fonts/${path.basename(path.dirname(pathData.filename))}/[name][ext][query]`,
},
},
// images (load 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: {
hints: false, // don't show the size limit warning when a bundle is bigger than 250 KB
},
};
if (isDev) {
config.devServer = {
//open: true,
compress: true,
static: {
directory: path.join(__dirname, './dist'),
},
// enable HMR
watchFiles: {
paths: ['src/**/*.*'],
options: {
usePolling: true,
},
},
// rewrite rules
historyApiFallback: {
rewrites: [
{ from: /^\/$/, to: '/index.html' },
{ from: /./, to: '/404.html' },
],
},
};
}
return config;
};