ptech-preset 1.1.5 → 1.1.7

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,12 +13,12 @@ type MfAutoOptions = {
13
13
  cssInjection?: CssInjectionMode;
14
14
  cssEntry?: string;
15
15
  stylesExposeKey?: string;
16
+ /** Ép tách chunk cho từng expose */
17
+ separateExposes?: boolean;
18
+ separateExposeChunkPrefix?: string;
16
19
  /** options chuyển nguyên vẹn sang @module-federation/rsbuild-plugin */
17
20
  mf: Record<string, any>;
18
21
  };
19
- /**
20
- * Plugin bọc (wrapper) để tự động hoá exposes/remotes rồi compose MF plugin.
21
- */
22
22
  declare function pluginMFAuto(opts: MfAutoOptions): RsbuildPlugin;
23
23
 
24
24
  export { type MfAutoOptions, pluginMFAuto as default, pluginMFAuto };
package/dist/index.js CHANGED
@@ -12,20 +12,32 @@ async function collectAutoExposes(opts = {}) {
12
12
  const globs = opts.globs ?? ["src/components/**/*.{ts,tsx}"];
13
13
  const baseDir = path.resolve(process.cwd(), opts.baseDir ?? "src");
14
14
  const tag = opts.tag ?? "expose";
15
+ const debug = !!opts.debug;
15
16
  const files = await fg(globs, {
16
17
  cwd: process.cwd(),
17
18
  absolute: true,
18
19
  ignore: ["**/*.d.ts"]
19
20
  });
21
+ const jsdocRe = new RegExp(
22
+ String.raw`/\*\*[\s\S]*?@${tag}(?:\s+([^\s*]+))?[\s\S]*?\*/`,
23
+ "m"
24
+ );
20
25
  const exposes = {};
21
26
  for (const abs of files) {
22
27
  const text = fs.readFileSync(abs, "utf8");
23
- const m = text.match(new RegExp(`/\\*\\*[^*]*\\*+[^/]*@${tag}\\s*([^*\\n\\r]*)`, "m"));
28
+ const m = text.match(jsdocRe);
24
29
  if (!m) continue;
25
30
  const raw = (m[1] || "").trim();
26
31
  const key = raw || path.basename(abs).replace(/\.(tsx?|jsx?)$/, "");
27
32
  const rel = path.relative(baseDir, abs).replace(/\\/g, "/");
28
33
  exposes[`./${key}`] = `./${rel}`;
34
+ if (debug) {
35
+ console.log(`[mf-auto] JSDoc expose: key="./${key}" -> ${rel}`);
36
+ }
37
+ }
38
+ if (debug) {
39
+ const count = Object.keys(exposes).length;
40
+ console.log(`[mf-auto] collectAutoExposes: ${count} expose(s) found`);
29
41
  }
30
42
  return exposes;
31
43
  }
