@lark-apaas/fullstack-rspack-preset 0.1.2-alpha.3 → 0.1.2-alpha.5

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.
package/README.md CHANGED
@@ -32,9 +32,9 @@ yarn add @lark-apaas/fullstack-rspack-preset
32
32
  ```javascript
33
33
  // rspack.config.js
34
34
  const path = require('path');
35
- const { createFullstackRspackConfig } = require('@lark-apaas/fullstack-rspack-preset');
35
+ const { defineConfig } = require('@lark-apaas/fullstack-rspack-preset');
36
36
 
37
- module.exports = createFullstackRspackConfig({
37
+ module.exports = defineConfig({
38
38
  entry: {
39
39
  main: './client/src/index.tsx',
40
40
  },
@@ -80,11 +80,16 @@ NODE_ENV=production npx rspack build --config rspack.config.js
80
80
 
81
81
  ## API 文档
82
82
 
83
- ### createFullstackRspackConfig (简化版)
83
+ ### createFullstackRspackConfig / defineConfig(推荐)
84
84
 
85
85
  自动从环境变量读取配置,用户只需提供覆盖配置。
86
86
 
87
87
  ```typescript
88
+ function defineConfig(
89
+ overrides?: Partial<Configuration>
90
+ ): Configuration;
91
+
92
+ // 别名:createFullstackRspackConfig(向后兼容)
88
93
  function createFullstackRspackConfig(
89
94
  overrides?: Partial<Configuration>
90
95
  ): Configuration;
