ptech-preset 1.2.7 → 1.2.9
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.js +128 -31
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -150,10 +150,18 @@ function normalizeExposePath(root, maybeRel) {
|
|
|
150
150
|
var escapeRegExp = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
151
151
|
var DEFAULT_SHARED = {
|
|
152
152
|
react: { singleton: true, eager: true, requiredVersion: false },
|
|
153
|
-
"react-dom": {
|
|
153
|
+
"react-dom": {
|
|
154
|
+
singleton: true,
|
|
155
|
+
eager: true,
|
|
156
|
+
requiredVersion: false
|
|
157
|
+
},
|
|
154
158
|
"react-router": { singleton: true, eager: true, requiredVersion: false },
|
|
155
159
|
"@azure/msal-react": { singleton: true, eager: true, requiredVersion: false },
|
|
156
|
-
"@azure/msal-browser": {
|
|
160
|
+
"@azure/msal-browser": {
|
|
161
|
+
singleton: true,
|
|
162
|
+
eager: true,
|
|
163
|
+
requiredVersion: false
|
|
164
|
+
}
|
|
157
165
|
};
|
|
158
166
|
var CDN_BASE = "https://oneportal.blob.core.windows.net/external/ts/";
|
|
159
167
|
function parseRemoteSpec(key, val) {
|
|
@@ -201,7 +209,9 @@ async function devFetchRemoteTypesOnce(params) {
|
|
|
201
209
|
try {
|
|
202
210
|
const res = await fetch(spec.api);
|
|
203
211
|
if (!res.ok) {
|
|
204
|
-
console.warn(
|
|
212
|
+
console.warn(
|
|
213
|
+
`[mf-auto] fetch types failed for ${scope}: ${res.status} ${res.statusText}`
|
|
214
|
+
);
|
|
205
215
|
continue;
|
|
206
216
|
}
|
|
207
217
|
const txt = await res.text();
|
|
@@ -213,7 +223,11 @@ async function devFetchRemoteTypesOnce(params) {
|
|
|
213
223
|
const refs = Object.keys(urls).map((s) => `/// <reference path="./__remotes/${s}.d.ts" />`).join(os.EOL);
|
|
214
224
|
const banner = `// generated by plugin-mf-auto (dev once)
|
|
215
225
|
`;
|
|
216
|
-
fs3.writeFileSync(
|
|
226
|
+
fs3.writeFileSync(
|
|
227
|
+
path3.join(typesRootAbs, "index.d.ts"),
|
|
228
|
+
banner + refs + os.EOL,
|
|
229
|
+
"utf8"
|
|
230
|
+
);
|
|
217
231
|
const shimDir = path3.join(root, baseDir, ".mf-auto");
|
|
218
232
|
fs3.mkdirSync(shimDir, { recursive: true });
|
|
219
233
|
const relToIndex = path3.relative(shimDir, path3.join(typesRootAbs, "index.d.ts")).replace(/\\/g, "/");
|
|
@@ -225,6 +239,55 @@ async function devFetchRemoteTypesOnce(params) {
|
|
|
225
239
|
"utf8"
|
|
226
240
|
);
|
|
227
241
|
}
|
|
242
|
+
function stripJsonComments(json) {
|
|
243
|
+
return json.replace(/^\uFEFF/, "").replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
|
|
244
|
+
}
|
|
245
|
+
function writePatchedTsconfig(opts) {
|
|
246
|
+
const { root, baseTsconfigPath, typesFolder, remoteAliases, baseDir } = opts;
|
|
247
|
+
const baseAbs = path3.resolve(root, baseTsconfigPath);
|
|
248
|
+
let base = {};
|
|
249
|
+
try {
|
|
250
|
+
const raw = fs3.readFileSync(baseAbs, "utf8");
|
|
251
|
+
base = JSON.parse(stripJsonComments(raw));
|
|
252
|
+
} catch (e) {
|
|
253
|
+
console.warn(
|
|
254
|
+
"[plugin-mf-auto] Cannot read base tsconfig, using minimal:",
|
|
255
|
+
e
|
|
256
|
+
);
|
|
257
|
+
base = {};
|
|
258
|
+
}
|
|
259
|
+
const compilerOptions = { ...base.compilerOptions ?? {} };
|
|
260
|
+
const rootDirFromEnv = process.env.MF_DTS_ROOTDIR;
|
|
261
|
+
compilerOptions.rootDir = compilerOptions.rootDir ?? rootDirFromEnv ?? ".";
|
|
262
|
+
compilerOptions.baseUrl = compilerOptions.baseUrl ?? ".";
|
|
263
|
+
const paths = { ...compilerOptions.paths ?? {} };
|
|
264
|
+
for (const scope of remoteAliases) {
|
|
265
|
+
const key = `${scope}/*`;
|
|
266
|
+
const val = `./${typesFolder}/${scope}/*`;
|
|
267
|
+
const arr = Array.isArray(paths[key]) ? paths[key] : paths[key] ? [paths[key]] : [];
|
|
268
|
+
if (!arr.includes(val)) arr.push(val);
|
|
269
|
+
paths[key] = arr;
|
|
270
|
+
}
|
|
271
|
+
compilerOptions.paths = paths;
|
|
272
|
+
const tr = new Set(
|
|
273
|
+
Array.isArray(compilerOptions.typeRoots) ? compilerOptions.typeRoots : []
|
|
274
|
+
);
|
|
275
|
+
if (![...tr].some((s) => /node_modules\/@types$/.test(s)))
|
|
276
|
+
tr.add("./node_modules/@types");
|
|
277
|
+
compilerOptions.typeRoots = Array.from(tr);
|
|
278
|
+
base.compilerOptions = compilerOptions;
|
|
279
|
+
const include = new Set(
|
|
280
|
+
Array.isArray(base.include) ? base.include : []
|
|
281
|
+
);
|
|
282
|
+
include.add(`./${baseDir}/**/*`);
|
|
283
|
+
include.add(`./${typesFolder}/**/*`);
|
|
284
|
+
base.include = Array.from(include);
|
|
285
|
+
const outDir = path3.resolve(root, ".mf-auto");
|
|
286
|
+
fs3.mkdirSync(outDir, { recursive: true });
|
|
287
|
+
const outPath = path3.join(outDir, "tsconfig.for-dts.json");
|
|
288
|
+
fs3.writeFileSync(outPath, JSON.stringify(base, null, 2), "utf8");
|
|
289
|
+
return outPath;
|
|
290
|
+
}
|
|
228
291
|
function pluginCore(opts) {
|
|
229
292
|
const {
|
|
230
293
|
baseDir = "src",
|
|
@@ -243,15 +306,19 @@ function pluginCore(opts) {
|
|
|
243
306
|
mf
|
|
244
307
|
} = opts;
|
|
245
308
|
if (!mf || typeof mf !== "object") {
|
|
246
|
-
throw new Error(
|
|
309
|
+
throw new Error(
|
|
310
|
+
'[plugin-mf-auto] "mf" options is required and must be an object.'
|
|
311
|
+
);
|
|
247
312
|
}
|
|
248
313
|
return {
|
|
249
314
|
name: "plugin-mf-auto",
|
|
250
315
|
async setup(api) {
|
|
251
316
|
const { pluginModuleFederation } = await import("@module-federation/rsbuild-plugin");
|
|
252
317
|
const getExposes = async () => {
|
|
253
|
-
if (exposesMode === "jsdoc")
|
|
254
|
-
|
|
318
|
+
if (exposesMode === "jsdoc")
|
|
319
|
+
return collectAutoExposes({ baseDir, globs });
|
|
320
|
+
if (exposesMode === "wrapper")
|
|
321
|
+
return collectAutoExposesWithWrapper({ baseDir, globs });
|
|
255
322
|
const a = await collectAutoExposes({ baseDir, globs });
|
|
256
323
|
const b = await collectAutoExposesWithWrapper({ baseDir, globs });
|
|
257
324
|
return { ...a, ...b };
|
|
@@ -294,11 +361,22 @@ function pluginCore(opts) {
|
|
|
294
361
|
fs3.mkdirSync(tempDir, { recursive: true });
|
|
295
362
|
if (cssInjection === "wrapper" && mfMerged.exposes) {
|
|
296
363
|
const wrapped = {};
|
|
297
|
-
for (const [key, rel] of Object.entries(
|
|
298
|
-
|
|
299
|
-
|
|
364
|
+
for (const [key, rel] of Object.entries(
|
|
365
|
+
mfMerged.exposes
|
|
366
|
+
)) {
|
|
367
|
+
const targetAbs = path3.resolve(
|
|
368
|
+
root,
|
|
369
|
+
String(rel).replace(/^\.\//, "")
|
|
370
|
+
);
|
|
371
|
+
const proxyFile = path3.join(
|
|
372
|
+
tempDir,
|
|
373
|
+
`expose_${key.replace(/[./]/g, "_")}.ts`
|
|
374
|
+
);
|
|
300
375
|
const lines = [];
|
|
301
|
-
if (hasCss)
|
|
376
|
+
if (hasCss)
|
|
377
|
+
lines.push(
|
|
378
|
+
`import ${JSON.stringify(cssAbs.replace(/\\/g, "/"))};`
|
|
379
|
+
);
|
|
302
380
|
else lines.push(`/* no css: ${cssEntry} not found */`);
|
|
303
381
|
const target = JSON.stringify(targetAbs.replace(/\\/g, "/"));
|
|
304
382
|
lines.push(`export * from ${target};`);
|
|
@@ -314,18 +392,34 @@ function pluginCore(opts) {
|
|
|
314
392
|
const stylesFile = path3.join(tempDir, `styles_expose.ts`);
|
|
315
393
|
fs3.writeFileSync(
|
|
316
394
|
stylesFile,
|
|
317
|
-
`import ${JSON.stringify(
|
|
395
|
+
`import ${JSON.stringify(
|
|
396
|
+
cssAbs.replace(/\\/g, "/")
|
|
397
|
+
)};
|
|
318
398
|
// generated by plugin-mf-auto`,
|
|
319
399
|
"utf8"
|
|
320
400
|
);
|
|
321
401
|
const relFromRoot = path3.relative(root, stylesFile).replace(/\\/g, "/");
|
|
322
|
-
mfMerged.exposes = {
|
|
402
|
+
mfMerged.exposes = {
|
|
403
|
+
...mfMerged.exposes ?? {},
|
|
404
|
+
[stylesExposeKey]: `./${relFromRoot}`
|
|
405
|
+
};
|
|
323
406
|
}
|
|
324
407
|
}
|
|
325
408
|
const command = api?.context?.command;
|
|
326
409
|
const isProdLike = command === "build" || process.env.NODE_ENV === "production";
|
|
327
410
|
const isDev = !isProdLike;
|
|
328
411
|
const autoTypeUrls = buildRemoteTypeUrls(mfMerged.remotes || {});
|
|
412
|
+
const remoteAliases = Object.keys(autoTypeUrls);
|
|
413
|
+
const baseTsconfigPath = "./tsconfig.json";
|
|
414
|
+
const tsconfigForDtsAbs = writePatchedTsconfig({
|
|
415
|
+
root,
|
|
416
|
+
baseTsconfigPath,
|
|
417
|
+
typesFolder,
|
|
418
|
+
remoteAliases,
|
|
419
|
+
baseDir
|
|
420
|
+
});
|
|
421
|
+
const overrideTsCfg = process.env.MF_DTS_TSCONFIG;
|
|
422
|
+
const tsconfigForDtsRel = (overrideTsCfg ? overrideTsCfg : path3.relative(root, tsconfigForDtsAbs)).replace(/\\/g, "/");
|
|
329
423
|
if (isDev) {
|
|
330
424
|
const refresh = process.env[devTypesRefreshEnvVar] === "1";
|
|
331
425
|
const typesRootAbs = path3.resolve(root, typesFolder);
|
|
@@ -333,23 +427,20 @@ function pluginCore(opts) {
|
|
|
333
427
|
const needFetch = refresh || !fs3.existsSync(indexFile) || Object.keys(autoTypeUrls).some(
|
|
334
428
|
(s) => !fs3.existsSync(path3.join(typesRootAbs, "__remotes", `${s}.d.ts`))
|
|
335
429
|
);
|
|
336
|
-
if (Object.keys(autoTypeUrls).length > 0) {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
});
|
|
344
|
-
}
|
|
345
|
-
const dtsObj = ensureDtsObject(mfMerged.dts);
|
|
346
|
-
dtsObj.generateTypes = dtsObj.generateTypes ?? { tsConfigPath: "./tsconfig.json" };
|
|
347
|
-
mfMerged.dts = dtsObj;
|
|
348
|
-
} else {
|
|
349
|
-
const dtsObj = ensureDtsObject(mfMerged.dts);
|
|
350
|
-
dtsObj.generateTypes = dtsObj.generateTypes ?? { tsConfigPath: "./tsconfig.json" };
|
|
351
|
-
mfMerged.dts = dtsObj;
|
|
430
|
+
if (Object.keys(autoTypeUrls).length > 0 && needFetch) {
|
|
431
|
+
await devFetchRemoteTypesOnce({
|
|
432
|
+
root,
|
|
433
|
+
baseDir,
|
|
434
|
+
typesFolderRel: typesFolder,
|
|
435
|
+
urls: autoTypeUrls
|
|
436
|
+
});
|
|
352
437
|
}
|
|
438
|
+
const dtsObj = ensureDtsObject(mfMerged.dts);
|
|
439
|
+
dtsObj.generateTypes = {
|
|
440
|
+
...dtsObj.generateTypes ?? {},
|
|
441
|
+
tsConfigPath: tsconfigForDtsRel
|
|
442
|
+
};
|
|
443
|
+
mfMerged.dts = dtsObj;
|
|
353
444
|
} else {
|
|
354
445
|
if (mfMerged.dts !== false) {
|
|
355
446
|
const dtsObj = ensureDtsObject(mfMerged.dts);
|
|
@@ -363,7 +454,11 @@ function pluginCore(opts) {
|
|
|
363
454
|
remoteTypeUrls: { ...autoTypeUrls, ...existingRt }
|
|
364
455
|
};
|
|
365
456
|
}
|
|
366
|
-
dtsObj.generateTypes =
|
|
457
|
+
dtsObj.generateTypes = {
|
|
458
|
+
...dtsObj.generateTypes ?? {},
|
|
459
|
+
tsConfigPath: tsconfigForDtsRel
|
|
460
|
+
// dùng tsconfig đã patch từ tsconfig của app
|
|
461
|
+
};
|
|
367
462
|
mfMerged.dts = dtsObj;
|
|
368
463
|
}
|
|
369
464
|
}
|
|
@@ -383,7 +478,9 @@ function pluginCore(opts) {
|
|
|
383
478
|
});
|
|
384
479
|
if (separateExposes && mfMerged.exposes) {
|
|
385
480
|
const force = {};
|
|
386
|
-
for (const [key, rel] of Object.entries(
|
|
481
|
+
for (const [key, rel] of Object.entries(
|
|
482
|
+
mfMerged.exposes
|
|
483
|
+
)) {
|
|
387
484
|
const abs = path3.resolve(root, String(rel).replace(/^\.\//, "")).replace(/\\/g, "/");
|
|
388
485
|
const safe = separateExposeChunkPrefix + key.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
389
486
|
force[safe] = new RegExp(`${escapeRegExp(abs)}$`);
|