@esmx/rspack 3.0.0-rc.44 → 3.0.0-rc.46

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
@@ -15,7 +15,7 @@ import { createVmImport } from '@esmx/import';
15
15
  import type { RspackOptions } from '@rspack/core';
16
16
  import hotMiddleware from 'webpack-hot-middleware';
17
17
  import type { BuildTarget } from './build-target';
18
- import { createRspackConfig } from './config';
18
+ import { createRspackConfig } from './chain-config';
19
19
  import { pack } from './pack';
20
20
  import { createRsBuild } from './utils';
21
21
 
@@ -87,6 +87,42 @@ export interface RspackAppConfigContext {
87
87
  options: RspackAppOptions;
88
88
  }
89
89
 
90
+ /**
91
+ * Rspack 链式配置上下文接口。
92
+ *
93
+ * 该接口提供了在 chain 钩子函数中可以访问的上下文信息,允许你:
94
+ * - 访问 Esmx 框架实例
95
+ * - 获取当前的构建目标(client/server/node)
96
+ * - 使用 rspack-chain 修改配置
97
+ * - 访问应用选项
98
+ */
99
+ export interface RspackAppChainContext {
100
+ /**
101
+ * Esmx 框架实例。
102
+ * 可用于访问框架提供的 API 和工具函数。
103
+ */
104
+ esmx: Esmx;
105
+
106
+ /**
107
+ * 当前的构建目标。
108
+ * - 'client': 客户端构建,生成浏览器可执行的代码
109
+ * - 'server': 服务端构建,生成 SSR 渲染代码
110
+ * - 'node': Node.js 构建,生成服务器入口代码
111
+ */
112
+ buildTarget: BuildTarget;
113
+
114
+ /**
115
+ * rspack-chain 配置对象。
116
+ * 你可以在 chain 钩子中使用链式 API 修改配置。
117
+ */
118
+ chain: import('rspack-chain');
119
+
120
+ /**
121
+ * 创建应用时传入的选项对象。
122
+ */
123
+ options: RspackAppOptions;
124
+ }
125
+
90
126
  /**
91
127
  * Rspack 应用配置选项接口。
92
128
  *
@@ -138,6 +174,16 @@ export interface RspackAppOptions {
138
174
  * @param context - 配置上下文,包含框架实例、构建目标和配置对象
139
175
  */
140
176
  config?: (context: RspackAppConfigContext) => void;
177
+
178
+ /**
179
+ * Rspack chain 配置钩子函数。
180
+ *
181
+ * 使用 rspack-chain 提供链式配置方式,可以更灵活地修改 Rspack 配置。
182
+ * 在 config 钩子之后调用,如果有 chain 钩子则优先使用链式配置。
183
+ *
184
+ * @param context - 配置上下文,包含框架实例、构建目标和 chain 配置对象
185
+ */
186
+ chain?: (context: RspackAppChainContext) => void;
141
187
  }
142
188
 
