-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.prod.ts
More file actions
130 lines (122 loc) · 3 KB
/
webpack.config.prod.ts
File metadata and controls
130 lines (122 loc) · 3 KB
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
import CopyPlugin from 'copy-webpack-plugin';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import glob from 'glob';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import { PurgeCSSPlugin } from 'purgecss-webpack-plugin';
import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin';
import type { Configuration } from 'webpack';
const projectPath = __dirname;
const distPath = `${projectPath}/dist`;
const srcPath = `${projectPath}/src`;
const viewsPath = `${projectPath}/views`;
const scriptsPath = `${srcPath}/scripts`;
const pagesPath = `${srcPath}/pages`;
const entries = glob.sync(`${scriptsPath}/**/*.ts`)
.filter(name => !/\.d\.ts$/.test(name))
.map(name => name.slice(scriptsPath.length + 1, -3));
const pages = glob.sync(`${pagesPath}/**/*.ejs`)
.map(name => name.slice(pagesPath.length + 1, -4));
const htmls = pages.map(name => (
new HtmlWebpackPlugin({
chunks: [name],
publicPath: '/',
base: '',
template: `${pagesPath}/${name}.ejs`,
filename: `${viewsPath}/${name}.ejs`,
})
));
const purgeExcludes = [
/swal2-popup$/,
/toast$/,
/ver-line$/,
];
console.log('\x1B[33mbuilding...\x1B[0m');
const config: Configuration = {
mode: 'production',
devtool: false,
entry: Object.fromEntries(entries.map(name => (
[name, [`${scriptsPath}/${name}.ts`]]
))),
output: {
filename: 'javascripts/[name].js',
path: distPath,
clean: true,
},
optimization: {
splitChunks: {
chunks: 'all',
maxSize: 25000,
},
},
module: {
rules: [
{
test: /\.ts$/,
loader: 'ts-loader',
options: {
transpileOnly: true,
},
},
{
test: /\.ejs$/,
loader: 'html-loader',
},
{
test: /\.scss$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader'],
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader'],
},
{
test: /\.(svg|eot|woff|woff2|ttf|jpg|png|gif)$/,
type: 'asset/resource',
generator: {
filename: 'assets/[hash][ext][query]',
},
},
],
},
plugins: [
...htmls,
new MiniCssExtractPlugin({
filename: 'stylesheets/[name].css',
}),
new PurgeCSSPlugin({
paths: glob.sync(`${srcPath}/**/*`, { nodir: true }),
safelist: {
greedy: purgeExcludes,
},
blocklist: [],
keyframes: true,
}),
new CopyPlugin({
patterns: [
{ from: 'public' },
],
}),
new ForkTsCheckerWebpackPlugin({
logger: {
log: () => {},
error: console.error,
},
typescript: {
configFile: `${srcPath}/tsconfig.json`,
},
}),
],
resolve: {
roots: [srcPath],
modules: [
'node_modules',
srcPath,
],
extensions: ['.ts', '.js'],
plugins: [
new TsconfigPathsPlugin(),
],
},
};
export default config;