ptech-preset 1.3.3 → 1.3.5

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 +64 -42
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,10 +1,3 @@
1
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
- }) : x)(function(x) {
4
- if (typeof require !== "undefined") return require.apply(this, arguments);
5
- throw Error('Dynamic require of "' + x + '" is not supported');
6
- });
7
-
8
1
  // src/index.ts
9
2
  import path3 from "path";
10
3
  import fs3 from "fs";
@@ -134,6 +127,10 @@ async function collectAutoRemotes(opts = {}) {
134
127
  }
135
128
 
136
129
  // src/index.ts
130
+ import { createRequire } from "module";
131
+ import { fileURLToPath } from "url";
132
+ var esmRequire = createRequire(import.meta.url);
133
+ var HERE = typeof __dirname !== "undefined" ? __dirname : path3.dirname(fileURLToPath(import.meta.url));
137
134
  var DEFAULT_SHARED = {
138
135
  react: { singleton: true, eager: true, requiredVersion: false },
139
136
  "react-dom": { singleton: true, eager: true, requiredVersion: false },
@@ -227,11 +224,11 @@ async function devFetchRemoteTypesOnce(params) {
227
224
  );
228
225
  }
229
226
  function tryRequireTs(root) {
230
- const paths = [root, __dirname, process.cwd()];
231
- for (const p of paths) {
227
+ const searchPaths = [root, HERE, process.cwd()];
228
+ for (const p of searchPaths) {
232
229
  try {
233
- const tsPath = __require.resolve("typescript", { paths: [p] });
234
- return __require(tsPath);
230
+ const tsPath = esmRequire.resolve("typescript", { paths: [p] });
231
+ return esmRequire(tsPath);
235
232
  } catch {
236
233
  }
237
234
  }
@@ -248,21 +245,21 @@ function readBaseTsConfigJSONC(baseAbs, root) {
248
245
  }
249
246
  if (r.config) return r.config;
250
247
  } catch (e) {
251
- console.warn("[plugin-mf-auto] TS readConfigFile threw, fallback to JSONC:", e);
248
+ console.warn("[plugin-mf-auto] TS readConfigFile threw, fallback:", e);
252
249
  }
253
250
  }
