ptech-preset 1.2.6 → 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.
package/dist/index.d.ts CHANGED
@@ -13,7 +13,7 @@ type MfAutoOptions = {
13
13
  stylesExposeKey?: string;
14
14
  separateExposes?: boolean;
15
15
  separateExposeChunkPrefix?: string;
16
- /** tên folder chứa types đã consume; mặc định "@mf-types" */
16
+ /** thư mục chứa types đã consume; mặc định "@mf-types" */
17
17
  typesFolder?: string;
18
18
  /** env để ép refresh types ở dev (khởi động lại dev) */
19
19
  devTypesRefreshEnvVar?: string;
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",
@@ -323,9 +360,19 @@ function pluginCore(opts) {
323
360
  }
324
361
  }
325
362
  const command = api?.context?.command;
326
- const isProd = process.env.NODE_ENV === "production" || command === "build";
327
- const isDev = !isProd;
363
+ const isProdLike = command === "build" || process.env.NODE_ENV === "production";
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);
@@ -359,22 +403,29 @@ function pluginCore(opts) {
359
403
  dtsObj.consumeTypes = {
360
404
  typesFolder: existingConsume.typesFolder ?? typesFolder,
361
405
  maxRetries: existingConsume.maxRetries ?? 3,
362
- // ưu tiên config hiện hữu nếu đụng key
406
+ // ưu tiên config hiện hữu nếu trùng key
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
  }
370
418
  const mfPlugin = pluginModuleFederation(mfMerged);
371
419
  await mfPlugin.setup?.(api);
372
420
  const pkgSafe = getPackageName(root) || mfMerged.name || path3.basename(root).replace(/\W+/g, "");
373
- const cdnUrl = `${CDN_BASE}${pkgSafe}/`;
374
421
  api.modifyRsbuildConfig((config) => {
375
422
  config.output ??= {};
376
- if (!config.output.assetPrefix && process.env.NODE_ENV === "production") {
377
- config.output.assetPrefix = cdnUrl;
423
+ const current = config.output.assetPrefix;
424
+ const fromEnv = process.env.CDN_URL || process.env.ASSET_PREFIX;
425
+ const fallbackCdn = `${CDN_BASE}${pkgSafe}/`;
426
+ const target = fromEnv ?? fallbackCdn;
427
+ if (isProdLike && (!current || current === "/" || current === "./")) {
428
+ config.output.assetPrefix = target;
378
429
  }
379
430
  return config;
380
431
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ptech-preset",
3
- "version": "1.2.6",
3
+ "version": "1.2.8",
4
4
  "description": "Auto Module.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",