/
webpack.config.ts
127 lines (125 loc) · 3.16 KB
/
webpack.config.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
import ESLintPlugin from 'eslint-webpack-plugin'
import FileManagerPlugin from 'filemanager-webpack-plugin'
import MiniCssExtractPlugin, {
loader as miniCSSLoader,
} from 'mini-css-extract-plugin'
import path from 'path'
import type { Configuration } from 'webpack'
import 'webpack-dev-server'
import RemoveEmptyScriptsPlugin from 'webpack-remove-empty-scripts'
import config from './quick.config'
export default function (env = { WEBPACK_SERVE: false }): Configuration {
const { WEBPACK_SERVE: isDev = false } = env
return {
entry: {
main: './src/ts/main.ts',
...(isDev
? {
dev: './src/ts/dev.ts',
}
: {
styles: './src/css/main.css',
'editor-styles': './src/css/editor.css',
}),
},
output: {
filename: 'js/[name].js',
path: path.resolve(__dirname, config.distDir),
clean: true,
},
resolve: {
extensions: ['.ts', '...'],
},
mode: isDev ? 'development' : 'production',
devtool: false,
devServer: {
port: config.devServerPort,
hot: true,
watchFiles: [
'inc/**/*.php',
'blocks/**/*.php',
'parts/**/*.php',
'templates/**/*.php',
'*.php',
],
allowedHosts: ['localhost', '.local'],
headers: {
'Access-Control-Allow-Origin': '*',
},
},
module: {
rules: [
{
test: /\.ts$/,
exclude: /node_modules/,
use: {
loader: 'swc-loader',
options: {
jsc: {
parser: {
syntax: 'typescript',
tsx: false,
},
},
},
},
},
{
test: /\.css$/,
exclude: /node_modules/,
use: [
isDev ? 'style-loader' : miniCSSLoader,
'css-loader',
'postcss-loader',
],
},
{
test: /\.(woff|woff2)$/,
type: 'asset',
generator: {
filename: './fonts/[name][ext]',
},
},
],
},
plugins: [
...(!isDev
? [
new FileManagerPlugin({
events: {
onStart: {
delete: [config.exportDir],
},
onEnd: {
copy: [
{
source: '*.{php,css}|theme.json',
destination: config.exportDir,
},
...config.dirsToExport.map((name) => ({
source: name,
destination: `${config.exportDir}/${name}`,
})),
],
delete: [`${config.exportDir}/inc/dev-setup.php`],
},
},
}),
new RemoveEmptyScriptsPlugin({}),
new MiniCssExtractPlugin({
filename: 'css/[name].css',
}),
]
: []),
new ESLintPlugin({
extensions: ['ts'],
}),
],
cache: {
type: 'filesystem',
},
optimization: {
runtimeChunk: isDev ? 'single' : undefined,
},
}
}