@lark-apaas/fullstack-rspack-preset 1.0.55-alpha.2 → 1.0.55-alpha.20
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 +4 -0
- package/lib/rspack-plugins/basename-injection-loader.d.ts +2 -0
- package/lib/rspack-plugins/basename-injection-loader.js +23 -0
- package/lib/rspack-plugins/basename-injection-plugin.d.ts +27 -0
- package/lib/rspack-plugins/basename-injection-plugin.js +56 -0
- package/lib/rspack-plugins/source-map-upload-plugin.js +16 -10
- package/lib/rspack-plugins/view-context-injection-plugin.js +1 -0
- package/package.json +2 -2
package/lib/preset.js
CHANGED
|
@@ -15,6 +15,7 @@ const slardar_performance_monitor_plugin_1 = __importDefault(require("./rspack-p
|
|
|
15
15
|
const view_context_injection_plugin_1 = __importDefault(require("./rspack-plugins/view-context-injection-plugin"));
|
|
16
16
|
const og_meta_injection_plugin_1 = __importDefault(require("./rspack-plugins/og-meta-injection-plugin"));
|
|
17
17
|
const runtime_injection_plugin_1 = __importDefault(require("./rspack-plugins/runtime-injection-plugin"));
|
|
18
|
+
const basename_injection_plugin_1 = __importDefault(require("./rspack-plugins/basename-injection-plugin"));
|
|
18
19
|
const css_legacy_plugin_1 = __importDefault(require("./rspack-plugins/css-legacy-plugin"));
|
|
19
20
|
const polyfill_plugin_1 = __importDefault(require("./rspack-plugins/polyfill-plugin"));
|
|
20
21
|
const static_assets_plugin_1 = __importDefault(require("./rspack-plugins/static-assets-plugin"));
|
|
@@ -193,6 +194,9 @@ function createRecommendRspackConfig(options) {
|
|
|
193
194
|
plugins: [
|
|
194
195
|
// 运行时注入插件 - 自动将 @lark-apaas/client-toolkit/runtime 注入到所有入口之前
|
|
195
196
|
new runtime_injection_plugin_1.default(),
|
|
197
|
+
// basename 运行时注入 - 改写入口文件 process.env.CLIENT_BASE_PATH 为运行时读 window.__BASENAME__
|
|
198
|
+
// 必须在 DefinePlugin 之前生效(plugin 内部 enforce:'pre')
|
|
199
|
+
new basename_injection_plugin_1.default({ envBasePath: clientBasePath }),
|
|
196
200
|
new core_1.default.BannerPlugin({
|
|
197
201
|
banner: `window.__RELEASE_COMMIT_ID__ = '${releaseId}';`,
|
|
198
202
|
raw: true,
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Basename Injection Loader
|
|
4
|
+
*
|
|
5
|
+
* 仅由 BasenameInjectionPlugin 在入口文件上挂载,做字符串替换:
|
|
6
|
+
* process.env.CLIENT_BASE_PATH
|
|
7
|
+
* → ((typeof window !== "undefined" && window.__BASENAME__) || "<envBasePath>")
|
|
8
|
+
*
|
|
9
|
+
* loader 阶段天然早于 DefinePlugin 的表达式替换(rspack source-transform → parsing → DefinePlugin 流水线顺序)。
|
|
10
|
+
* 替换后的字符串里没有 process.env.X 形式,DefinePlugin 不会再二次替换。
|
|
11
|
+
*/
|
|
12
|
+
module.exports = function basenameInjectionLoader(source) {
|
|
13
|
+
const options = (typeof this.getOptions === 'function' ? this.getOptions() : this.query) || {};
|
|
14
|
+
const envBasePath = options.envBasePath || '';
|
|
15
|
+
if (typeof source !== 'string' || !source.includes('process.env.CLIENT_BASE_PATH')) {
|
|
16
|
+
return source;
|
|
17
|
+
}
|
|
18
|
+
const fallback = JSON.stringify(envBasePath);
|
|
19
|
+
const runtimeExpr = '((typeof window !== "undefined" && window.__BASENAME__) || ' +
|
|
20
|
+
fallback +
|
|
21
|
+
')';
|
|
22
|
+
return source.replace(/\bprocess\.env\.CLIENT_BASE_PATH\b/g, runtimeExpr);
|
|
23
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Basename Injection Plugin for Rspack
|
|
3
|
+
*
|
|
4
|
+
* 在 module rules 最前面注入一个 loader,仅匹配应用入口文件
|
|
5
|
+
* (client/src/index.{ts,tsx,js,jsx}),把入口里的 process.env.CLIENT_BASE_PATH
|
|
6
|
+
* 改写为运行时读 window.__BASENAME__(fallback 到构建时常量)。
|
|
7
|
+
*
|
|
8
|
+
* 让 BrowserRouter basename 运行时动态化,实现同一份 bundle 跑在不同 basename 下
|
|
9
|
+
* (默认域名 `/app/<appId>`、自定义域名 `/` 或 `/<alias>`)。
|
|
10
|
+
*
|
|
11
|
+
* 关键约束:
|
|
12
|
+
* - 只匹配入口文件,不影响 SDK 内部代码(如 client-toolkit-lite 仍读编译期常量)
|
|
13
|
+
* - loader 阶段天然早于 DefinePlugin 的表达式替换(rspack 内部 source-transform → parsing → DefinePlugin 流水线顺序)
|
|
14
|
+
* enforce:'pre' 是用来跟 babel-loader / swc-loader 等其他 loader 抢先,避免被它们先处理掉表达式
|
|
15
|
+
* - 兜底常量来自构建时 process.env.CLIENT_BASE_PATH,保证降级运行
|
|
16
|
+
*/
|
|
17
|
+
import type { Compiler } from '@rspack/core';
|
|
18
|
+
export interface BasenameInjectionPluginOptions {
|
|
19
|
+
/** 编译时兜底 basename,通常等于 process.env.CLIENT_BASE_PATH */
|
|
20
|
+
envBasePath?: string;
|
|
21
|
+
}
|
|
22
|
+
export declare class BasenameInjectionPlugin {
|
|
23
|
+
private readonly envBasePath;
|
|
24
|
+
constructor(options?: BasenameInjectionPluginOptions);
|
|
25
|
+
apply(compiler: Compiler): void;
|
|
26
|
+
}
|
|
27
|
+
export default BasenameInjectionPlugin;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Basename Injection Plugin for Rspack
|
|
4
|
+
*
|
|
5
|
+
* 在 module rules 最前面注入一个 loader,仅匹配应用入口文件
|
|
6
|
+
* (client/src/index.{ts,tsx,js,jsx}),把入口里的 process.env.CLIENT_BASE_PATH
|
|
7
|
+
* 改写为运行时读 window.__BASENAME__(fallback 到构建时常量)。
|
|
8
|
+
*
|
|
9
|
+
* 让 BrowserRouter basename 运行时动态化,实现同一份 bundle 跑在不同 basename 下
|
|
10
|
+
* (默认域名 `/app/<appId>`、自定义域名 `/` 或 `/<alias>`)。
|
|
11
|
+
*
|
|
12
|
+
* 关键约束:
|
|
13
|
+
* - 只匹配入口文件,不影响 SDK 内部代码(如 client-toolkit-lite 仍读编译期常量)
|
|
14
|
+
* - loader 阶段天然早于 DefinePlugin 的表达式替换(rspack 内部 source-transform → parsing → DefinePlugin 流水线顺序)
|
|
15
|
+
* enforce:'pre' 是用来跟 babel-loader / swc-loader 等其他 loader 抢先,避免被它们先处理掉表达式
|
|
16
|
+
* - 兜底常量来自构建时 process.env.CLIENT_BASE_PATH,保证降级运行
|
|
17
|
+
*/
|
|
18
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
19
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.BasenameInjectionPlugin = void 0;
|
|
23
|
+
const path_1 = __importDefault(require("path"));
|
|
24
|
+
const PLUGIN_NAME = 'BasenameInjectionPlugin';
|
|
25
|
+
const ENTRY_FILE_REGEX = /[/\\]client[/\\]src[/\\]index\.(tsx?|jsx?)$/;
|
|
26
|
+
class BasenameInjectionPlugin {
|
|
27
|
+
constructor(options = {}) {
|
|
28
|
+
this.envBasePath = options.envBasePath ?? process.env.CLIENT_BASE_PATH ?? '';
|
|
29
|
+
}
|
|
30
|
+
apply(compiler) {
|
|
31
|
+
const loaderPath = path_1.default.resolve(__dirname, './basename-injection-loader.js');
|
|
32
|
+
const envBasePath = this.envBasePath;
|
|
33
|
+
compiler.hooks.afterEnvironment.tap(PLUGIN_NAME, () => {
|
|
34
|
+
if (!compiler.options.module) {
|
|
35
|
+
// 极端兜底:rspack 必定提供 module,这里只是 narrow type
|
|
36
|
+
compiler.options.module = { rules: [] };
|
|
37
|
+
}
|
|
38
|
+
const rules = compiler.options.module.rules || [];
|
|
39
|
+
compiler.options.module.rules = [
|
|
40
|
+
{
|
|
41
|
+
test: ENTRY_FILE_REGEX,
|
|
42
|
+
enforce: 'pre',
|
|
43
|
+
use: [
|
|
44
|
+
{
|
|
45
|
+
loader: loaderPath,
|
|
46
|
+
options: { envBasePath },
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
...rules,
|
|
51
|
+
];
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.BasenameInjectionPlugin = BasenameInjectionPlugin;
|
|
56
|
+
exports.default = BasenameInjectionPlugin;
|
|
@@ -68,19 +68,25 @@ class SourceMapUploadPlugin {
|
|
|
68
68
|
if (!fs.existsSync(destinationDir)) {
|
|
69
69
|
fs.mkdirSync(destinationDir, { recursive: true });
|
|
70
70
|
}
|
|
71
|
-
//
|
|
72
|
-
const source = sourceMap.source.source();
|
|
73
|
-
const sourceBuffer = Buffer.isBuffer(source) ? source : Buffer.from(source, 'utf-8');
|
|
74
|
-
fs.writeFileSync(destinationPath, sourceBuffer);
|
|
75
|
-
console.log(`SourceMapUploadPlugin: Consolidated ${assetName} to ${destinationPath}`);
|
|
76
|
-
// Delete the original sourcemap file.
|
|
71
|
+
// Move the file; fallback to copy+delete on cross-filesystem (EXDEV)
|
|
77
72
|
try {
|
|
78
|
-
fs.
|
|
79
|
-
console.log(`SourceMapUploadPlugin: Deleted original sourcemap ${originalPath}`);
|
|
73
|
+
fs.renameSync(originalPath, destinationPath);
|
|
80
74
|
}
|
|
81
|
-
catch (
|
|
82
|
-
|
|
75
|
+
catch (renameError) {
|
|
76
|
+
if (renameError.code === 'EXDEV') {
|
|
77
|
+
fs.copyFileSync(originalPath, destinationPath);
|
|
78
|
+
try {
|
|
79
|
+
fs.unlinkSync(originalPath);
|
|
80
|
+
}
|
|
81
|
+
catch (deleteError) {
|
|
82
|
+
console.warn(`SourceMapUploadPlugin: Failed to delete original sourcemap ${originalPath}.`, deleteError);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
throw renameError;
|
|
87
|
+
}
|
|
83
88
|
}
|
|
89
|
+
console.log(`SourceMapUploadPlugin: Consolidated ${assetName} to ${destinationPath}`);
|
|
84
90
|
}
|
|
85
91
|
catch (error) {
|
|
86
92
|
const errorMessage = `SourceMapUploadPlugin: Failed to consolidate ${assetName}.`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lark-apaas/fullstack-rspack-preset",
|
|
3
|
-
"version": "1.0.55-alpha.
|
|
3
|
+
"version": "1.0.55-alpha.20",
|
|
4
4
|
"files": [
|
|
5
5
|
"lib",
|
|
6
6
|
"patches",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"@babel/parser": "^7.28.0",
|
|
32
32
|
"@babel/traverse": "^7.28.0",
|
|
33
33
|
"@babel/types": "^7.28.2",
|
|
34
|
-
"@lark-apaas/devtool-kits": "1.2.
|
|
34
|
+
"@lark-apaas/devtool-kits": "^1.2.23",
|
|
35
35
|
"@lark-apaas/miaoda-inspector-babel-plugin": "^1.0.2",
|
|
36
36
|
"@lark-apaas/styled-jsx": "^1.0.1",
|
|
37
37
|
"@rspack/plugin-react-refresh": "^1.5.1",
|