143
189
  /**
@@ -240,11 +286,8 @@ function generateBuildConfig(
240
286
  esmx: Esmx,
241
287
  options: RspackAppOptions,
242
288
  buildTarget: BuildTarget
243
- ) {
244
- const config = createRspackConfig(esmx, buildTarget, options);
245
- options.config?.({ esmx, options, buildTarget, config });
246
-
247
- return config;
289
+ ): RspackOptions {
290
+ return createRspackConfig(esmx, buildTarget, options);
248
291
  }
249
292
 
250
293
  function rewriteRender(esmx: Esmx) {
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Build target environment type
3
+ * @description Defines the build target environment for the application, used to configure specific optimizations and features during the build process
4
+ * - node: Build code to run in Node.js environment
5
+ * - client: Build code to run in browser environment
6
+ * - server: Build code to run in server environment
7
+ */
8
+ export type BuildTarget = 'node' | 'client' | 'server';
@@ -0,0 +1,193 @@
1
+ import type { Esmx } from '@esmx/core';
2
+ import { moduleLinkPlugin } from '@esmx/rspack-module-link-plugin';
3
+ import { rspack } from '@rspack/core';
4
+ import type { RspackOptions } from '@rspack/core';
5
+ import RspackChain from 'rspack-chain';
6
+ import nodeExternals from 'webpack-node-externals';
7
+ import type { RspackAppOptions } from './app';
8
+ import type { BuildTarget } from './build-target';
9
+ import { HMR_DIR, HMR_JSONP } from './hmr-config';
10
+
11
+ export function createChainConfig(
12
+ esmx: Esmx,
13
+ buildTarget: BuildTarget,
14
+ options: RspackAppOptions
15
+ ): RspackChain {
16
+ const isHot = buildTarget === 'client' && !esmx.isProd;
17
+ const isClient = buildTarget === 'client';
18
+ const isServer = buildTarget === 'server';
19
+ const isNode = buildTarget === 'node';
20
+
21
+ const config = new RspackChain();
22
+
23
+ config.context(esmx.root);
24
+ config.mode(esmx.isProd ? 'production' : 'development');
25
+ config.target(isClient ? 'web' : 'node24');
26
+ config.cache(!esmx.isProd);
27
+
28
+ config.output
29
+ .clean(esmx.isProd)
30
+ .filename(
31
+ !isNode && esmx.isProd
32
+ ? 'exports/[name].[contenthash:8].final.mjs'
33
+ : 'exports/[name].mjs'
34
+ )
35
+ .chunkFilename(
36
+ esmx.isProd
37
+ ? 'chunks/[name].[contenthash:8].final.mjs'
38
+ : 'chunks/[name].mjs'
39
+ )
40
+ .publicPath(
41
+ isClient ? 'auto' : `${esmx.basePathPlaceholder}${esmx.basePath}`
42
+ )
43
+ .uniqueName(esmx.varName)
44
+ .hotUpdateGlobal(HMR_JSONP)
45
+ .chunkLoadingGlobal(`${HMR_JSONP}_chunk`)
46
+ .hotUpdateChunkFilename(`${HMR_DIR}/[id].[fullhash].hot-update.mjs`)
47
+ .hotUpdateMainFilename(
48
+ `${HMR_DIR}/[runtime].[fullhash].hot-update.json`
49
+ );
50
+
51
+ config.output.set(
52
+ 'cssFilename',
53
+ esmx.isProd
54
+ ? 'exports/[name].[contenthash:8].final.css'
55
+ : 'exports/[name].css'
56
+ );
57
+ config.output.set(
58
+ 'cssChunkFilename',
59
+ esmx.isProd
60
+ ? 'chunks/[name].[contenthash:8].final.css'
61
+ : 'chunks/[name].css'
62
+ );
63
+
64
+ const outputPath = (() => {
65
+ switch (buildTarget) {
66
+ case 'client':
67
+ return esmx.resolvePath('dist/client');
68
+ case 'server':
69
+ return esmx.resolvePath('dist/server');
70
+ case 'node':
71
+ return esmx.resolvePath('dist/node');
72
+ }
73
+ })();
74
+ config.output.path(outputPath);
75
+
76
+ config.plugin('progress').use(rspack.ProgressPlugin, [
77
+ {
78
+ prefix: buildTarget
79
+ }
80
+ ]);
81
+
82
+ config
83
+ .plugin('module-link')
84
+ .use(moduleLinkPlugin, [createModuleLinkConfig(esmx, buildTarget)]);
85
+
86
+ if (isHot) {
87
+ config.plugin('hmr').use(rspack.HotModuleReplacementPlugin);
88
+ }
89
+
90
+ config.module.parser.set('javascript', {
91
+ dynamicImportMode: 'lazy',
92
+ url: isClient ? true : 'relative'
93
+ });
94
+
95
+ config.module.generator.set('asset', {
96
+ emit: isClient
97
+ });
98
+
99
+ config.module.generator.set('asset/resource', {
100
+ emit: isClient
101
+ });
102
+
103
+ config.resolve.alias.set(esmx.name, esmx.root);
104
+
105
+ config.optimization
106
+ .minimize(options.minimize ?? esmx.isProd)
107
+ .emitOnErrors(true);
108
+
109
+ config.externalsPresets({
110
+ web: isClient,
111
+ node: isServer || isNode
112
+ });
113
+ config.externalsType('module-import');
114
+
115
+ if (isNode) {
116
+ config.externals([
117
+ // @ts-ignore
118
+ nodeExternals({
119
+ // @ts-ignore
120
+ importType: 'module-import'
121
+ })
122
+ ]);
123
+ }
124
+
125
+ // Temporary fix for development environment
126
+ // Related issue: https://github.com/esmnext/esmx/issues/109
127
+ // TODO: Remove when Rspack officially supports these features
128
+ if (!esmx.isProd) {
129
+ config.optimization.splitChunks(false).runtimeChunk(false);
130
+ config.module.parser.set('javascript', {
131
+ ...config.module.parser.get('javascript'),
132
+ dynamicImportMode: 'eager'
133
+ });
134
+ }
135
+
136
+ return config;
137
+ }
138
+
139
+ function createModuleLinkConfig(esmx: Esmx, buildTarget: BuildTarget) {
140
+ const isClient = buildTarget === 'client';
141
+ const isServer = buildTarget === 'server';
142
+ const isNode = buildTarget === 'node';
143
+
144
+ if (isNode) {
145
+ return {
146
+ name: esmx.name,
147
+ exports: {
148
+ 'src/entry.node': {
149
+ rewrite: false,
150
+ file: './src/entry.node'
151
+ }
152
+ }
153
+ };
154
+ }
155
+
156
+ const exports: Record<string, { rewrite: boolean; file: string }> = {};
157
+ for (const [name, item] of Object.entries(esmx.moduleConfig.exports)) {
158
+ if (item.inputTarget[buildTarget]) {
159
+ exports[name] = {
160
+ rewrite: item.rewrite,
161
+ file: item.inputTarget[buildTarget]
162
+ };
163
+ }
164
+ }
165
+
166
+ const preEntries: string[] = [];
167
+ if (isClient && !esmx.isProd) {
168
+ preEntries.push(
169
+ `${import.meta.resolve('webpack-hot-middleware/client.js')}?path=/${esmx.name}/hot-middleware`
170
+ );
171
+ }
172
+
173
+ return {
174
+ name: esmx.name,
175
+ injectChunkName: isServer,
176
+ imports: esmx.moduleConfig.imports,
177
+ deps: Object.keys(esmx.moduleConfig.links),
178
+ exports,
179
+ preEntries
180
+ };
181
+ }
182
+
183
+ export function createRspackConfig(
184
+ esmx: Esmx,
185
+ buildTarget: BuildTarget,
186
+ options: RspackAppOptions
187
+ ): RspackOptions {
188
+ const chain = createChainConfig(esmx, buildTarget, options);
189
+ options.chain?.({ esmx, options, buildTarget, chain });
190
+ const config = chain.toConfig();
191
+ options.config?.({ esmx, options, buildTarget, config });
192
+ return config;
193
+ }
@@ -0,0 +1,8 @@
1
+ export {
2
+ type RspackAppConfigContext,
3
+ type RspackAppChainContext,
4
+ type RspackAppOptions,
5
+ createRspackApp
6
+ } from './app';
7
+ export type { BuildTarget } from './build-target';
8
+ export { RSPACK_LOADER } from './loader';