@@ -33,29 +45,41 @@ async function collectAutoExposesWithWrapper(opts = {}) {
33
45
  if (opts.enabled === false) return {};
34
46
  const globs = opts.globs ?? ["src/components/**/*.{ts,tsx}"];
35
47
  const baseDir = path.resolve(process.cwd(), opts.baseDir ?? "src");
48
+ const debug = !!opts.debug;
36
49
  const files = await fg(globs, {
37
50
  cwd: process.cwd(),
38
51
  absolute: true,
39
52
  ignore: ["**/*.d.ts"]
40
53
  });
54
+ const jsdocRe = new RegExp(
55
+ String.raw`/\*\*[\s\S]*?@expose(?:\s+([^\s*]+))?[\s\S]*?\*/`,
56
+ "m"
57
+ );
58
+ const wrapperRe = /exposeComponent\s*\(\s*[^,]+,\s*['"`]([^'"`]+)['"`]\s*\)/;
41
59
  const exposes = {};
42
60
  for (const abs of files) {
43
61
  const text = fs.readFileSync(abs, "utf8");
44
- const jsdocMatch = text.match(/\/\*\*[^*]*\*+[^/]*@expose\s*([^*\n\r]*)/m);
62
+ let key;
63
+ const jsdocMatch = text.match(jsdocRe);
45
64
  if (jsdocMatch) {
46
- const raw = (jsdocMatch[1] || "").trim();
47
- const key = raw || path.basename(abs).replace(/\.(tsx?|jsx?)$/, "");
48
- const rel = path.relative(baseDir, abs).replace(/\\/g, "/");
49
- exposes[`./${key}`] = `./${rel}`;
65
+ key = (jsdocMatch[1] || "").trim();
66
+ } else {
67
+ const wrapperMatch = text.match(wrapperRe);
68
+ if (wrapperMatch) key = wrapperMatch[1];
69
+ }
70
+ if (!key) {
50
71
  continue;
51
72
  }
52
- const wrapperMatch = text.match(/exposeComponent\s*\(\s*[^,]+,\s*['"`]([^'"`]+)['"`]\s*\)/);
53
- if (wrapperMatch) {
54
- const key = wrapperMatch[1];
55
- const rel = path.relative(baseDir, abs).replace(/\\/g, "/");
56
- exposes[`./${key}`] = `./${rel}`;
73
+ const rel = path.relative(baseDir, abs).replace(/\\/g, "/");
74
+ exposes[`./${key}`] = `./${rel}`;
75
+ if (debug) {
76
+ console.log(`[mf-auto] Wrapper/JSDoc expose: key="./${key}" -> ${rel}`);
57
77
  }
58
78
  }
79
+ if (debug) {
80
+ const count = Object.keys(exposes).length;
81
+ console.log(`[mf-auto] collectAutoExposesWithWrapper: ${count} expose(s) found`);
82
+ }
59
83
  return exposes;
60
84
  }
61
85
 
@@ -125,6 +149,7 @@ function normalizeExposePath(root, maybeRel) {
125
149
  const relFromRoot = path3.relative(root, abs).replace(/\\/g, "/");
126
150
  return `./${relFromRoot}`;
127
151
  }
152
+ var escapeRegExp = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
128
153
  function pluginMFAuto(opts) {
129
154
  const {
130
155
  baseDir = "src",
@@ -136,6 +161,8 @@ function pluginMFAuto(opts) {
136
161
  cssInjection = "none",
137
162
  cssEntry = "src/index.css",
138
163
  stylesExposeKey = "./styles",
164
+ separateExposes = true,
165
+ separateExposeChunkPrefix = "mf-expose-",
139
166
  mf
140
167
  } = opts;
141
168
  if (!mf || typeof mf !== "object") {
@@ -149,8 +176,7 @@ function pluginMFAuto(opts) {
149
176
  async setup(api) {
150
177
  const { pluginModuleFederation } = await import("@module-federation/rsbuild-plugin");
151
178
  const getExposes = async () => {
152
- if (exposesMode === "jsdoc")
153
- return collectAutoExposes({ baseDir, globs });
179
+ if (exposesMode === "jsdoc") return collectAutoExposes({ baseDir, globs });
154
180
  if (exposesMode === "wrapper")
155
181
  return collectAutoExposesWithWrapper({ baseDir, globs });
156
182
  const a = await collectAutoExposes({ baseDir, globs });
@@ -231,11 +257,25 @@ function pluginMFAuto(opts) {
231
257
  };
232
258
  }
233
259
  }
234
- if (typeof mfMerged.dts === "undefined") {
235
- mfMerged.dts = true;
236
- }
260
+ if (typeof mfMerged.dts === "undefined") mfMerged.dts = true;
237
261
  const mfPlugin = pluginModuleFederation(mfMerged);
238
262
  await mfPlugin.setup?.(api);
263
+ if (separateExposes && mfMerged.exposes) {
264
+ const force = {};
265
+ for (const [key, rel] of Object.entries(
266
+ mfMerged.exposes
267
+ )) {
268
+ const abs = path3.resolve(root, String(rel).replace(/^\.\//, "")).replace(/\\/g, "/");
269
+ const safe = separateExposeChunkPrefix + key.replace(/[^a-zA-Z0-9_]/g, "_");
270
+ force[safe] = new RegExp(`${escapeRegExp(abs)}$`);
271
+ }
272
+ api.modifyRsbuildConfig((config) => {
273
+ (config.performance ??= {}).chunkSplit ??= {};
274
+ const cs = config.performance.chunkSplit;
275
+ cs.forceSplitting = { ...cs.forceSplitting || {}, ...force };
276
+ return config;
277
+ });
278
+ }
239
279
  }
240
280
  };
241
281
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ptech-preset",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
4
4
  "description": "Auto Module.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",