-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.ts
More file actions
667 lines (626 loc) · 18.9 KB
/
Copy pathwebpack.config.ts
File metadata and controls
667 lines (626 loc) · 18.9 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
import { watch } from 'chokidar';
import { globSync } from 'glob';
import HTMLInlineCSSWebpackPluginModule from 'html-inline-css-webpack-plugin';
import HtmlInlineScriptWebpackPlugin from 'html-inline-script-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import _ from 'lodash';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import { exec } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import url from 'node:url';
import RemarkHTML from 'remark-html';
import { Server } from 'socket.io';
import TerserPlugin from 'terser-webpack-plugin';
import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin';
import unpluginAutoImport from 'unplugin-auto-import/webpack';
import {
VueUseComponentsResolver,
VueUseDirectiveResolver,
} from 'unplugin-vue-components/resolvers';
import unpluginVueComponents from 'unplugin-vue-components/webpack';
import { VueLoaderPlugin } from 'vue-loader';
import webpack from 'webpack';
import WebpackObfuscator from 'webpack-obfuscator';
const HTMLInlineCSSWebpackPlugin =
(HTMLInlineCSSWebpackPluginModule as any).default || HTMLInlineCSSWebpackPluginModule;
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
interface Config {
port: number;
entries: Entry[];
}
interface Entry {
script: string;
html?: string;
}
function parse_entry(script_file: string) {
const html = path.join(path.dirname(script_file), 'index.html');
if (fs.existsSync(html)) {
return { script: script_file, html };
}
return { script: script_file };
}
function common_path(lhs: string, rhs: string) {
const lhs_parts = lhs.split(path.sep);
const rhs_parts = rhs.split(path.sep);
for (let i = 0; i < Math.min(lhs_parts.length, rhs_parts.length); i++) {
if (lhs_parts[i] !== rhs_parts[i]) {
return lhs_parts.slice(0, i).join(path.sep);
}
}
return lhs_parts.join(path.sep);
}
function glob_script_files(entryFilter?: string) {
const files: string[] = globSync(`src/**/index.{ts,tsx,js,jsx}`).filter(file => {
const matchesCiRule =
process.env.CI !== 'true' || !fs.readFileSync(path.join(__dirname, file)).includes('@no-ci');
if (!matchesCiRule) {
return false;
}
if (!entryFilter) {
return true;
}
const relativeScriptPath = path.relative(
path.join(__dirname, 'src'),
path.join(__dirname, file),
);
const [projectName] = relativeScriptPath.split(path.sep);
return projectName === entryFilter;
});
const results: string[] = [];
const handle = (file: string) => {
const file_dirname = path.dirname(file);
for (const [index, result] of results.entries()) {
const result_dirname = path.dirname(result);
const common = common_path(result_dirname, file_dirname);
if (common === result_dirname) {
return;
}
if (common === file_dirname) {
results.splice(index, 1, file);
return;
}
}
results.push(file);
};
files.forEach(handle);
return results;
}
function createConfig(entryFilter?: string): Config {
return {
port: 6621,
entries: glob_script_files(entryFilter).map(parse_entry),
};
}
let io: Server;
function watch_it(compiler: webpack.Compiler) {
if (compiler.options.watch) {
if (!io) {
const port = 6621;
io = new Server(port, { cors: { origin: '*' } });
console.info(`[Listener] 已启动酒馆监听服务, 正在监听: http://0.0.0.0:${port}`);
io.on('connect', socket => {
console.info(`[Listener] 成功连接到酒馆网页 '${socket.id}', 初始化推送...`);
io.emit('iframe_updated');
socket.on('disconnect', reason => {
console.info(`[Listener] 与酒馆网页 '${socket.id}' 断开连接: ${reason}`);
});
});
}
compiler.hooks.done.tap('updater', () => {
console.info('\n[Listener] 检测到完成编译, 推送更新事件...');
if (compiler.options.plugins.find(plugin => plugin instanceof HtmlWebpackPlugin)) {
io.emit('message_iframe_updated');
} else {
io.emit('script_iframe_updated');
}
});
}
}
// schema dump 相关状态
const SchemaDump = {
initialized: false,
hasSchemaFiles: null as boolean | null,
/** 检测 src 目录下是否存在 schema.ts 文件 */
detectSchemaFiles(): boolean {
if (this.hasSchemaFiles === null) {
this.hasSchemaFiles = globSync('src/**/schema.ts').length > 0;
}
return this.hasSchemaFiles;
},
};
function dump_schema(compiler: webpack.Compiler) {
// 单例模式 + 按需执行:仅在存在 schema.ts 时初始化一次
if (SchemaDump.initialized) return;
if (!SchemaDump.detectSchemaFiles()) return;
SchemaDump.initialized = true;
const execute = () => {
exec('pnpm dump', { cwd: __dirname });
console.info('\x1b[36m[schema_dump]\x1b[0m 已将所有 schema.ts 转换为 schema.json');
};
const executeDebounced = _.debounce(execute, 500, { leading: true, trailing: false });
if (!compiler.options.watch) {
executeDebounced();
} else {
watch('src', {
awaitWriteFinish: true,
}).on('all', (_event, changedPath) => {
if (changedPath.endsWith('schema.ts')) {
executeDebounced();
}
});
}
}
// 公共配置常量 (DRY 原则)
const RegExpSpecialCharacters = /[.*+?^${}()|[\]\\]/g;
/** ts-loader 公共配置 */
const TsLoaderOptions = {
transpileOnly: true,
onlyCompileBundledFiles: true,
compilerOptions: {
noUnusedLocals: false,
noUnusedParameters: false,
},
};
function resolveEntryProjectName(entry: Entry) {
const relativeScriptPath = path.relative(
path.join(__dirname, 'src'),
path.join(__dirname, entry.script),
);
const [projectName] = relativeScriptPath.split(path.sep);
if (!projectName) {
throw new Error(`无法从入口路径解析子项目名称: ${entry.script}`);
}
return projectName;
}
function resolveTsconfigFile(entry: Entry) {
return path.join(__dirname, 'src', resolveEntryProjectName(entry), 'tsconfig.json');
}
function createEntryScopedAutoImportInclude(entry: Entry) {
const projectName = resolveEntryProjectName(entry).replace(RegExpSpecialCharacters, '\\$&');
return [
new RegExp(`[\\/]src[\\/]${projectName}[\\/].*\\.[tj]sx?$`),
new RegExp(`[\\/]src[\\/]${projectName}[\\/].*\\.vue$`),
new RegExp(`[\\/]src[\\/]${projectName}[\\/].*\\.vue\\?vue`),
new RegExp(`[\\/]src[\\/]${projectName}[\\/].*\\.vue\\.[tj]sx?\\?vue`),
];
}
function createEntryScopedComponentGlobs(entry: Entry) {
const projectName = resolveEntryProjectName(entry);
return [`src/${projectName}/**/*.vue`];
}
function createTsLoaderOptions(entry: Entry) {
return {
...TsLoaderOptions,
instance: entry.script,
configFile: resolveTsconfigFile(entry),
};
}
/** css-loader 基础配置 */
const CssLoaderOptionsBase = { url: false } as const;
/** css-loader 模块化配置 */
const CssLoaderOptionsModules = {
url: false,
esModule: false,
modules: {
localIdentName: '[name]__[local]--[hash:base64:5]',
namedExport: false,
},
} as const;
/** 通用排除规则 */
const ExcludeNodeModules = /node_modules/;
// 规则生成工厂函数
type AssetType = 'asset/source' | 'asset/inline';
interface ResourceQueryRuleOptions {
entry: Entry;
resourceQuery: RegExp;
type: AssetType;
}
/**
* 生成带 resourceQuery 的规则集
* @description 用于 ?raw 和 ?url 导入的统一处理
*/
function createResourceQueryRules({
entry,
resourceQuery,
type,
}: ResourceQueryRuleOptions): webpack.RuleSetRule[] {
return [
{
test: /\.tsx?$/,
loader: 'ts-loader',
options: createTsLoaderOptions(entry),
resourceQuery,
type,
exclude: ExcludeNodeModules,
},
{
test: /\.(sa|sc)ss$/,
use: ['postcss-loader', 'sass-loader'],
resourceQuery,
type,
exclude: ExcludeNodeModules,
},
{
test: /\.css$/,
use: ['postcss-loader'],
resourceQuery,
type,
exclude: ExcludeNodeModules,
},
{
resourceQuery,
type,
exclude: ExcludeNodeModules,
},
];
}
/**
* 生成无 HTML 入口时的样式规则
* @description 使用 vue-style-loader 和 style-loader
*/
function createNoHtmlStyleRules(): webpack.RuleSetRule[] {
return [
{
test: /\.vue\.s(a|c)ss$/,
use: [
{ loader: 'vue-style-loader', options: { ssrId: true } },
{ loader: 'css-loader', options: CssLoaderOptionsBase },
'postcss-loader',
'sass-loader',
],
exclude: ExcludeNodeModules,
},
{
test: /\.vue\.css$/,
use: [
{ loader: 'vue-style-loader', options: { ssrId: true } },
{ loader: 'css-loader', options: CssLoaderOptionsBase },
'postcss-loader',
],
exclude: ExcludeNodeModules,
},
{
test: /\.s(a|c)ss$/,
use: [
'style-loader',
{ loader: 'css-loader', options: CssLoaderOptionsBase },
'postcss-loader',
'sass-loader',
],
exclude: ExcludeNodeModules,
},
{
test: /\.css$/,
use: [
'style-loader',
{ loader: 'css-loader', options: CssLoaderOptionsBase },
'postcss-loader',
],
exclude: ExcludeNodeModules,
},
];
}
/**
* 生成有 HTML 入口时的样式规则
* @description 使用 MiniCssExtractPlugin.loader 提取 CSS
*/
function createHtmlStyleRules(): webpack.RuleSetRule[] {
return [
{
test: /\.module\.s(a|c)ss$/,
use: [
MiniCssExtractPlugin.loader,
{ loader: 'css-loader', options: CssLoaderOptionsModules },
'postcss-loader',
'sass-loader',
],
exclude: ExcludeNodeModules,
},
{
test: /\.s(a|c)ss$/,
use: [
MiniCssExtractPlugin.loader,
{ loader: 'css-loader', options: CssLoaderOptionsBase },
'postcss-loader',
'sass-loader',
],
exclude: [ExcludeNodeModules, /\.module\.s(a|c)ss$/],
},
{
test: /\.module\.css$/,
use: [
MiniCssExtractPlugin.loader,
{ loader: 'css-loader', options: CssLoaderOptionsModules },
'postcss-loader',
],
exclude: ExcludeNodeModules,
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
{ loader: 'css-loader', options: CssLoaderOptionsBase },
'postcss-loader',
],
exclude: [ExcludeNodeModules, /\.module\.css$/],
},
];
}
// 配置生成函数
function parse_configuration(entry: Entry): (_env: any, argv: any) => webpack.Configuration {
const should_obfuscate = fs
.readFileSync(path.join(__dirname, entry.script), 'utf-8')
.includes('@obfuscate');
const script_filepath = path.parse(entry.script);
return (_env, argv) => ({
experiments: {
outputModule: true,
},
performance: {
hints: argv.mode === 'production' ? 'warning' : false,
maxAssetSize: 1024 * 1024, // 1 MiB
maxEntrypointSize: 1024 * 1024, // 1 MiB
},
devtool: argv.mode === 'production' ? 'hidden-source-map' : 'eval-source-map',
cache: {
type: 'filesystem',
buildDependencies: {
config: [
__filename,
path.join(__dirname, 'package.json'),
path.join(__dirname, 'tsconfig.json'),
path.join(__dirname, 'tsconfig.base.json'),
path.join(__dirname, 'postcss.config.cjs'),
resolveTsconfigFile(entry),
],
},
},
watchOptions: {
ignored: ['**/dist', '**/node_modules'],
},
entry: path.join(__dirname, entry.script),
target: 'browserslist',
output: {
devtoolNamespace: 'tavern_helper_template',
devtoolModuleFilenameTemplate: info => {
const resource_path = decodeURIComponent(info.resourcePath.replace(/^\.\//, ''));
const is_direct = info.allLoaders === '';
const is_vue_script =
resource_path.match(/\.vue$/) &&
info.query.match(/\btype=script\b/) &&
!info.allLoaders.match(/\bts-loader\b/);
return `${is_direct === true ? 'src' : 'webpack'}://${info.namespace}/${resource_path}${is_direct || is_vue_script ? '' : '?' + info.hash}`;
},
filename: `${script_filepath.name}.js`,
path: path.join(
__dirname,
'dist',
path.relative(path.join(__dirname, 'src'), script_filepath.dir),
),
chunkFilename: `${script_filepath.name}.[contenthash].chunk.js`,
asyncChunks: true,
clean: true,
publicPath: '',
library: {
type: 'module',
},
},
module: {
rules: [
{
test: /\.vue$/,
use: 'vue-loader',
exclude: ExcludeNodeModules,
},
{
oneOf: [
// ?raw 导入规则
...createResourceQueryRules({ entry, resourceQuery: /raw/, type: 'asset/source' }),
// ?url 导入规则
...createResourceQueryRules({ entry, resourceQuery: /url/, type: 'asset/inline' }),
// TypeScript 默认规则
{
test: /\.tsx?$/,
loader: 'ts-loader',
options: createTsLoaderOptions(entry),
exclude: ExcludeNodeModules,
},
// HTML 规则
{
test: /\.html$/,
use: 'html-loader',
exclude: ExcludeNodeModules,
},
// Markdown 规则
{
test: /\.md$/,
use: [
{ loader: 'html-loader' },
{
loader: 'remark-loader',
options: {
remarkOptions: {
plugins: [RemarkHTML],
},
},
},
],
},
// 样式规则 (DRY: 使用工厂函数)
...(entry.html === undefined ? createNoHtmlStyleRules() : createHtmlStyleRules()),
],
},
],
},
resolve: {
extensions: ['.ts', '.js', '.tsx', '.jsx', '.css'],
plugins: [
new TsconfigPathsPlugin({
extensions: ['.ts', '.js', '.tsx', '.jsx'],
configFile: path.join(__dirname, 'tsconfig.json'),
}),
],
alias: {},
},
plugins: (entry.html === undefined
? [new MiniCssExtractPlugin()]
: [
new HtmlWebpackPlugin({
template: path.join(__dirname, entry.html),
filename: path.parse(entry.html).base,
scriptLoading: 'module',
cache: false,
}),
new HtmlInlineScriptWebpackPlugin(),
new MiniCssExtractPlugin(),
new HTMLInlineCSSWebpackPlugin({
styleTagFactory({ style }: { style: string }) {
return `<style>${style}</style>`;
},
}),
]
)
.concat(
{ apply: watch_it },
{ apply: dump_schema },
new VueLoaderPlugin(),
unpluginAutoImport({
dts: argv.mode === 'production' ? false : true,
dtsMode: 'overwrite',
include: createEntryScopedAutoImportInclude(entry),
imports: [
'vue',
'pinia',
'@vueuse/core',
{ from: 'dedent', imports: [['default', 'dedent']] },
{ from: 'klona', imports: ['klona'] },
{ from: 'vue-final-modal', imports: ['useModal'] },
{ from: 'zod', imports: ['z'] },
],
}),
unpluginVueComponents({
dts: argv.mode === 'production' ? false : true,
syncMode: 'overwrite',
globs: createEntryScopedComponentGlobs(entry),
resolvers: [VueUseComponentsResolver(), VueUseDirectiveResolver()],
}),
new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
new webpack.DefinePlugin({
__VUE_OPTIONS_API__: false,
__VUE_PROD_DEVTOOLS__: process.env.CI !== 'true',
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__: false,
// 注入版本号用于 CDN 缓存破坏
__APP_VERSION__: JSON.stringify(
JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf-8')).version,
),
}),
)
.concat(
should_obfuscate
? [
new WebpackObfuscator({
controlFlowFlattening: true,
numbersToExpressions: true,
selfDefending: true,
simplify: true,
splitStrings: true,
seed: 1,
}),
]
: [],
),
optimization: {
minimize: true,
minimizer: [
argv.mode === 'production'
? new TerserPlugin({
terserOptions: {
format: { quote_style: 1 },
mangle: { reserved: ['_', 'toastr', 'YAML', '$', 'z'] },
},
})
: new TerserPlugin({
extractComments: false,
terserOptions: {
format: { beautify: true, indent_level: 2 },
compress: false,
mangle: false,
},
}),
],
splitChunks: false,
// splitChunks: {
// chunks: 'async',
// minSize: 20000,
// minChunks: 1,
// maxAsyncRequests: 30,
// maxInitialRequests: 30,
// cacheGroups: {
// vendor: {
// name: 'vendor',
// test: /[\\/]node_modules[\\/]/,
// priority: -10,
// },
// default: {
// name: 'default',
// minChunks: 2,
// priority: -20,
// reuseExistingChunk: true,
// },
// },
// },
},
externals: ({ context, request }, callback) => {
if (!context || !request) {
return callback();
}
if (
request.startsWith('-') ||
request.startsWith('.') ||
request.startsWith('/') ||
request.startsWith('!') ||
request.startsWith('http') ||
request.startsWith('@/') ||
path.isAbsolute(request) ||
fs.existsSync(path.join(context, request)) ||
fs.existsSync(request)
) {
return callback();
}
if (
['vue', 'vue-router'].every(key => request !== key) &&
['vue', 'react', 'zustand'].some(key => request.includes(key))
) {
return callback();
}
const global = {
jquery: '$',
lodash: '_',
showdown: 'showdown',
toastr: 'toastr',
vue: 'Vue',
'vue-router': 'VueRouter',
yaml: 'YAML',
zod: 'z',
};
if (request in global) {
return callback(null, 'var ' + global[request as keyof typeof global]);
}
const cdn = {
sass: 'https://jspm.dev/sass',
};
return callback(
null,
'module-import ' +
(cdn[request as keyof typeof cdn] ??
`https://testingcf.jsdelivr.net/npm/${request}/+esm`),
);
},
});
}
export default (env: { entry?: string } = {}, argv: any) => {
const config = createConfig(env.entry);
return config.entries.map(entry => parse_configuration(entry)(env, argv));
};