@@ -234,9 +239,9 @@ module.exports = defineConfig(({mode}) => {
234
239
 
235
240
  ```javascript
236
241
  const path = require('path');
237
- const { createFullstackRspackConfig } = require('@lark-apaas/fullstack-rspack-preset');
242
+ const { defineConfig } = require('@lark-apaas/fullstack-rspack-preset');
238
243
 
239
- module.exports = createFullstackRspackConfig({
244
+ module.exports = defineConfig({
240
245
  entry: {
241
246
  main: './client/src/index.tsx',
242
247
  },
@@ -250,7 +255,7 @@ module.exports = createFullstackRspackConfig({
250
255
 
251
256
  **迁移步骤:**
252
257
 
253
- 1. 修改导入:`createRecommendRspackConfig` → `createFullstackRspackConfig`
258
+ 1. 修改导入:`createRecommendRspackConfig` → `defineConfig`
254
259
  2. 删除手动环境变量处理代码
255
260
  3. 删除 `defineConfig`、`normalizeBasePath`、`merge` 等导入
256
261
  4. 只保留覆盖配置(entry、resolve 等)
@@ -275,7 +280,10 @@ ENABLE_REACT_REFRESH=false
275
280
  直接在 `overrides` 中添加:
276
281
 
277
282
  ```javascript
278
- module.exports = createFullstackRspackConfig({
283
+ const { defineConfig } = require('@lark-apaas/fullstack-rspack-preset');
284
+ const rspack = require('@rspack/core');
285
+
286
+ module.exports = defineConfig({
279
287
  plugins: [
280
288
  new rspack.DefinePlugin({
281
289
  'process.env.CUSTOM_VAR': JSON.stringify('custom-value'),
@@ -302,6 +310,11 @@ ASSETS_CDN_PATH=https://cdn.example.com
302
310
 
303
311
  ## 版本历史
304
312
 
313
+ ### 0.1.2-alpha.3
314
+ - ✨ 新增 `defineConfig` API(对齐 rspack 官方)
315
+ - ✨ 保持 `createFullstackRspackConfig` 向后兼容
316
+ - 🐛 修复 .env 异步加载问题
317
+
305
318
  ### 0.1.2-alpha.2
306
319
  - ✨ 新增 `createFullstackRspackConfig` 简化 API
307
320
  - ✨ 自动加载 `.env` 环境变量
package/lib/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { Configuration } from '@rspack/core';
2
2
  /**
3
- * 创建 Fullstack Rspack 配置(简化版)
3
+ * 定义 Fullstack Rspack 配置
4
4
  *
5
5
  * 自动从环境变量读取配置,用户只需提供覆盖配置
6
6
  *
@@ -9,9 +9,9 @@ import type { Configuration } from '@rspack/core';
9
9
  *
10
10
  * @example
11
11
  * ```javascript
12
- * const { createFullstackRspackConfig } = require('@lark-apaas/fullstack-rspack-preset');
12
+ * const { defineConfig } = require('@lark-apaas/fullstack-rspack-preset');
13
13
  *
14
- * module.exports = createFullstackRspackConfig({
14
+ * module.exports = defineConfig({
15
15
  * entry: {
16
16
  * main: './client/src/index.tsx',
17
17
  * },
@@ -34,4 +34,5 @@ import type { Configuration } from '@rspack/core';
34
34
  * - CLIENT_DEV_PORT: 客户端开发端口 (默认: 8080)
35
35
  */
36
36
  export declare function createFullstackRspackConfig(overrides?: Partial<Configuration>): Configuration;
37
+ export { createFullstackRspackConfig as defineConfig };
37
38
  export default createFullstackRspackConfig;
package/lib/index.js CHANGED
@@ -1,48 +1,12 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- var __importDefault = (this && this.__importDefault) || function (mod) {
36
- return (mod && mod.__esModule) ? mod : { "default": mod };
37
- };
38
2
  Object.defineProperty(exports, "__esModule", { value: true });
39
3
  exports.createFullstackRspackConfig = createFullstackRspackConfig;
4
+ exports.defineConfig = createFullstackRspackConfig;
40
5
  const webpack_merge_1 = require("webpack-merge");
41
6
  const devtool_kits_1 = require("@lark-apaas/devtool-kits");
42
- const path_1 = __importDefault(require("path"));
43
7
  const preset_1 = require("./preset");
44
8
  /**
45
- * 创建 Fullstack Rspack 配置(简化版)
9
+ * 定义 Fullstack Rspack 配置
46
10
  *
47
11
  * 自动从环境变量读取配置,用户只需提供覆盖配置
48
12
  *
@@ -51,9 +15,9 @@ const preset_1 = require("./preset");
51
15
  *
52
16
  * @example
53
17
  * ```javascript
54
- * const { createFullstackRspackConfig } = require('@lark-apaas/fullstack-rspack-preset');
18
+ * const { defineConfig } = require('@lark-apaas/fullstack-rspack-preset');
55
19
  *
56
- * module.exports = createFullstackRspackConfig({
20
+ * module.exports = defineConfig({
57
21
  * entry: {
58
22
  * main: './client/src/index.tsx',
59
23
  * },
@@ -76,11 +40,10 @@ const preset_1 = require("./preset");
76
40
  * - CLIENT_DEV_PORT: 客户端开发端口 (默认: 8080)
77
41
  */
78
42
  function createFullstackRspackConfig(overrides = {}) {
79
- // 1. 自动加载 .env 文件
43
+ // 1. 自动加载 .env 文件(同步加载,确保环境变量立即可用)
80
44
  try {
81
- Promise.resolve().then(() => __importStar(require('dotenv'))).then((dotenv) => dotenv.config({
82
- path: path_1.default.resolve(process.cwd(), '.env'),
83
- }));
45
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
46
+ require('dotenv').config();
84
47
  }
85
48
  catch (e) {
86
49
  // dotenv 是可选依赖,如果没有安装也不影响使用
@@ -108,5 +71,5 @@ function createFullstackRspackConfig(overrides = {}) {
108
71
  // 5. 深度合并用户配置
109
72
  return (0, webpack_merge_1.merge)(baseConfig, overrides);
110
73
  }
111
- // 默认导出简化版 API
74
+ // 默认导出
112
75
  exports.default = createFullstackRspackConfig;
package/lib/preset.js CHANGED
@@ -11,6 +11,7 @@ const plugin_react_refresh_1 = __importDefault(require("@rspack/plugin-react-ref
11
11
  const dev_server_listener_1 = require("./utils/dev-server-listener");
12
12
  const route_parser_plugin_1 = __importDefault(require("./rspack-plugins/route-parser-plugin"));
13
13
  const slardar_performance_monitor_plugin_1 = __importDefault(require("./rspack-plugins/slardar-performance-monitor-plugin"));
14
+ const view_context_injection_plugin_1 = __importDefault(require("./rspack-plugins/view-context-injection-plugin"));
14
15
  function createRecommendRspackConfig(options) {
15
16
  const { isDev = true, enableReactRefresh = isDev, needRoutes = true, clientBasePath = '', publicPath = '', // 静态资源路径
16
17
  } = options;
@@ -152,6 +153,8 @@ function createRecommendRspackConfig(options) {
152
153
  }),
153
154
  // 性能监控插件
154
155
  new slardar_performance_monitor_plugin_1.default(),
156
+ // 视图上下文注入插件
157
+ new view_context_injection_plugin_1.default(),
155
158
  // 开发环境下,解析路由
156
159
  isDev && needRoutes &&
157
160
  new route_parser_plugin_1.default({
@@ -0,0 +1,9 @@
1
+ interface ViewContextInjectionPluginOptions {
2
+ position?: 'head-end' | 'body-start' | 'body-end';
3
+ }
4
+ declare class ViewContextInjectionPlugin {
5
+ private options;
6
+ constructor(options?: ViewContextInjectionPluginOptions);
7
+ apply(compiler: any): void;
8
+ }
9
+ export default ViewContextInjectionPlugin;
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class ViewContextInjectionPlugin {
4
+ constructor(options) {
5
+ this.options = options || {};
6
+ }
7
+ apply(compiler) {
8
+ compiler.hooks.compilation.tap('ViewContextInjectionPlugin', (compilation) => {
9
+ try {
10
+ // 从 compiler.webpack 或 compiler.rspack 获取 HtmlRspackPlugin
11
+ // 这样可以避免在 npm link 环境下的模块解析问题
12
+ let HtmlPlugin;
13
+ try {
14
+ // 尝试从 compiler 中获取 rspack
15
+ const rspack = compiler.webpack || compiler.rspack || compiler.constructor.webpack;
16
+ if (rspack && rspack.HtmlRspackPlugin) {
17
+ HtmlPlugin = rspack.HtmlRspackPlugin;
18
+ }
19
+ else {
20
+ // 降级到 require,但这在 npm link 下可能失败
21
+ try {
22
+ HtmlPlugin = require('html-webpack-plugin');
23
+ }
24
+ catch (e2) {
25
+ console.warn('ViewContextInjectionPlugin: HtmlRspackPlugin not found');
26
+ return;
27
+ }
28
+ }
29
+ }
30
+ catch (e) {
31
+ console.warn('ViewContextInjectionPlugin: Failed to get HtmlRspackPlugin');
32
+ return;
33
+ }
34
+ const hooks = HtmlPlugin.getHooks(compilation);
35
+ hooks.beforeEmit.tapAsync('ViewContextInjectionPlugin', (data, cb) => {
36
+ const position = this.options.position ?? 'head-end';
37
+ const snippet = getViewContextScript();
38
+ if (position === 'body-end') {
39
+ data.html = data.html.replace('</body>', `${snippet}\n</body>`);
40
+ }
41
+ else if (position === 'body-start') {
42
+ data.html = data.html.replace('<body>', `<body>\n${snippet}`);
43
+ }
44
+ else if (position === 'head-end') {
45
+ data.html = data.html.replace('</head>', `${snippet}\n</head>`);
46
+ }
47
+ cb(null, data);
48
+ });
49
+ }
50
+ catch (error) {
51
+ console.error('Error in ViewContextInjectionPlugin:', error);
52
+ }
53
+ });
54
+ }
55
+ }
56
+ exports.default = ViewContextInjectionPlugin;
57
+ function getViewContextScript() {
58
+ return ` <script>
59
+ window.csrfToken = "{{csrfToken}}";
60
+ window.userId = "{{userId}}";
61
+ window.tenantId = "{{tenantId}}";
62
+ window.appId = "{{appId}}";
63
+ </script>`;
64
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/fullstack-rspack-preset",
3
- "version": "0.1.2-alpha.3",
3
+ "version": "0.1.2-alpha.5",
4
4
  "files": [
5
5
  "lib"
6
6
  ],