@esmx/rspack 3.0.0-rc.43 → 3.0.0-rc.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/index.d.ts +2 -4
  2. package/dist/index.mjs +4 -4
  3. package/dist/{app.d.ts → rspack/app.d.ts} +41 -0
  4. package/dist/{app.mjs → rspack/app.mjs} +2 -4
  5. package/dist/rspack/build-target.d.ts +8 -0
  6. package/dist/rspack/chain-config.d.ts +7 -0
  7. package/dist/rspack/chain-config.mjs +133 -0
  8. package/dist/rspack/index.d.ts +3 -0
  9. package/dist/rspack/index.mjs +4 -0
  10. package/dist/{html-app.d.ts → rspack-html/index.d.ts} +1 -114
  11. package/dist/rspack-html/index.mjs +158 -0
  12. package/package.json +7 -6
  13. package/src/index.ts +6 -5
  14. package/src/{app.ts → rspack/app.ts} +49 -6
  15. package/src/rspack/build-target.ts +8 -0
  16. package/src/rspack/chain-config.ts +193 -0
  17. package/src/rspack/index.ts +8 -0
  18. package/src/rspack-html/index.ts +510 -0
  19. package/dist/build-target.d.ts +0 -8
  20. package/dist/html-app.mjs +0 -214
  21. package/src/build-target.ts +0 -8
  22. package/src/html-app.ts +0 -559
  23. /package/dist/{build-target.mjs → rspack/build-target.mjs} +0 -0
  24. /package/dist/{config.d.ts → rspack/config.d.ts} +0 -0
  25. /package/dist/{config.mjs → rspack/config.mjs} +0 -0
  26. /package/dist/{hmr-config.d.ts → rspack/hmr-config.d.ts} +0 -0
  27. /package/dist/{hmr-config.mjs → rspack/hmr-config.mjs} +0 -0
  28. /package/dist/{hot-fix.d.ts → rspack/hot-fix.d.ts} +0 -0
  29. /package/dist/{hot-fix.mjs → rspack/hot-fix.mjs} +0 -0
  30. /package/dist/{loader.d.ts → rspack/loader.d.ts} +0 -0
  31. /package/dist/{loader.mjs → rspack/loader.mjs} +0 -0
  32. /package/dist/{pack.d.ts → rspack/pack.d.ts} +0 -0
  33. /package/dist/{pack.mjs → rspack/pack.mjs} +0 -0
  34. /package/dist/{utils → rspack/utils}/index.d.ts +0 -0
  35. /package/dist/{utils → rspack/utils}/index.mjs +0 -0
  36. /package/dist/{utils → rspack/utils}/rsbuild.d.ts +0 -0
  37. /package/dist/{utils → rspack/utils}/rsbuild.mjs +0 -0
  38. /package/src/{config.ts → rspack/config.ts} +0 -0
  39. /package/src/{hmr-config.ts → rspack/hmr-config.ts} +0 -0
  40. /package/src/{hot-fix.ts → rspack/hot-fix.ts} +0 -0
  41. /package/src/{loader.ts → rspack/loader.ts} +0 -0
  42. /package/src/{pack.ts → rspack/pack.ts} +0 -0
  43. /package/src/{utils → rspack/utils}/index.ts +0 -0
  44. /package/src/{utils → rspack/utils}/rsbuild.ts +0 -0