254
251
  try {
255
- const { parse } = __require("jsonc-parser");
252
+ const { parse } = esmRequire("jsonc-parser");
256
253
  const raw = fs3.readFileSync(baseAbs, "utf8");
257
254
  return parse(raw);
258
255
  } catch {
259
- try {
260
- const raw = fs3.readFileSync(baseAbs, "utf8").replace(/^\uFEFF/, "").replace(/(^|[\s{[,])\/\/.*$/gm, "$1").replace(/\/\*[\s\S]*?\*\//g, "").replace(/,(?=\s*[\]}])/g, "");
261
- return JSON.parse(raw);
262
- } catch (e) {
263
- console.warn("[plugin-mf-auto] Cannot parse tsconfig (fallback) -> using minimal:", e);
264
- return {};
265
- }
256
+ }
257
+ try {
258
+ const raw = fs3.readFileSync(baseAbs, "utf8").replace(/^\uFEFF/, "").replace(/(^|[\s{[,])\/\/.*$/gm, "$1").replace(/\/\*[\s\S]*?\*\//g, "").replace(/,(?=\s*[\]}])/g, "");
259
+ return JSON.parse(raw);
260
+ } catch (e) {
261
+ console.warn("[plugin-mf-auto] Cannot parse tsconfig (fallback) -> using minimal:", e);
262
+ return {};
266
263
  }
267
264
  }
268
265
  function ensureDtsObject(dts) {
@@ -341,7 +338,6 @@ function pluginCore(opts) {
341
338
  return {
342
339
  name: "plugin-mf-auto",
343
340
  async setup(api) {
344
- const { pluginModuleFederation } = await import("@module-federation/rsbuild-plugin");
345
341
  const getExposes = async () => {
346
342
  if (exposesMode === "jsdoc") return collectAutoExposes({ baseDir, globs });
347
343
  if (exposesMode === "wrapper") return collectAutoExposesWithWrapper({ baseDir, globs });
@@ -380,6 +376,17 @@ function pluginCore(opts) {
380
376
  if (typeof mfMerged.shared === "undefined") {
381
377
  mfMerged.shared = DEFAULT_SHARED;
382
378
  }
379
+ const command = api?.context?.command;
380
+ const isBuild = command === "build";
381
+ const isDev = !isBuild;
382
+ if (isDev && mfMerged.shared && typeof mfMerged.shared === "object") {
383
+ for (const key of Object.keys(mfMerged.shared)) {
384
+ const conf = mfMerged.shared[key];
385
+ if (conf && typeof conf === "object" && "eager" in conf) {
386
+ conf.eager = false;
387
+ }
388
+ }
389
+ }
383
390
  if (cssInjection !== "none") {
384
391
  const cssAbs = path3.resolve(root, cssEntry);
385
392
  const hasCss = fs3.existsSync(cssAbs);
@@ -388,14 +395,24 @@ function pluginCore(opts) {
388
395
  if (cssInjection === "wrapper" && mfMerged.exposes) {
389
396
  const wrapped = {};
390
397
  for (const [key, rel] of Object.entries(mfMerged.exposes)) {
391
- const targetAbs = path3.resolve(root, String(rel).replace(/^\.\//, ""));
398
+ const targetAbs = path3.resolve(root, String(rel).replace(/^\.\//, "")).replace(/\\/g, "/");
392
399
  const proxyFile = path3.join(tempDir, `expose_${key.replace(/[./]/g, "_")}.ts`);
393
400
  const lines = [];
394
- if (hasCss) lines.push(`import ${JSON.stringify(cssAbs.replace(/\\/g, "/"))};`);
395
- else lines.push(`/* no css: ${cssEntry} not found */`);
396
- const target = JSON.stringify(targetAbs.replace(/\\/g, "/"));
401
+ if (hasCss) {
402
+ const cssRel = path3.relative(path3.dirname(proxyFile), cssAbs).replace(/\\/g, "/");
403
+ const cssImport = cssRel.startsWith(".") ? cssRel : `./${cssRel}`;
404
+ lines.push(`import ${JSON.stringify(cssImport)};`);
405
+ } else {
406
+ lines.push(`/* no css: ${cssEntry} not found */`);
407
+ }
408
+ const targetRel = path3.relative(path3.dirname(proxyFile), targetAbs).replace(/\\/g, "/");
409
+ const targetImport = targetRel.startsWith(".") ? targetRel : `./${targetRel}`;
410
+ const target = JSON.stringify(targetImport);
397
411
  lines.push(`export * from ${target};`);
398
412
  lines.push(`export { default } from ${target};`);
413
+ lines.push(
414
+ `if (import.meta && (import.meta as any).webpackHot) {(import.meta as any).webpackHot.accept();}`
415
+ );
399
416
  lines.push(`// generated by plugin-mf-auto`);
400
417
  fs3.writeFileSync(proxyFile, lines.join(os.EOL), "utf8");
401
418
  const relFromRoot = path3.relative(root, proxyFile).replace(/\\/g, "/");
@@ -405,9 +422,12 @@ function pluginCore(opts) {
405
422
  }
406
423
  if (cssInjection === "styles-expose" && hasCss) {
407
424
  const stylesFile = path3.join(tempDir, `styles_expose.ts`);
425
+ const cssRel = path3.relative(path3.dirname(stylesFile), cssAbs).replace(/\\/g, "/");
426
+ const cssImport = cssRel.startsWith(".") ? cssRel : `./${cssRel}`;
408
427
  fs3.writeFileSync(
409
428
  stylesFile,
410
- `import ${JSON.stringify(cssAbs.replace(/\\/g, "/"))};
429
+ `import ${JSON.stringify(cssImport)};
430
+ if (import.meta && (import.meta as any).webpackHot) {(import.meta as any).webpackHot.accept();}
411
431
  // generated by plugin-mf-auto`,
412
432
  "utf8"
413
433
  );
@@ -415,9 +435,6 @@ function pluginCore(opts) {
415
435
  mfMerged.exposes = { ...mfMerged.exposes ?? {}, [stylesExposeKey]: `./${relFromRoot}` };
416
436
  }
417
437
  }
418
- const command = api?.context?.command;
419
- const isProdLike = command === "build" || process.env.NODE_ENV === "production";
420
- const isDev = !isProdLike;
421
438
  const autoTypeUrls = buildRemoteTypeUrls(mfMerged.remotes || {});
422
439
  const remoteAliases = Object.keys(autoTypeUrls);
423
440
  const tsconfigForDtsAbs = writePatchedTsconfig({
@@ -441,18 +458,23 @@ function pluginCore(opts) {
441
458
  await devFetchRemoteTypesOnce({ root, baseDir, typesFolderRel: typesFolder, urls: autoTypeUrls });
442
459
  }
443
460
  }
444
- const dtsGen = ensureDtsObject(mfMerged.dts);
445
- dtsGen.generateTypes = { ...dtsGen.generateTypes ?? {}, tsConfigPath: tsconfigForDtsToUse };
446
- if (!isDev && remoteAliases.length > 0) {
447
- const existingConsume = dtsGen.consumeTypes ?? {};
448
- const existingRt = existingConsume.remoteTypeUrls ?? {};
449
- dtsGen.consumeTypes = {
450
- typesFolder: existingConsume.typesFolder ?? typesFolder,
451
- maxRetries: existingConsume.maxRetries ?? 3,
452
- remoteTypeUrls: { ...autoTypeUrls, ...existingRt }
453
- };
461
+ const dtsGenMaybe = ensureDtsObject(mfMerged.dts);
462
+ if (dtsGenMaybe !== false) {
463
+ const dtsGen = dtsGenMaybe;
464
+ dtsGen.generateTypes = { ...dtsGen.generateTypes ?? {}, tsConfigPath: tsconfigForDtsToUse };
465
+ if (isBuild && remoteAliases.length > 0) {
466
+ const existingConsume = dtsGen.consumeTypes ?? {};
467
+ const existingRt = existingConsume.remoteTypeUrls ?? {};
468
+ dtsGen.consumeTypes = {
469
+ typesFolder: existingConsume.typesFolder ?? typesFolder,
470
+ maxRetries: existingConsume.maxRetries ?? 3,
471
+ remoteTypeUrls: { ...autoTypeUrls, ...existingRt }
472
+ };
473
+ }
474
+ mfMerged.dts = dtsGen;
475
+ } else {
476
+ mfMerged.dts = false;
454
477
  }
455
- mfMerged.dts = dtsGen;
456
478
  const { pluginModuleFederation: mfPluginFactory } = await import("@module-federation/rsbuild-plugin");
457
479
  const mfPlugin = mfPluginFactory(mfMerged);
458
480
  await mfPlugin.setup?.(api);
@@ -463,7 +485,7 @@ function pluginCore(opts) {
463
485
  const fromEnv = process.env.CDN_URL || process.env.ASSET_PREFIX;
464
486
  const fallbackCdn = `${CDN_BASE}${pkgSafe}/`;
465
487
  const target = fromEnv ?? fallbackCdn;
466
- if (isProdLike && (!current || current === "/" || current === "./")) {
488
+ if (isBuild && (!current || current === "/" || current === "./")) {
467
489
  config.output.assetPrefix = target;
468
490
  }
469
491
  return config;
@@ -473,7 +495,7 @@ function pluginCore(opts) {
473
495
  for (const [key, rel] of Object.entries(mfMerged.exposes)) {
474
496
  const abs = path3.resolve(root, String(rel).replace(/^\.\//, "")).replace(/\\/g, "/");
475
497
  const safe = separateExposeChunkPrefix + key.replace(/[^a-zA-Z0-9_]/g, "_");
476
- force[safe] = new RegExp(`${escapeRegExp(abs)}$`);
498
+ force[safe] = new RegExp(`${escapeRegExp(abs)}(\\?.*)?$`);
477
499
  }
478
500
  api.modifyRsbuildConfig((config) => {
479
501
  (config.performance ??= {}).chunkSplit ??= {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ptech-preset",
3
- "version": "1.3.3",
3
+ "version": "1.3.5",
4
4
  "description": "Auto Module.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",