-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebpack.common.js
99 lines (97 loc) · 2.86 KB
/
webpack.common.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
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const dirNode = 'node_modules'
const dirSrc = path.join(__dirname, 'src')
const dirAssets = path.join(__dirname, 'src/assets')
module.exports = () => {
return {
entry: {
app: path.join(dirSrc, 'app'),
},
resolve: {
modules: [dirNode, dirSrc],
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(dirSrc, 'index.html'),
minify: false,
// https://stackoverflow.com/questions/74928998/how-do-i-reference-the-hashed-image-in-the-html-page-in-the-dist-folder-after
// usage: <%= loadAsset('./assets/logo.svg') %>
templateParameters: (compilation, assets, assetTags, options) => {
return {
compilation,
webpackConfig: compilation.options,
htmlWebpackPlugin: {
tags: assetTags,
files: assets,
options,
},
loadAsset: filename => {
const parsedFilepath = path.parse(filename)
const assetNames = Object.keys(compilation.assets).map(k => compilation.options.output.publicPath + k)
for (let i = 0; i < assetNames.length; i++) {
const assetName = assetNames[i]
const parsedAssetPath = path.parse(assetName)
const parsedAssetNameWithoutContentHash = parsedAssetPath.name.split('.')[0]
if (
parsedAssetNameWithoutContentHash === parsedFilepath.name &&
parsedAssetPath.ext === parsedFilepath.ext
) {
return assetName
}
}
},
}
},
}),
new MiniCssExtractPlugin(),
new CopyWebpackPlugin({
patterns: [{ from: dirAssets, to: 'assets/[name].[contenthash].[ext]' }],
}),
],
module: {
rules: [
{
test: /\.m?js$/,
exclude: {
and: [/node_modules/],
not: [/node_modules\/xtendui/],
},
use: {
loader: 'babel-loader',
},
},
{
test: /\.css/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: false,
},
},
{
loader: 'postcss-loader',
options: {
sourceMap: false,
},
},
],
},
{
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/,
type: 'asset/resource',
},
],
},
optimization: {
runtimeChunk: 'single',
},
performance: {
hints: false,
},
}
}