generated from pavle-goloskokovic/webpack5-typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.common.ts
133 lines (127 loc) · 4.04 KB
/
webpack.common.ts
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
import { resolve } from 'path';
import webpack from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import pkg from './package.json';
// boolean indicating if current build is dev or prod
const prod: boolean = module.parent.id.includes('.prod');
const banner = '\nCopyright (c) ' + new Date().getFullYear() + ' ' + pkg.author + '\n';
const htmlminOptions = {
removeComments: true,
collapseWhitespace: true,
conservativeCollapse: true,
minifyJS: {
compress: false,
mangle: false
},
minifyCSS: true
} as HtmlWebpackPlugin.MinifyOptions;
const assetsRule = (
test: webpack.RuleSetRule['test'],
folderName: 'images' | 'fonts' | 'audio'
): webpack.RuleSetRule =>
{
return {
test,
type: 'asset/resource',
generator: {
filename: `assets/${folderName}/[name]${
prod ? '.[contenthash]' : ''
}[ext][query]`
}
};
};
export default <webpack.Configuration>{
entry: {
// defaults to ./src
// Here the application starts executing
// and webpack starts bundling
index: resolve(__dirname, 'src/ts/index.ts') // string | object | array
},
target: ['web', 'es5'],
output: {
// options related to how webpack emits results
filename: `[name]${ prod ? '.[contenthash]' : '' }.js`, // string
// the filename template for entry chunks
// TODO never set path to root as it will delete the whole project on next build
path: resolve(__dirname, 'docs'), // string
// the target directory for all output files
// must be an absolute path (use the Node.js path module)
// publicPath: "/assets/", // string
// the url to the output directory resolved relative to the HTML page
clean: true
},
optimization: {
moduleIds: 'deterministic',
runtimeChunk: false
},
module: {
// configuration regarding modules
rules: [
// rules for modules (configure loaders, parser options, etc.)
{
test: /\.ts$/,
loader: 'ts-loader',
options: {
onlyCompileBundledFiles: true
},
exclude: /node_modules/
},
{
test: /\.ejs$/,
use: {
loader: 'ejs-compiled-loader',
options: {
htmlmin: prod,
htmlminOptions
}
}
},
{
test: /\.css$/i,
use: [
prod ? MiniCssExtractPlugin.loader : 'style-loader',
'css-loader'
]
},
/**
* Fonts
*/
assetsRule(/\.(woff|woff2|eot|ttf|otf)$/i, 'fonts'),
assetsRule(/assets\\fonts\\.+\.xml$/i, 'fonts'),
/**
* Images
*/
assetsRule(/\.(png|svg|jpg|jpeg|gif)$/i, 'images'),
/**
* Audio
*/
assetsRule(/\.(mp3|ogg)$/i, 'audio')
]
},
resolve: {
extensions: ['.ts', '.js'],
// extensions that are used
alias: {}
},
plugins: [
new webpack.BannerPlugin({
banner,
entryOnly: true
}),
new HtmlWebpackPlugin({
template: './src/ejs/index.ejs',
templateParameters: {
banner: `\n${banner.replace('\n', '\n ')}\n`,
title: `${pkg.description} v${pkg.version}`
},
minify: prod ? htmlminOptions : 'auto'
}),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// all options are optional
filename: `style${ prod ? '.[contenthash]' : '' }.css`,
chunkFilename: `style${ prod ? '.[contenthash]' : '' }.css`
})
]
};