-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
webpack.config.mjs
136 lines (130 loc) · 2.76 KB
/
webpack.config.mjs
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
/**
* Imports
*/
import path from 'path';
import { fileURLToPath } from 'url';
import RemoveEmptyScriptsPlugin from 'webpack-remove-empty-scripts';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import LiveReloadPlugin from 'webpack-livereload-plugin';
import CopyPlugin from 'copy-webpack-plugin';
import { ESBuildMinifyPlugin } from 'esbuild-loader';
import { resolveToEsbuildTarget } from 'esbuild-plugin-browserslist';
import browserslist from 'browserslist';
/**
* Settings
*/
const settings = {
entryPoints: {
docs: ['./src/_assets/docs.js']
},
outputPath: './_site/assets',
target: resolveToEsbuildTarget(browserslist(), {
printUnknownTargets: false
})
};
/**
* Setup the config
*/
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
let config = {
entry: settings.entryPoints,
output: {
publicPath: 'auto',
path: path.resolve(__dirname, settings.outputPath),
clean: true,
filename: '[name].js',
assetModuleFilename: '[name].[ext][query]',
chunkFilename: '[name].chunk.js?v=[contenthash]'
},
module: {
parser: {
javascript: {
importMeta: false
}
},
rules: [
{
test: /\.(js|ts)x?$/,
loader: 'esbuild-loader',
// exclude: /(node_modules)/,
resolve: {
// fix an issue with preload-plugin 3.2.*
fullySpecified: false
},
options: {
loader: 'ts',
target: settings.target
}
},
{
test: /\.(scss|css)$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader']
},
{
test: /\.(png|jpg|gif|svg)$/,
type: 'asset/resource'
},
{
test: /.(ttf|otf|eot|woff(2)?)$/,
type: 'asset/resource',
generator: {
filename: '[name][ext]'
}
},
{
test: /\/static\//,
type: 'asset/resource',
generator: {
filename: 'static/[name][ext]'
}
}
]
},
resolve: {
extensionAlias: {
'.js': ['.ts', '.js'],
'.mjs': ['.mts', '.mjs']
},
alias: {
'~': path.resolve(__dirname, './src/_assets/')
}
},
optimization: {
usedExports: true,
minimizer: [
new ESBuildMinifyPlugin({
target: settings.target
})
]
},
plugins: [
new RemoveEmptyScriptsPlugin(),
new MiniCssExtractPlugin({
filename: '[name].css'
}),
new CopyPlugin({
patterns: [{ from: './src/_assets/images', to: 'images' }]
})
]
};
/**
* Adjust the config depending on the --mode
* @see https://webpack.js.org/configuration/mode/
* @param {object} env set via --env flag
* @param {object} argv
* @returns
*/
export default (env, argv) => {
config.mode = argv.mode;
if (argv.mode === 'development') {
config.devtool = 'cheap-source-map';
config.plugins.push(
new LiveReloadPlugin({
useSourceHash: true,
appendScriptTag: true
})
);
}
return config;
};