ptech-preset 1.2.8 → 1.3.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.
Files changed (2) hide show
  1. package/dist/index.js +84 -37
  2. 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": { singleton: true, eager: true, requiredVersion: false },
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": { singleton: true, eager: true, requiredVersion: false }
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(`[mf-auto] fetch types failed for ${scope}: ${res.status} ${res.statusText}`);
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(path3.join(typesRootAbs, "index.d.ts"), banner + refs + os.EOL, "utf8");
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,39 +239,43 @@ async function devFetchRemoteTypesOnce(params) {
225
239
  "utf8"
226
240
  );
227
241
  }
228
- function stripJsonComments(json) {
229
- return json.replace(/^\uFEFF/, "").replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
230
- }
231
242
  function writePatchedTsconfig(opts) {
232
- const { root, baseTsconfigPath, typesFolder, remoteAliases } = opts;
243
+ const { root, baseTsconfigPath, typesFolder, remoteAliases, baseDir } = opts;
244
+ const outDir = path3.resolve(root, ".mf-auto");
245
+ fs3.mkdirSync(outDir, { recursive: true });
233
246
  const baseAbs = path3.resolve(root, baseTsconfigPath);
234
247
  let base = {};
235
248
  try {
236
- const raw = fs3.readFileSync(baseAbs, "utf8");
237
- base = JSON.parse(stripJsonComments(raw));
249
+ const raw = fs3.readFileSync(baseAbs, "utf8").replace(/^\uFEFF/, "").replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
250
+ base = JSON.parse(raw);
238
251
  } catch (e) {
239
252
  console.warn("[plugin-mf-auto] Cannot read base tsconfig, using minimal:", e);
240
253
  base = {};
241
254
  }
242
255
  const compilerOptions = { ...base.compilerOptions ?? {} };
256
+ const rootDirRel = path3.relative(outDir, root).replace(/\\/g, "/") || ".";
257
+ const baseDirRel = path3.relative(outDir, path3.resolve(root, baseDir)).replace(/\\/g, "/");
258
+ const typesDirRel = path3.relative(outDir, path3.resolve(root, typesFolder)).replace(/\\/g, "/");
259
+ compilerOptions.rootDir = compilerOptions.rootDir ?? rootDirRel;
260
+ compilerOptions.baseUrl = compilerOptions.baseUrl ?? rootDirRel;
243
261
  const paths = { ...compilerOptions.paths ?? {} };
244
262
  for (const scope of remoteAliases) {
245
263
  const key = `${scope}/*`;
246
- const val = `./${typesFolder}/${scope}/*`;
264
+ const val = `${typesDirRel}/${scope}/*`;
247
265
  const arr = Array.isArray(paths[key]) ? paths[key] : paths[key] ? [paths[key]] : [];
248
266
  if (!arr.includes(val)) arr.push(val);
249
267
  paths[key] = arr;
250
268
  }
251
269
  compilerOptions.paths = paths;
252
270
  const tr = new Set(Array.isArray(compilerOptions.typeRoots) ? compilerOptions.typeRoots : []);
253
- if (![...tr].some((s) => /node_modules\/@types$/.test(s))) tr.add("./node_modules/@types");
271
+ if (![...tr].some((s) => /node_modules\/@types$/.test(s))) tr.add(`${rootDirRel}/node_modules/@types`);
254
272
  compilerOptions.typeRoots = Array.from(tr);
255
273
  base.compilerOptions = compilerOptions;
256
274
  const include = new Set(Array.isArray(base.include) ? base.include : []);
257
- include.add(`./${typesFolder}/**/*`);
275
+ if (baseDirRel) include.add(`${baseDirRel}/**/*`);
276
+ include.add(`${typesDirRel}/**/*`);
277
+ include.add("./**/*");
258
278
  base.include = Array.from(include);
259
- const outDir = path3.resolve(root, ".mf-auto");
260
- fs3.mkdirSync(outDir, { recursive: true });
261
279
  const outPath = path3.join(outDir, "tsconfig.for-dts.json");
262
280
  fs3.writeFileSync(outPath, JSON.stringify(base, null, 2), "utf8");
263
281
  return outPath;
@@ -280,15 +298,19 @@ function pluginCore(opts) {
280
298
  mf
281
299
  } = opts;
282
300
  if (!mf || typeof mf !== "object") {
283
- throw new Error('[plugin-mf-auto] "mf" options is required and must be an object.');
301
+ throw new Error(
302
+ '[plugin-mf-auto] "mf" options is required and must be an object.'
303
+ );
284
304
  }
285
305
  return {
286
306
  name: "plugin-mf-auto",
287
307
  async setup(api) {
288
308
  const { pluginModuleFederation } = await import("@module-federation/rsbuild-plugin");
289
309
  const getExposes = async () => {
290
- if (exposesMode === "jsdoc") return collectAutoExposes({ baseDir, globs });
291
- if (exposesMode === "wrapper") return collectAutoExposesWithWrapper({ baseDir, globs });
310
+ if (exposesMode === "jsdoc")
311
+ return collectAutoExposes({ baseDir, globs });
312
+ if (exposesMode === "wrapper")
313
+ return collectAutoExposesWithWrapper({ baseDir, globs });
292
314
  const a = await collectAutoExposes({ baseDir, globs });
293
315
  const b = await collectAutoExposesWithWrapper({ baseDir, globs });
294
316
  return { ...a, ...b };
@@ -331,11 +353,22 @@ function pluginCore(opts) {
331
353
  fs3.mkdirSync(tempDir, { recursive: true });
332
354
  if (cssInjection === "wrapper" && mfMerged.exposes) {
333
355
  const wrapped = {};
334
- for (const [key, rel] of Object.entries(mfMerged.exposes)) {
335
- const targetAbs = path3.resolve(root, String(rel).replace(/^\.\//, ""));
336
- const proxyFile = path3.join(tempDir, `expose_${key.replace(/[./]/g, "_")}.ts`);
356
+ for (const [key, rel] of Object.entries(
357
+ mfMerged.exposes
358
+ )) {
359
+ const targetAbs = path3.resolve(
360
+ root,
361
+ String(rel).replace(/^\.\//, "")
362
+ );
363
+ const proxyFile = path3.join(
364
+ tempDir,
365
+ `expose_${key.replace(/[./]/g, "_")}.ts`
366
+ );
337
367
  const lines = [];
338
- if (hasCss) lines.push(`import ${JSON.stringify(cssAbs.replace(/\\/g, "/"))};`);
368
+ if (hasCss)
369
+ lines.push(
370
+ `import ${JSON.stringify(cssAbs.replace(/\\/g, "/"))};`
371
+ );
339
372
  else lines.push(`/* no css: ${cssEntry} not found */`);
340
373
  const target = JSON.stringify(targetAbs.replace(/\\/g, "/"));
341
374
  lines.push(`export * from ${target};`);
@@ -351,12 +384,17 @@ function pluginCore(opts) {
351
384
  const stylesFile = path3.join(tempDir, `styles_expose.ts`);
352
385
  fs3.writeFileSync(
353
386
  stylesFile,
354
- `import ${JSON.stringify(cssAbs.replace(/\\/g, "/"))};
387
+ `import ${JSON.stringify(
388
+ cssAbs.replace(/\\/g, "/")
389
+ )};
355
390
  // generated by plugin-mf-auto`,
356
391
  "utf8"
357
392
  );
358
393
  const relFromRoot = path3.relative(root, stylesFile).replace(/\\/g, "/");
359
- mfMerged.exposes = { ...mfMerged.exposes ?? {}, [stylesExposeKey]: `./${relFromRoot}` };
394
+ mfMerged.exposes = {
395
+ ...mfMerged.exposes ?? {},
396
+ [stylesExposeKey]: `./${relFromRoot}`
397
+ };
360
398
  }
361
399
  }
362
400
  const command = api?.context?.command;
@@ -367,12 +405,19 @@ function pluginCore(opts) {
367
405
  const baseTsconfigPath = "./tsconfig.json";
368
406
  const tsconfigForDtsAbs = writePatchedTsconfig({
369
407
  root,
370
- baseTsconfigPath,
408
+ baseTsconfigPath: "./tsconfig.json",
371
409
  typesFolder,
372
- remoteAliases
410
+ remoteAliases: Object.keys(autoTypeUrls),
411
+ baseDir
373
412
  });
374
413
  const overrideTsCfg = process.env.MF_DTS_TSCONFIG;
375
414
  const tsconfigForDtsRel = (overrideTsCfg ? overrideTsCfg : path3.relative(root, tsconfigForDtsAbs)).replace(/\\/g, "/");
415
+ const dtsObj = ensureDtsObject(mfMerged.dts);
416
+ dtsObj.generateTypes = {
417
+ ...dtsObj.generateTypes ?? {},
418
+ tsConfigPath: tsconfigForDtsRel
419
+ };
420
+ mfMerged.dts = dtsObj;
376
421
  if (isDev) {
377
422
  const refresh = process.env[devTypesRefreshEnvVar] === "1";
378
423
  const typesRootAbs = path3.resolve(root, typesFolder);
@@ -388,31 +433,31 @@ function pluginCore(opts) {
388
433
  urls: autoTypeUrls
389
434
  });
390
435
  }
391
- const dtsObj = ensureDtsObject(mfMerged.dts);
392
- dtsObj.generateTypes = {
393
- ...dtsObj.generateTypes ?? {},
436
+ const dtsObj2 = ensureDtsObject(mfMerged.dts);
437
+ dtsObj2.generateTypes = {
438
+ ...dtsObj2.generateTypes ?? {},
394
439
  tsConfigPath: tsconfigForDtsRel
395
440
  };
396
- mfMerged.dts = dtsObj;
441
+ mfMerged.dts = dtsObj2;
397
442
  } else {
398
443
  if (mfMerged.dts !== false) {
399
- const dtsObj = ensureDtsObject(mfMerged.dts);
444
+ const dtsObj2 = ensureDtsObject(mfMerged.dts);
400
445
  if (Object.keys(autoTypeUrls).length > 0) {
401
- const existingConsume = dtsObj.consumeTypes ?? {};
446
+ const existingConsume = dtsObj2.consumeTypes ?? {};
402
447
  const existingRt = existingConsume.remoteTypeUrls ?? {};
403
- dtsObj.consumeTypes = {
448
+ dtsObj2.consumeTypes = {
404
449
  typesFolder: existingConsume.typesFolder ?? typesFolder,
405
450
  maxRetries: existingConsume.maxRetries ?? 3,
406
451
  // ưu tiên config hiện hữu nếu trùng key
407
452
  remoteTypeUrls: { ...autoTypeUrls, ...existingRt }
408
453
  };
409
454
  }
410
- dtsObj.generateTypes = {
411
- ...dtsObj.generateTypes ?? {},
455
+ dtsObj2.generateTypes = {
456
+ ...dtsObj2.generateTypes ?? {},
412
457
  tsConfigPath: tsconfigForDtsRel
413
458
  // dùng tsconfig đã patch từ tsconfig của app
414
459
  };
415
- mfMerged.dts = dtsObj;
460
+ mfMerged.dts = dtsObj2;
416
461
  }
417
462
  }
418
463
  const mfPlugin = pluginModuleFederation(mfMerged);
@@ -431,7 +476,9 @@ function pluginCore(opts) {
431
476
  });
432
477
  if (separateExposes && mfMerged.exposes) {
433
478
  const force = {};
434
- for (const [key, rel] of Object.entries(mfMerged.exposes)) {
479
+ for (const [key, rel] of Object.entries(
480
+ mfMerged.exposes
481
+ )) {
435
482
  const abs = path3.resolve(root, String(rel).replace(/^\.\//, "")).replace(/\\/g, "/");
436
483
  const safe = separateExposeChunkPrefix + key.replace(/[^a-zA-Z0-9_]/g, "_");
437
484
  force[safe] = new RegExp(`${escapeRegExp(abs)}$`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ptech-preset",
3
- "version": "1.2.8",
3
+ "version": "1.3.1",
4
4
  "description": "Auto Module.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",