@lark-apaas/fullstack-rspack-preset 0.1.2-alpha.4 → 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/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.4",
3
+ "version": "0.1.2-alpha.5",
4
4
  "files": [
5
5
  "lib"
6
6
  ],