ptech-preset 1.2.9 → 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 +35 -37
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -239,51 +239,43 @@ async function devFetchRemoteTypesOnce(params) {
239
239
  "utf8"
240
240
  );
241
241
  }
242
- function stripJsonComments(json) {
243
- return json.replace(/^\uFEFF/, "").replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
244
- }
245
242
  function writePatchedTsconfig(opts) {
246
243
  const { root, baseTsconfigPath, typesFolder, remoteAliases, baseDir } = opts;
244
+ const outDir = path3.resolve(root, ".mf-auto");
245
+ fs3.mkdirSync(outDir, { recursive: true });
247
246
  const baseAbs = path3.resolve(root, baseTsconfigPath);
248
247
  let base = {};
249
248
  try {
250
- const raw = fs3.readFileSync(baseAbs, "utf8");
251
- 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);
252
251
  } catch (e) {
253
- console.warn(
254
- "[plugin-mf-auto] Cannot read base tsconfig, using minimal:",
255
- e
256
- );
252
+ console.warn("[plugin-mf-auto] Cannot read base tsconfig, using minimal:", e);
257
253
  base = {};
258
254
  }
259
255
  const compilerOptions = { ...base.compilerOptions ?? {} };
260
- const rootDirFromEnv = process.env.MF_DTS_ROOTDIR;
261
- compilerOptions.rootDir = compilerOptions.rootDir ?? rootDirFromEnv ?? ".";
262
- compilerOptions.baseUrl = compilerOptions.baseUrl ?? ".";
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;
263
261
  const paths = { ...compilerOptions.paths ?? {} };
264
262
  for (const scope of remoteAliases) {
265
263
  const key = `${scope}/*`;
266
- const val = `./${typesFolder}/${scope}/*`;
264
+ const val = `${typesDirRel}/${scope}/*`;
267
265
  const arr = Array.isArray(paths[key]) ? paths[key] : paths[key] ? [paths[key]] : [];
268
266
  if (!arr.includes(val)) arr.push(val);
269
267
  paths[key] = arr;
270
268
  }
271
269
  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");
270
+ const tr = new Set(Array.isArray(compilerOptions.typeRoots) ? compilerOptions.typeRoots : []);
271
+ if (![...tr].some((s) => /node_modules\/@types$/.test(s))) tr.add(`${rootDirRel}/node_modules/@types`);
277
272
  compilerOptions.typeRoots = Array.from(tr);
278
273
  base.compilerOptions = compilerOptions;
279
- const include = new Set(
280
- Array.isArray(base.include) ? base.include : []
281
- );
282
- include.add(`./${baseDir}/**/*`);
283
- include.add(`./${typesFolder}/**/*`);
274
+ const include = new Set(Array.isArray(base.include) ? base.include : []);
275
+ if (baseDirRel) include.add(`${baseDirRel}/**/*`);
276
+ include.add(`${typesDirRel}/**/*`);
277
+ include.add("./**/*");
284
278
  base.include = Array.from(include);
285
- const outDir = path3.resolve(root, ".mf-auto");
286
- fs3.mkdirSync(outDir, { recursive: true });
287
279
  const outPath = path3.join(outDir, "tsconfig.for-dts.json");
288
280
  fs3.writeFileSync(outPath, JSON.stringify(base, null, 2), "utf8");
289
281
  return outPath;
@@ -413,13 +405,19 @@ function pluginCore(opts) {
413
405
  const baseTsconfigPath = "./tsconfig.json";
414
406
  const tsconfigForDtsAbs = writePatchedTsconfig({
415
407
  root,
416
- baseTsconfigPath,
408
+ baseTsconfigPath: "./tsconfig.json",
417
409
  typesFolder,
418
- remoteAliases,
410
+ remoteAliases: Object.keys(autoTypeUrls),
419
411
  baseDir
420
412
  });
421
413
  const overrideTsCfg = process.env.MF_DTS_TSCONFIG;
422
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;
423
421
  if (isDev) {
424
422
  const refresh = process.env[devTypesRefreshEnvVar] === "1";
425
423
  const typesRootAbs = path3.resolve(root, typesFolder);
@@ -435,31 +433,31 @@ function pluginCore(opts) {
435
433
  urls: autoTypeUrls
436
434
  });
437
435
  }
438
- const dtsObj = ensureDtsObject(mfMerged.dts);
439
- dtsObj.generateTypes = {
440
- ...dtsObj.generateTypes ?? {},
436
+ const dtsObj2 = ensureDtsObject(mfMerged.dts);
437
+ dtsObj2.generateTypes = {
438
+ ...dtsObj2.generateTypes ?? {},
441
439
  tsConfigPath: tsconfigForDtsRel
442
440
  };
443
- mfMerged.dts = dtsObj;
441
+ mfMerged.dts = dtsObj2;
444
442
  } else {
445
443
  if (mfMerged.dts !== false) {
446
- const dtsObj = ensureDtsObject(mfMerged.dts);
444
+ const dtsObj2 = ensureDtsObject(mfMerged.dts);
447
445
  if (Object.keys(autoTypeUrls).length > 0) {
448
- const existingConsume = dtsObj.consumeTypes ?? {};
446
+ const existingConsume = dtsObj2.consumeTypes ?? {};
449
447
  const existingRt = existingConsume.remoteTypeUrls ?? {};
450
- dtsObj.consumeTypes = {
448
+ dtsObj2.consumeTypes = {
451
449
  typesFolder: existingConsume.typesFolder ?? typesFolder,
452
450
  maxRetries: existingConsume.maxRetries ?? 3,
453
451
  // ưu tiên config hiện hữu nếu trùng key
454
452
  remoteTypeUrls: { ...autoTypeUrls, ...existingRt }
455
453
  };
456
454
  }
457
- dtsObj.generateTypes = {
458
- ...dtsObj.generateTypes ?? {},
455
+ dtsObj2.generateTypes = {
456
+ ...dtsObj2.generateTypes ?? {},
459
457
  tsConfigPath: tsconfigForDtsRel
460
458
  // dùng tsconfig đã patch từ tsconfig của app
461
459
  };
462
- mfMerged.dts = dtsObj;
460
+ mfMerged.dts = dtsObj2;
463
461
  }
464
462
  }
465
463
  const mfPlugin = pluginModuleFederation(mfMerged);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ptech-preset",
3
- "version": "1.2.9",
3
+ "version": "1.3.1",
4
4
  "description": "Auto Module.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",