@lark-apaas/fullstack-rspack-preset 1.0.32 → 1.0.33-alpha.0
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.
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
interface EnvironmentInjectionPluginOptions {
|
|
2
|
+
environment?: string;
|
|
3
|
+
}
|
|
4
|
+
declare class EnvironmentInjectionPlugin {
|
|
5
|
+
private options;
|
|
6
|
+
constructor(options?: EnvironmentInjectionPluginOptions);
|
|
7
|
+
apply(compiler: unknown): void;
|
|
8
|
+
}
|
|
9
|
+
export default EnvironmentInjectionPlugin;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class EnvironmentInjectionPlugin {
|
|
4
|
+
constructor(options) {
|
|
5
|
+
this.options = options || {};
|
|
6
|
+
}
|
|
7
|
+
apply(compiler) {
|
|
8
|
+
const compilerLike = getCompilerLike(compiler);
|
|
9
|
+
if (!compilerLike)
|
|
10
|
+
return;
|
|
11
|
+
compilerLike.hooks.compilation.tap('EnvironmentInjectionPlugin', (compilation) => {
|
|
12
|
+
try {
|
|
13
|
+
const HtmlPlugin = resolveHtmlPlugin(compilerLike);
|
|
14
|
+
if (!HtmlPlugin) {
|
|
15
|
+
console.warn('EnvironmentInjectionPlugin: HtmlRspackPlugin not found');
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const hooks = HtmlPlugin.getHooks(compilation);
|
|
19
|
+
hooks.alterAssetTagGroups.tap('EnvironmentInjectionPlugin', (data) => {
|
|
20
|
+
const mapped = mapToWindowEnvironment(this.options.environment ??
|
|
21
|
+
process.env.FORCE_FRAMEWORK_ENVIRONMENT);
|
|
22
|
+
const envTag = {
|
|
23
|
+
tagName: 'script',
|
|
24
|
+
voidTag: false,
|
|
25
|
+
innerHTML: `window.ENVIRONMENT = ${JSON.stringify(mapped)};`,
|
|
26
|
+
attributes: {},
|
|
27
|
+
};
|
|
28
|
+
const headIndex = findMainJsIndex(data.headTags);
|
|
29
|
+
if (headIndex !== -1) {
|
|
30
|
+
data.headTags.splice(headIndex, 0, envTag);
|
|
31
|
+
return data;
|
|
32
|
+
}
|
|
33
|
+
const bodyIndex = findMainJsIndex(data.bodyTags);
|
|
34
|
+
if (bodyIndex !== -1) {
|
|
35
|
+
data.bodyTags.splice(bodyIndex, 0, envTag);
|
|
36
|
+
return data;
|
|
37
|
+
}
|
|
38
|
+
data.headTags.unshift(envTag);
|
|
39
|
+
return data;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
console.error('Error in EnvironmentInjectionPlugin:', error);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.default = EnvironmentInjectionPlugin;
|
|
49
|
+
function mapToWindowEnvironment(input) {
|
|
50
|
+
const value = (input || '').trim().toLowerCase();
|
|
51
|
+
if (value === 'boe')
|
|
52
|
+
return 'staging';
|
|
53
|
+
if (value === 'pre')
|
|
54
|
+
return 'gray';
|
|
55
|
+
if (value === 'prod')
|
|
56
|
+
return 'online';
|
|
57
|
+
return '';
|
|
58
|
+
}
|
|
59
|
+
function findMainJsIndex(tags) {
|
|
60
|
+
if (!Array.isArray(tags))
|
|
61
|
+
return -1;
|
|
62
|
+
return tags.findIndex((tag) => {
|
|
63
|
+
if (!tag || tag.tagName !== 'script')
|
|
64
|
+
return false;
|
|
65
|
+
const src = tag.attributes?.src;
|
|
66
|
+
return typeof src === 'string' && src.includes('main.js');
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
function getCompilerLike(input) {
|
|
70
|
+
if (!input || typeof input !== 'object')
|
|
71
|
+
return;
|
|
72
|
+
const hooks = input.hooks;
|
|
73
|
+
if (!hooks || typeof hooks !== 'object')
|
|
74
|
+
return;
|
|
75
|
+
const compilation = hooks.compilation;
|
|
76
|
+
if (!compilation || typeof compilation !== 'object')
|
|
77
|
+
return;
|
|
78
|
+
const tap = compilation.tap;
|
|
79
|
+
if (typeof tap !== 'function')
|
|
80
|
+
return;
|
|
81
|
+
return input;
|
|
82
|
+
}
|
|
83
|
+
function resolveHtmlPlugin(compiler) {
|
|
84
|
+
const fromWebpack = getHtmlPluginLike(compiler.webpack?.HtmlRspackPlugin);
|
|
85
|
+
if (fromWebpack)
|
|
86
|
+
return fromWebpack;
|
|
87
|
+
const fromRspack = getHtmlPluginLike(compiler.rspack?.HtmlRspackPlugin);
|
|
88
|
+
if (fromRspack)
|
|
89
|
+
return fromRspack;
|
|
90
|
+
const fromCtor = getHtmlPluginLike(compiler.constructor?.webpack?.HtmlRspackPlugin);
|
|
91
|
+
if (fromCtor)
|
|
92
|
+
return fromCtor;
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
function getHtmlPluginLike(value) {
|
|
96
|
+
if (!value)
|
|
97
|
+
return;
|
|
98
|
+
const type = typeof value;
|
|
99
|
+
if (type !== 'function' && type !== 'object')
|
|
100
|
+
return;
|
|
101
|
+
const getHooks = value.getHooks;
|
|
102
|
+
if (typeof getHooks !== 'function')
|
|
103
|
+
return;
|
|
104
|
+
return value;
|
|
105
|
+
}
|
|
@@ -65,7 +65,9 @@ class ViewContextInjectionPlugin {
|
|
|
65
65
|
}
|
|
66
66
|
exports.default = ViewContextInjectionPlugin;
|
|
67
67
|
function getViewContextScriptContent() {
|
|
68
|
+
const windowEnvironment = mapToWindowEnvironment(process.env.FORCE_FRAMEWORK_ENVIRONMENT);
|
|
68
69
|
return `
|
|
70
|
+
window.ENVIRONMENT = ${JSON.stringify(windowEnvironment)};
|
|
69
71
|
window.csrfToken = "{{csrfToken}}";
|
|
70
72
|
window.userId = "{{userId}}";
|
|
71
73
|
window.tenantId = "{{tenantId}}";
|
|
@@ -80,3 +82,13 @@ function getViewContextScriptContent() {
|
|
|
80
82
|
}
|
|
81
83
|
`;
|
|
82
84
|
}
|
|
85
|
+
function mapToWindowEnvironment(input) {
|
|
86
|
+
const value = (input || '').trim().toLowerCase();
|
|
87
|
+
if (value === 'boe')
|
|
88
|
+
return 'staging';
|
|
89
|
+
if (value === 'pre')
|
|
90
|
+
return 'gray';
|
|
91
|
+
if (value === 'prod')
|
|
92
|
+
return 'online';
|
|
93
|
+
return '';
|
|
94
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lark-apaas/fullstack-rspack-preset",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.33-alpha.0",
|
|
4
4
|
"files": [
|
|
5
5
|
"lib",
|
|
6
6
|
"patches",
|
|
@@ -56,6 +56,5 @@
|
|
|
56
56
|
"@rspack/core": "^1.5.5",
|
|
57
57
|
"react": ">=16.14.0",
|
|
58
58
|
"react-dom": ">=16.14.0"
|
|
59
|
-
}
|
|
60
|
-
"gitHead": "c4a62d1944234bf2ccc11d51a99be7857c4446d8"
|
|
59
|
+
}
|
|
61
60
|
}
|