package/src/html-app.ts DELETED
@@ -1,559 +0,0 @@
1
- import type { Esmx } from '@esmx/core';
2
- import {
3
- type LightningcssLoaderOptions,
4
- type RuleSetUse,
5
- type SwcLoaderOptions,
6
- rspack
7
- } from '@rspack/core';
8
- import NodePolyfillPlugin from 'node-polyfill-webpack-plugin';
9
- import {
10
- type RspackAppConfigContext,
11
- type RspackAppOptions,
12
- createRspackApp
13
- } from './app';
14
- import type { BuildTarget } from './build-target';
15
- import { RSPACK_LOADER } from './loader';
16
-
17
- /**
18
- * Rspack HTML 应用配置选项接口
19
- *
20
- * @example
21
- * ```ts
22
- * // entry.node.ts
23
- * export default {
24
- * async devApp(esmx) {
25
- * return import('@esmx/rspack').then((m) =>
26
- * m.createRspackHtmlApp(esmx, {
27
- * // 将 CSS 输出到独立的 CSS 文件中
28
- * css: 'css',
29
- * // 自定义 loader
30
- * loaders: {
31
- * styleLoader: 'vue-style-loader'
32
- * },
33
- * // 配置 CSS 相关 loader
34
- * styleLoader: {
35
- * injectType: 'singletonStyleTag'
36
- * },
37
- * cssLoader: {
38
- * modules: true
39
- * },
40
- * // 配置构建目标
41
- * target: {
42
- * web: ['chrome>=87'],
43
- * node: ['node>=16']
44
- * },
45
- * // 定义全局常量
46
- * definePlugin: {
47
- * 'process.env.APP_ENV': JSON.stringify('production')
48
- * }
49
- * })
50
- * );
51
- * }
52
- * };
53
- */
54
- export interface RspackHtmlAppOptions extends RspackAppOptions {
55
- /**
56
- * CSS 输出模式配置
57
- *
58
- * @default 根据环境自动选择:
59
- * - 生产环境: 'css',将CSS输出到独立文件中,有利于缓存和并行加载
60
- * - 开发环境: 'js',将CSS打包到JS中以支持热更新(HMR),实现样式的即时更新
61
- *
62
- * - 'css': 将 CSS 输出到独立的 CSS 文件中
63
- * - 'js': 将 CSS 打包到 JS 文件中,运行时动态插入样式
64
- * - false: 关闭默认的 CSS 处理配置,需要手动配置 loader 规则
65
- *
66
- * @example
67
- * ```ts
68
- * // 使用环境默认配置
69
- * css: undefined
70
- *
71
- * // 强制输出到独立的 CSS 文件
72
- * css: 'css'
73
- *
74
- * // 强制打包到 JS 中
75
- * css: 'js'
76
- *
77
- * // 自定义 CSS 处理
78
- * css: false
79
- * ```
80
- */
81
- css?: 'css' | 'js' | false;
82
-
83
- /**
84
- * 自定义 loader 配置
85
- *
86
- * 允许替换默认的 loader 实现,可用于切换到特定框架的 loader
87
- *
88
- * @example
89
- * ```ts
90
- * // 使用 Vue 的 style-loader
91
- * loaders: {
92
- * styleLoader: 'vue-style-loader'
93
- * }
94
- * ```
95
- */
96
- loaders?: Partial<Record<keyof typeof RSPACK_LOADER, string>>;
97
-
98
- /**
99
- * style-loader 配置项
100
- *
101
- * 用于配置样式注入方式,完整选项参考:
102
- * https://github.com/webpack-contrib/style-loader
103
- *
104
- * @example
105
- * ```ts
106
- * styleLoader: {
107
- * injectType: 'singletonStyleTag',
108
- * attributes: { id: 'app-styles' }
109
- * }
110
- * ```
111
- */
112
- styleLoader?: Record<string, any>;
113
-
114
- /**
115
- * css-loader 配置项
116
- *
117
- * 用于配置 CSS 模块化、URL 解析等,完整选项参考:
118
- * https://github.com/webpack-contrib/css-loader
119
- *
120
- * @example
121
- * ```ts
122
- * cssLoader: {
123
- * modules: true,
124
- * url: false
125
- * }
126
- * ```
127
- */
128
- cssLoader?: Record<string, any>;
129
-
130
- /**
131
- * less-loader 配置项
132
- *
133
- * 用于配置 Less 编译选项,完整选项参考:
134
- * https://github.com/webpack-contrib/less-loader
135
- *
136
- * @example
137
- * ```ts
138
- * lessLoader: {
139
- * lessOptions: {
140
- * javascriptEnabled: true,
141
- * modifyVars: { '@primary-color': '#1DA57A' }
142
- * }
143
- * }
144
- * ```
145
- */
146
- lessLoader?: Record<string, any>;
147
-
148
- /**
149
- * style-resources-loader 配置项
150
- *
151
- * 用于自动注入全局的样式资源,完整选项参考:
152
- * https://github.com/yenshih/style-resources-loader
153
- *
154
- * @example
155
- * ```ts
156
- * styleResourcesLoader: {
157
- * patterns: [
158
- * './src/styles/variables.less',
159
- * './src/styles/mixins.less'
160
- * ]
161
- * }
162
- * ```
163
- */
164
- styleResourcesLoader?: Record<string, any>;
165
-
166
- /**
167
- * SWC loader 配置项
168
- *
169
- * 用于配置 TypeScript/JavaScript 编译选项,完整选项参考:
170
- * https://rspack.dev/guide/features/builtin-swc-loader
171
- *
172
- * @example
173
- * ```ts
174
- * swcLoader: {
175
- * jsc: {
176
- * parser: {
177
- * syntax: 'typescript',
178
- * decorators: true
179
- * },
180
- * transform: {
181
- * legacyDecorator: true
182
- * }
183
- * }
184
- * }
185
- * ```
186
- */
187
- swcLoader?: SwcLoaderOptions;
188
-
189
- /**
190
- * DefinePlugin 配置项
191
- *
192
- * 用于定义编译时的全局常量,支持针对不同构建目标设置不同的值
193
- * 完整说明参考: https://rspack.dev/plugins/webpack/define-plugin
194
- *
195
- * @example
196
- * ```ts
197
- * // 统一的值
198
- * definePlugin: {
199
- * 'process.env.APP_ENV': JSON.stringify('production')
200
- * }
201
- *
202
- * // 针对不同构建目标的值
203
- * definePlugin: {
204
- * 'process.env.IS_SERVER': {
205
- * server: 'true',
206
- * client: 'false'
207
- * }
208
- * }
209
- * ```
210
- */
211
- definePlugin?: Record<
212
- string,
213
- string | Partial<Record<BuildTarget, string>>
214
- >;
215
-
216
- /**
217
- * 构建目标配置
218
- *
219
- * 用于设置代码的目标运行环境,影响代码的编译降级和 polyfill 注入
220
- *
221
- * @example
222
- * ```ts
223
- * target: {
224
- * // 浏览器构建目标
225
- * web: ['chrome>=87', 'firefox>=78', 'safari>=14'],
226
- * // Node.js 构建目标
227
- * node: ['node>=16']
228
- * }
229
- * ```
230
- */
231
- target?: {
232
- /**
233
- * 浏览器构建目标
234
- *
235
- * @default ['chrome>=64', 'edge>=79', 'firefox>=67', 'safari>=11.1']
236
- */
237
- web?: string[];
238
-
239
- /**
240
- * Node.js 构建目标
241
- *
242
- * @default ['node>=24']
243
- */
244
- node?: string[];
245
- };
246
- }
247
- /**
248
- * 创建 Rspack HTML 应用实例。
249
- *
250
- * 该函数提供了完整的 Web 应用构建配置,支持以下资源类型的处理:
251
- * - TypeScript/JavaScript
252
- * - Web Worker
253
- * - JSON
254
- * - CSS/Less
255
- * - 视频/图片
256
- * - 字体文件
257
- *
258
- * @param esmx - Esmx 框架实例
259
- * @param options - Rspack HTML 应用配置选项
260
- * @returns 返回应用实例,包含中间件、渲染函数和构建函数
261
- *
262
- * @example
263
- * ```ts
264
- * // 开发环境配置
265
- * // entry.node.ts
266
- * export default {
267
- * async devApp(esmx) {
268
- * return import('@esmx/rspack').then((m) =>
269
- * m.createRspackHtmlApp(esmx, {
270
- * // 配置 CSS 输出模式
271
- * css: 'css',
272
- * // 配置 TypeScript 编译选项
273
- * swcLoader: {
274
- * jsc: {
275
- * parser: {
276
- * syntax: 'typescript',
277
- * decorators: true
278
- * }
279
- * }
280
- * },
281
- * // 配置构建目标
282
- * target: {
283
- * web: ['chrome>=87'],
284
- * node: ['node>=16']
285
- * },
286
- * // 自定义 Rspack 配置
287
- * config({ config }) {
288
- * // 添加自定义 loader
289
- * config.module.rules.push({
290
- * test: /\.vue$/,
291
- * loader: 'vue-loader'
292
- * });
293
- * }
294
- * })
295
- * );
296
- * }
297
- * };
298
- *
299
- * // 生产环境配置
300
- * // entry.node.ts
301
- * export default {
302
- * async buildApp(esmx) {
303
- * return import('@esmx/rspack').then((m) =>
304
- * m.createRspackHtmlApp(esmx, {
305
- * // 启用代码压缩
306
- * minimize: true,
307
- * // 配置全局常量
308
- * definePlugin: {
309
- * 'process.env.NODE_ENV': JSON.stringify('production'),
310
- * 'process.env.IS_SERVER': {
311
- * server: 'true',
312
- * client: 'false'
313
- * }
314
- * }
315
- * })
316
- * );
317
- * }
318
- * };
319
- * ```
320
- */
321
- export async function createRspackHtmlApp(
322
- esmx: Esmx,
323
- options?: RspackHtmlAppOptions
324
- ) {
325
- options = {
326
- ...options,
327
- target: {
328
- web: ['chrome>=64', 'edge>=79', 'firefox>=67', 'safari>=11.1'],
329
- node: ['node>=24'],
330
- ...options?.target
331
- },
332
- css: options?.css ? options.css : esmx.isProd ? 'css' : 'js'
333
- };
334
- return createRspackApp(esmx, {
335
- ...options,
336
- config(context) {
337
- const { config, buildTarget } = context;
338
- config.stats = 'errors-warnings';
339
- config.module = {
340
- ...config.module,
341
- rules: [
342
- ...(config.module?.rules ?? []),
343
- {
344
- test: /\.(jpe?g|png|gif|bmp|webp|svg)$/i,
345
- type: 'asset/resource',
346
- generator: {
347
- filename: filename(esmx, 'images')
348
- }
349
- },
350
- {
351
- test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)$/i,
352
- type: 'asset/resource',
353
- generator: {
354
- filename: filename(esmx, 'media')
355
- }
356
- },
357
- {
358
- test: /\.(woff|woff2|eot|ttf|otf)(\?.*)?$/i,
359
- type: 'asset/resource',
360
- generator: {
361
- filename: filename(esmx, 'fonts')
362
- }
363
- },
364
- {
365
- test: /\.json$/i,
366
- type: 'json'
367
- },
368
- {
369
- test: /\.worker\.(c|m)?(t|j)s$/i,
370
- loader:
371
- options.loaders?.workerRspackLoader ??
372
- RSPACK_LOADER.workerRspackLoader,
373
- options: {
374
- esModule: false,
375
- filename: `${esmx.name}/workers/[name].[contenthash]${esmx.isProd ? '.final' : ''}.js`
376
- }
377
- },
378
- {
379
- test: /\.(ts|mts)$/i,
380
- loader:
381
- options.loaders?.builtinSwcLoader ??
382
- RSPACK_LOADER.builtinSwcLoader,
383
- options: {
384
- env: {
385
- targets:
386
- buildTarget === 'client'
387
- ? options?.target?.web
388
- : options?.target?.node,
389
- ...options?.swcLoader?.env
390
- },
391
- jsc: {
392
- parser: {
393
- syntax: 'typescript',
394
- ...options?.swcLoader?.jsc?.parser
395
- },
396
- ...options?.swcLoader?.jsc
397
- },
398
- ...options?.swcLoader
399
- } satisfies SwcLoaderOptions,
400
- type: 'javascript/auto'
401
- }
402
- ]
403
- };
404
- config.optimization = {
405
- ...config.optimization,
406
- minimizer: [
407
- new rspack.SwcJsMinimizerRspackPlugin({
408
- minimizerOptions: {
409
- format: {
410
- comments: false
411
- }
412
- }
413
- }),
414
- new rspack.LightningCssMinimizerRspackPlugin({
415
- minimizerOptions: {
416
- targets: options.target?.web,
417
- errorRecovery: false
418
- }
419
- })
420
- ]
421
- };
422
- config.plugins = [
423
- new NodePolyfillPlugin(),
424
- ...(config.plugins ?? [])
425
- ];
426
- config.devtool = false;
427
- config.cache = false;
428
- if (options.definePlugin) {
429
- const defineOptions: Record<string, string> = {};
430
- Object.entries(options.definePlugin).forEach(
431
- ([name, value]) => {
432
- const targetValue =
433
- typeof value === 'string'
434
- ? value
435
- : value[buildTarget];
436
- if (
437
- typeof targetValue === 'string' &&
438
- name !== targetValue
439
- ) {
440
- defineOptions[name] = targetValue;
441
- }
442
- }
443
- );
444
- if (Object.keys(defineOptions).length) {
445
- config.plugins.push(new rspack.DefinePlugin(defineOptions));
446
- }
447
- }
448
- config.resolve = {
449
- ...config.resolve,
450
- extensions: ['...', '.ts']
451
- };
452
- addCssConfig(esmx, options, context);
453
- options?.config?.(context);
454
- }
455
- });
456
- }
457
-
458
- function filename(esmx: Esmx, name: string, ext = '[ext]') {
459
- return esmx.isProd
460
- ? `${name}/[name].[contenthash:8].final${ext}`
461
- : `${name}/[path][name]${ext}`;
462
- }
463
-
464
- function addCssConfig(
465
- esmx: Esmx,
466
- options: RspackHtmlAppOptions,
467
- { config }: RspackAppConfigContext
468
- ) {
469
- if (options.css === false) {
470
- return;
471
- }
472
- // 输出在 .js 文件中
473
- if (options.css === 'js') {
474
- const cssRule: RuleSetUse = [
475
- {
476
- loader:
477
- options.loaders?.styleLoader ?? RSPACK_LOADER.styleLoader,
478
- options: options.styleLoader
479
- },
480
- {
481
- loader: options.loaders?.cssLoader ?? RSPACK_LOADER.cssLoader,
482
- options: options.cssLoader
483
- },
484
- {
485
- loader:
486
- options.loaders?.lightningcssLoader ??
487
- RSPACK_LOADER.lightningcssLoader,
488
- options: {
489
- targets: options.target?.web ?? [],
490
- minify: esmx.isProd
491
- } satisfies LightningcssLoaderOptions
492
- }
493
- ];
494
- const lessRule: RuleSetUse = [
495
- {
496
- loader: options.loaders?.lessLoader ?? RSPACK_LOADER.lessLoader,
497
- options: options.lessLoader
498
- }
499
- ];
500
- if (options.styleResourcesLoader) {
501
- lessRule.push({
502
- loader:
503
- options.loaders?.styleResourcesLoader ??
504
- RSPACK_LOADER.styleResourcesLoader,
505
- options: options.styleResourcesLoader
506
- });
507
- }
508
- config.module = {
509
- ...config.module,
510
- rules: [
511
- ...(config.module?.rules ?? []),
512
- {
513
- test: /\.less$/,
514
- use: [...cssRule, ...lessRule],
515
- type: 'javascript/auto'
516
- },
517
- {
518
- test: /\.css$/,
519
- use: cssRule,
520
- type: 'javascript/auto'
521
- }
522
- ]
523
- };
524
- return;
525
- }
526
- // 输出在 .css 文件中
527
- config.experiments = {
528
- ...config.experiments,
529
- css: true
530
- };
531
- if (!config.experiments.css) {
532
- return;
533
- }
534
- const lessLoaders: RuleSetUse = [
535
- {
536
- loader: options.loaders?.lessLoader ?? RSPACK_LOADER.lessLoader,
537
- options: options.lessLoader
538
- }
539
- ];
540
- if (options.styleResourcesLoader) {
541
- lessLoaders.push({
542
- loader:
543
- options.loaders?.styleResourcesLoader ??
544
- RSPACK_LOADER.styleResourcesLoader,
545
- options: options.styleResourcesLoader
546
- });
547
- }
548
- config.module = {
549
- ...config.module,
550
- rules: [
551
- ...(config.module?.rules ?? []),
552
- {
553
- test: /\.less$/,
554
- use: [...lessLoaders],
555
- type: 'css'
556
- }
557
- ]
558
- };
559
- }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes