-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.mjs
169 lines (144 loc) · 3.15 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import defaultConfig from '@wordpress/scripts/config/webpack.config.js';
import clone from 'deep-clone';
import { config } from 'dotenv';
import MiniCSSExtractPlugin from 'mini-css-extract-plugin';
import path from 'path';
import { globby } from 'globby';
const blocks = await globby(['./blocks/*/block.json']);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
config();
import { existsSync } from 'fs';
import { fileURLToPath } from 'url';
const createConfig = ({ name, entry, dir, externals }) => {
const isProduction = process.env.NODE_ENV === 'production';
const config = {
...clone(defaultConfig),
name,
entry,
context: path.resolve(__dirname, dir),
module: {
...defaultConfig.module,
rules: [
{
resourceQuery: /inline/,
test: /\.(bmp|png|jpe?g|gif)$/i,
type: 'asset/inline',
},
{
test: /\.md$/i,
type: 'asset/source',
},
...defaultConfig.module.rules,
],
},
watchOptions: {
followSymlinks: true,
ignored: ['node_modules/**'].filter(Boolean),
},
resolveLoader: {
modules: [
path.resolve(__dirname, dir, 'node_modules'),
path.resolve(__dirname, dir, './'),
path.resolve(__dirname, 'node_modules'),
],
},
stats: {
children: false,
all: false,
entrypoints: true,
warnings: true,
errors: true,
hash: false,
timings: true,
errorDetails: true,
builtAt: true,
},
experiments: {
topLevelAwait: true,
},
resolve: {
...defaultConfig.resolve,
extensions: ['.tsx', '.ts', '.js', '.jsx', '.json'],
alias: {
...defaultConfig.resolve.alias,
},
},
optimization: {
...defaultConfig.optimization,
removeEmptyChunks: true,
splitChunks: {
cacheGroups: {
internalStyle: {
type: 'css/mini-extract',
test: /[\\/]+?\.(sc|sa|c)ss$/,
chunks: 'all',
enforce: true,
},
default: false,
},
},
},
externals: {
...defaultConfig.externals,
...(externals || {}),
},
output: {
...defaultConfig.output,
filename: '[name].js',
path: path.resolve(__dirname, dir, 'build'),
},
plugins: [
new MiniCSSExtractPlugin({
filename: '[name].css',
}),
...defaultConfig.plugins.filter(
(plugin) => !['LiveReloadPlugin'].includes(plugin.constructor.name)
),
],
};
if (!isProduction) {
config.devServer.host = '127.0.0.1';
config.devServer.allowedHosts = 'all';
config.devServer.server = {
type: 'https',
options: {
key: process.env['TSL_KEY'],
cert: process.env['TSL_CERT'],
requestCert: false,
},
};
} else {
delete config.devServer;
}
return config;
};
export default [
createConfig({
name: 'theme-css',
dir: './',
entry: {
theme: './src/theme.js',
},
}),
...blocks
.map((blockDir) => {
const dir = path.dirname(blockDir) + '/';
let entry;
if (existsSync(dir + '/script.js')) {
entry = './script.js';
} else if (existsSync(dir + '/style.css')) {
entry = './style.css';
}
return entry
? createConfig({
name: `block/${path.basename(dir)}`,
dir,
entry: {
block: entry,
},
})
: null;
})
.filter(Boolean),
];