ptech-preset 1.2.7 → 1.2.8

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 +65 -17
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -225,6 +225,43 @@ async function devFetchRemoteTypesOnce(params) {
225
225
  "utf8"
226
226
  );
227
227
  }
228
+ function stripJsonComments(json) {
229
+ return json.replace(/^\uFEFF/, "").replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
230
+ }
231
+ function writePatchedTsconfig(opts) {
232
+ const { root, baseTsconfigPath, typesFolder, remoteAliases } = opts;
233
+ const baseAbs = path3.resolve(root, baseTsconfigPath);
234
+ let base = {};
235
+ try {
236
+ const raw = fs3.readFileSync(baseAbs, "utf8");
237
+ base = JSON.parse(stripJsonComments(raw));
238
+ } catch (e) {
239
+ console.warn("[plugin-mf-auto] Cannot read base tsconfig, using minimal:", e);
240
+ base = {};
241
+ }
242
+ const compilerOptions = { ...base.compilerOptions ?? {} };
243
+ const paths = { ...compilerOptions.paths ?? {} };
244
+ for (const scope of remoteAliases) {
245
+ const key = `${scope}/*`;
246
+ const val = `./${typesFolder}/${scope}/*`;
247
+ const arr = Array.isArray(paths[key]) ? paths[key] : paths[key] ? [paths[key]] : [];
248
+ if (!arr.includes(val)) arr.push(val);
249
+ paths[key] = arr;
250
+ }
251
+ compilerOptions.paths = paths;
252
+ 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");
254
+ compilerOptions.typeRoots = Array.from(tr);
255
+ base.compilerOptions = compilerOptions;
256
+ const include = new Set(Array.isArray(base.include) ? base.include : []);
257
+ include.add(`./${typesFolder}/**/*`);
258
+ base.include = Array.from(include);
259
+ const outDir = path3.resolve(root, ".mf-auto");
260
+ fs3.mkdirSync(outDir, { recursive: true });
261
+ const outPath = path3.join(outDir, "tsconfig.for-dts.json");
262
+ fs3.writeFileSync(outPath, JSON.stringify(base, null, 2), "utf8");
263
+ return outPath;
264
+ }
228
265
  function pluginCore(opts) {
229
266
  const {
230
267
  baseDir = "src",
@@ -326,6 +363,16 @@ function pluginCore(opts) {
326
363
  const isProdLike = command === "build" || process.env.NODE_ENV === "production";
327
364
  const isDev = !isProdLike;
328
365
  const autoTypeUrls = buildRemoteTypeUrls(mfMerged.remotes || {});
366
+ const remoteAliases = Object.keys(autoTypeUrls);
367
+ const baseTsconfigPath = "./tsconfig.json";
368
+ const tsconfigForDtsAbs = writePatchedTsconfig({
369
+ root,
370
+ baseTsconfigPath,
371
+ typesFolder,
372
+ remoteAliases
373
+ });
374
+ const overrideTsCfg = process.env.MF_DTS_TSCONFIG;
375
+ const tsconfigForDtsRel = (overrideTsCfg ? overrideTsCfg : path3.relative(root, tsconfigForDtsAbs)).replace(/\\/g, "/");
329
376
  if (isDev) {
330
377
  const refresh = process.env[devTypesRefreshEnvVar] === "1";
331
378
  const typesRootAbs = path3.resolve(root, typesFolder);
@@ -333,23 +380,20 @@ function pluginCore(opts) {
333
380
  const needFetch = refresh || !fs3.existsSync(indexFile) || Object.keys(autoTypeUrls).some(
334
381
  (s) => !fs3.existsSync(path3.join(typesRootAbs, "__remotes", `${s}.d.ts`))
335
382
  );
336
- if (Object.keys(autoTypeUrls).length > 0) {
337
- if (needFetch) {
338
- await devFetchRemoteTypesOnce({
339
- root,
340
- baseDir,
341
- typesFolderRel: typesFolder,
342
- urls: autoTypeUrls
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;
383
+ if (Object.keys(autoTypeUrls).length > 0 && needFetch) {
384
+ await devFetchRemoteTypesOnce({
385
+ root,
386
+ baseDir,
387
+ typesFolderRel: typesFolder,
388
+ urls: autoTypeUrls
389
+ });
352
390
  }
391
+ const dtsObj = ensureDtsObject(mfMerged.dts);
392
+ dtsObj.generateTypes = {
393
+ ...dtsObj.generateTypes ?? {},
394
+ tsConfigPath: tsconfigForDtsRel
395
+ };
396
+ mfMerged.dts = dtsObj;
353
397
  } else {
354
398
  if (mfMerged.dts !== false) {
355
399
  const dtsObj = ensureDtsObject(mfMerged.dts);
@@ -363,7 +407,11 @@ function pluginCore(opts) {
363
407
  remoteTypeUrls: { ...autoTypeUrls, ...existingRt }
364
408
  };
365
409
  }
366
- dtsObj.generateTypes = dtsObj.generateTypes ?? { tsConfigPath: "./tsconfig.json" };
410
+ dtsObj.generateTypes = {
411
+ ...dtsObj.generateTypes ?? {},
412
+ tsConfigPath: tsconfigForDtsRel
413
+ // dùng tsconfig đã patch từ tsconfig của app
414
+ };
367
415
  mfMerged.dts = dtsObj;
368
416
  }
369
417
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ptech-preset",
3
- "version": "1.2.7",
3
+ "version": "1.2.8",
4
4
  "description": "Auto Module.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",