ptech-preset 1.1.8 → 1.2.1
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/dist/index.d.ts +2 -6
- package/dist/index.js +41 -42
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -7,18 +7,14 @@ type MfAutoOptions = {
|
|
|
7
7
|
exposesMode?: "jsdoc" | "wrapper" | "both";
|
|
8
8
|
envPrefix?: string;
|
|
9
9
|
manifestPathOrUrl?: string;
|
|
10
|
-
/** true: chỉ auto khi exposes/remotes trống; false: luôn auto rồi merge */
|
|
11
10
|
autoWhenEmpty?: boolean;
|
|
12
|
-
/** Tiêm CSS vào exposes */
|
|
13
11
|
cssInjection?: CssInjectionMode;
|
|
14
12
|
cssEntry?: string;
|
|
15
13
|
stylesExposeKey?: string;
|
|
16
|
-
/** Ép tách chunk cho từng expose */
|
|
17
14
|
separateExposes?: boolean;
|
|
18
15
|
separateExposeChunkPrefix?: string;
|
|
19
|
-
/** options chuyển nguyên vẹn sang @module-federation/rsbuild-plugin */
|
|
20
16
|
mf: Record<string, any>;
|
|
21
17
|
};
|
|
22
|
-
declare function
|
|
18
|
+
declare function pluginCore(opts: MfAutoOptions): RsbuildPlugin;
|
|
23
19
|
|
|
24
|
-
export { type MfAutoOptions,
|
|
20
|
+
export { type MfAutoOptions, pluginCore as default, pluginCore };
|
package/dist/index.js
CHANGED
|
@@ -10,7 +10,6 @@ import fg from "fast-glob";
|
|
|
10
10
|
async function collectAutoExposes(opts = {}) {
|
|
11
11
|
if (opts.enabled === false) return {};
|
|
12
12
|
const globs = opts.globs ?? ["src/components/**/*.{ts,tsx}"];
|
|
13
|
-
const baseDir = path.resolve(process.cwd(), opts.baseDir ?? "src");
|
|
14
13
|
const tag = opts.tag ?? "expose";
|
|
15
14
|
const debug = !!opts.debug;
|
|
16
15
|
const files = await fg(globs, {
|
|
@@ -23,34 +22,34 @@ async function collectAutoExposes(opts = {}) {
|
|
|
23
22
|
"m"
|
|
24
23
|
);
|
|
25
24
|
const exposes = {};
|
|
25
|
+
const root = process.cwd();
|
|
26
26
|
for (const abs of files) {
|
|
27
27
|
const text = fs.readFileSync(abs, "utf8");
|
|
28
28
|
const m = text.match(jsdocRe);
|
|
29
29
|
if (!m) continue;
|
|
30
30
|
const raw = (m[1] || "").trim();
|
|
31
31
|
const key = raw || path.basename(abs).replace(/\.(tsx?|jsx?)$/, "");
|
|
32
|
-
const
|
|
33
|
-
exposes[`./${key}`] = `./${
|
|
32
|
+
const relFromRoot = path.relative(root, abs).replace(/\\/g, "/");
|
|
33
|
+
exposes[`./${key}`] = `./${relFromRoot}`;
|
|
34
34
|
if (debug) {
|
|
35
|
-
console.log(`[mf-auto] JSDoc expose: key="./${key}" -> ${
|
|
35
|
+
console.log(`[mf-auto] JSDoc expose: key="./${key}" -> ${relFromRoot}`);
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
if (debug) {
|
|
39
|
-
|
|
40
|
-
console.log(`[mf-auto] collectAutoExposes: ${count} expose(s) found`);
|
|
39
|
+
console.log(`[mf-auto] collectAutoExposes: ${Object.keys(exposes).length} expose(s) found`);
|
|
41
40
|
}
|
|
42
41
|
return exposes;
|
|
43
42
|
}
|
|
44
43
|
async function collectAutoExposesWithWrapper(opts = {}) {
|
|
45
44
|
if (opts.enabled === false) return {};
|
|
46
45
|
const globs = opts.globs ?? ["src/components/**/*.{ts,tsx}"];
|
|
47
|
-
const baseDir = path.resolve(process.cwd(), opts.baseDir ?? "src");
|
|
48
46
|
const debug = !!opts.debug;
|
|
49
47
|
const files = await fg(globs, {
|
|
50
48
|
cwd: process.cwd(),
|
|
51
49
|
absolute: true,
|
|
52
50
|
ignore: ["**/*.d.ts"]
|
|
53
51
|
});
|
|
52
|
+
const root = process.cwd();
|
|
54
53
|
const jsdocRe = new RegExp(
|
|
55
54
|
String.raw`/\*\*[\s\S]*?@expose(?:\s+([^\s*]+))?[\s\S]*?\*/`,
|
|
56
55
|
"m"
|
|
@@ -67,18 +66,15 @@ async function collectAutoExposesWithWrapper(opts = {}) {
|
|
|
67
66
|
const wrapperMatch = text.match(wrapperRe);
|
|
68
67
|
if (wrapperMatch) key = wrapperMatch[1];
|
|
69
68
|
}
|
|
70
|
-
if (!key)
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
const rel = path.relative(baseDir, abs).replace(/\\/g, "/");
|
|
74
|
-
exposes[`./${key}`] = `./${rel}`;
|
|
69
|
+
if (!key) continue;
|
|
70
|
+
const relFromRoot = path.relative(root, abs).replace(/\\/g, "/");
|
|
71
|
+
exposes[`./${key}`] = `./${relFromRoot}`;
|
|
75
72
|
if (debug) {
|
|
76
|
-
console.log(`[mf-auto] Wrapper/JSDoc expose: key="./${key}" -> ${
|
|
73
|
+
console.log(`[mf-auto] Wrapper/JSDoc expose: key="./${key}" -> ${relFromRoot}`);
|
|
77
74
|
}
|
|
78
75
|
}
|
|
79
76
|
if (debug) {
|
|
80
|
-
|
|
81
|
-
console.log(`[mf-auto] collectAutoExposesWithWrapper: ${count} expose(s) found`);
|
|
77
|
+
console.log(`[mf-auto] collectAutoExposesWithWrapper: ${Object.keys(exposes).length} expose(s) found`);
|
|
82
78
|
}
|
|
83
79
|
return exposes;
|
|
84
80
|
}
|
|
@@ -150,7 +146,15 @@ function normalizeExposePath(root, maybeRel) {
|
|
|
150
146
|
return `./${relFromRoot}`;
|
|
151
147
|
}
|
|
152
148
|
var escapeRegExp = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
153
|
-
|
|
149
|
+
var DEFAULT_SHARED = {
|
|
150
|
+
react: { singleton: true, eager: true, requiredVersion: false },
|
|
151
|
+
"react-dom": { singleton: true, eager: true, requiredVersion: false },
|
|
152
|
+
"react-router": { singleton: true, eager: true, requiredVersion: false },
|
|
153
|
+
"@azure/msal-react": { singleton: true, eager: true, requiredVersion: false },
|
|
154
|
+
"@azure/msal-browser": { singleton: true, eager: true, requiredVersion: false }
|
|
155
|
+
};
|
|
156
|
+
var CDN_BASE = "https://oneportal.blob.core.windows.net/external/ts/";
|
|
157
|
+
function pluginCore(opts) {
|
|
154
158
|
const {
|
|
155
159
|
baseDir = "src",
|
|
156
160
|
globs = ["src/components/**/*.{ts,tsx}"],
|
|
@@ -166,9 +170,7 @@ function pluginMFAuto(opts) {
|
|
|
166
170
|
mf
|
|
167
171
|
} = opts;
|
|
168
172
|
if (!mf || typeof mf !== "object") {
|
|
169
|
-
throw new Error(
|
|
170
|
-
'[plugin-mf-auto] "mf" options is required and must be an object.'
|
|
171
|
-
);
|
|
173
|
+
throw new Error('[plugin-mf-auto] "mf" options is required and must be an object.');
|
|
172
174
|
}
|
|
173
175
|
return {
|
|
174
176
|
name: "plugin-mf-auto",
|
|
@@ -177,8 +179,7 @@ function pluginMFAuto(opts) {
|
|
|
177
179
|
const { pluginModuleFederation } = await import("@module-federation/rsbuild-plugin");
|
|
178
180
|
const getExposes = async () => {
|
|
179
181
|
if (exposesMode === "jsdoc") return collectAutoExposes({ baseDir, globs });
|
|
180
|
-
if (exposesMode === "wrapper")
|
|
181
|
-
return collectAutoExposesWithWrapper({ baseDir, globs });
|
|
182
|
+
if (exposesMode === "wrapper") return collectAutoExposesWithWrapper({ baseDir, globs });
|
|
182
183
|
const a = await collectAutoExposes({ baseDir, globs });
|
|
183
184
|
const b = await collectAutoExposesWithWrapper({ baseDir, globs });
|
|
184
185
|
return { ...a, ...b };
|
|
@@ -211,6 +212,9 @@ function pluginMFAuto(opts) {
|
|
|
211
212
|
if (!mfMerged.name) {
|
|
212
213
|
mfMerged.name = getPackageName(root) || path3.basename(root).replace(/\W+/g, "");
|
|
213
214
|
}
|
|
215
|
+
if (typeof mfMerged.shared === "undefined") {
|
|
216
|
+
mfMerged.shared = DEFAULT_SHARED;
|
|
217
|
+
}
|
|
214
218
|
if (cssInjection !== "none") {
|
|
215
219
|
const cssAbs = path3.resolve(root, cssEntry);
|
|
216
220
|
const hasCss = fs3.existsSync(cssAbs);
|
|
@@ -218,20 +222,12 @@ function pluginMFAuto(opts) {
|
|
|
218
222
|
fs3.mkdirSync(tempDir, { recursive: true });
|
|
219
223
|
if (cssInjection === "wrapper" && mfMerged.exposes) {
|
|
220
224
|
const wrapped = {};
|
|
221
|
-
for (const [key, rel] of Object.entries(
|
|
222
|
-
mfMerged.exposes
|
|
223
|
-
)) {
|
|
225
|
+
for (const [key, rel] of Object.entries(mfMerged.exposes)) {
|
|
224
226
|
const targetAbs = path3.resolve(root, String(rel).replace(/^\.\//, ""));
|
|
225
|
-
const proxyFile = path3.join(
|
|
226
|
-
tempDir,
|
|
227
|
-
`expose_${key.replace(/[./]/g, "_")}.ts`
|
|
228
|
-
);
|
|
227
|
+
const proxyFile = path3.join(tempDir, `expose_${key.replace(/[./]/g, "_")}.ts`);
|
|
229
228
|
const lines = [];
|
|
230
|
-
if (hasCss) {
|
|
231
|
-
|
|
232
|
-
} else {
|
|
233
|
-
lines.push(`/* no css: ${cssEntry} not found */`);
|
|
234
|
-
}
|
|
229
|
+
if (hasCss) lines.push(`import ${JSON.stringify(cssAbs.replace(/\\/g, "/"))};`);
|
|
230
|
+
else lines.push(`/* no css: ${cssEntry} not found */`);
|
|
235
231
|
const target = JSON.stringify(targetAbs.replace(/\\/g, "/"));
|
|
236
232
|
lines.push(`export * from ${target};`);
|
|
237
233
|
lines.push(`export { default } from ${target};`);
|
|
@@ -251,20 +247,23 @@ function pluginMFAuto(opts) {
|
|
|
251
247
|
"utf8"
|
|
252
248
|
);
|
|
253
249
|
const relFromRoot = path3.relative(root, stylesFile).replace(/\\/g, "/");
|
|
254
|
-
mfMerged.exposes = {
|
|
255
|
-
...mfMerged.exposes ?? {},
|
|
256
|
-
[stylesExposeKey]: `./${relFromRoot}`
|
|
257
|
-
};
|
|
250
|
+
mfMerged.exposes = { ...mfMerged.exposes ?? {}, [stylesExposeKey]: `./${relFromRoot}` };
|
|
258
251
|
}
|
|
259
252
|
}
|
|
260
253
|
if (typeof mfMerged.dts === "undefined") mfMerged.dts = true;
|
|
261
254
|
const mfPlugin = pluginModuleFederation(mfMerged);
|
|
262
255
|
await mfPlugin.setup?.(api);
|
|
256
|
+
const pkgSafe = getPackageName(root) || mfMerged.name || path3.basename(root).replace(/\W+/g, "");
|
|
257
|
+
const cdnUrl = `${CDN_BASE}${pkgSafe}/`;
|
|
258
|
+
const isProd = process.env.NODE_ENV === "production";
|
|
259
|
+
api.modifyRsbuildConfig((config) => {
|
|
260
|
+
config.output ??= {};
|
|
261
|
+
if (isProd && !config.output.assetPrefix) config.output.assetPrefix = cdnUrl;
|
|
262
|
+
return config;
|
|
263
|
+
});
|
|
263
264
|
if (separateExposes && mfMerged.exposes) {
|
|
264
265
|
const force = {};
|
|
265
|
-
for (const [key, rel] of Object.entries(
|
|
266
|
-
mfMerged.exposes
|
|
267
|
-
)) {
|
|
266
|
+
for (const [key, rel] of Object.entries(mfMerged.exposes)) {
|
|
268
267
|
const abs = path3.resolve(root, String(rel).replace(/^\.\//, "")).replace(/\\/g, "/");
|
|
269
268
|
const safe = separateExposeChunkPrefix + key.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
270
269
|
force[safe] = new RegExp(`${escapeRegExp(abs)}$`);
|
|
@@ -279,8 +278,8 @@ function pluginMFAuto(opts) {
|
|
|
279
278
|
}
|
|
280
279
|
};
|
|
281
280
|
}
|
|
282
|
-
var index_default =
|
|
281
|
+
var index_default = pluginCore;
|
|
283
282
|
export {
|
|
284
283
|
index_default as default,
|
|
285
|
-
|
|
284
|
+
pluginCore
|
|
286
285
|
};
|