ptech-preset 1.1.4 → 1.1.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.
package/dist/index.d.ts CHANGED
@@ -1,14 +1,24 @@
1
1
  import { RsbuildPlugin } from '@rsbuild/core';
2
2
 
3
+ type CssInjectionMode = "none" | "wrapper" | "styles-expose";
3
4
  type MfAutoOptions = {
4
5
  baseDir?: string;
5
6
  globs?: string[];
6
7
  exposesMode?: "jsdoc" | "wrapper" | "both";
7
8
  envPrefix?: string;
8
9
  manifestPathOrUrl?: string;
10
+ /** true: chỉ auto khi exposes/remotes trống; false: luôn auto rồi merge */
9
11
  autoWhenEmpty?: boolean;
12
+ /** Tiêm CSS vào exposes */
13
+ cssInjection?: CssInjectionMode;
14
+ cssEntry?: string;
15
+ stylesExposeKey?: string;
16
+ /** options chuyển nguyên vẹn sang @module-federation/rsbuild-plugin */
10
17
  mf: Record<string, any>;
11
18
  };
19
+ /**
20
+ * Plugin bọc (wrapper) để tự động hoá exposes/remotes rồi compose MF plugin.
21
+ */
12
22
  declare function pluginMFAuto(opts: MfAutoOptions): RsbuildPlugin;
13
23
 
14
24
  export { type MfAutoOptions, pluginMFAuto as default, pluginMFAuto };
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // src/index.ts
2
2
  import path3 from "path";
3
3
  import fs3 from "fs";
4
+ import os from "os";
4
5
 
5
6
  // src/collectAutoExposes.ts
6
7
  import path from "path";
@@ -118,6 +119,12 @@ function getPackageName(root) {
118
119
  }
119
120
  return void 0;
120
121
  }
122
+ function normalizeExposePath(root, maybeRel) {
123
+ const cleaned = maybeRel.replace(/^\.\//, "");
124
+ const abs = path3.resolve(root, cleaned);
125
+ const relFromRoot = path3.relative(root, abs).replace(/\\/g, "/");
126
+ return `./${relFromRoot}`;
127
+ }
121
128
  function pluginMFAuto(opts) {
122
129
  const {
123
130
  baseDir = "src",
@@ -126,6 +133,9 @@ function pluginMFAuto(opts) {
126
133
  envPrefix = "REMOTE_",
127
134
  manifestPathOrUrl,
128
135
  autoWhenEmpty = true,
136
+ cssInjection = "none",
137
+ cssEntry = "src/index.css",
138
+ stylesExposeKey = "./styles",
129
139
  mf
130
140
  } = opts;
131
141
  if (!mf || typeof mf !== "object") {
@@ -148,27 +158,84 @@ function pluginMFAuto(opts) {
148
158
  return { ...a, ...b };
149
159
  };
150
160
  const getRemotes = async () => collectAutoRemotes({ envPrefix, manifestPathOrUrl });
151
- api.modifyRsbuildConfig(async (config) => {
152
- const mfMerged = { ...mf };
153
- if (!autoWhenEmpty || mfMerged.exposes == null || Object.keys(mfMerged.exposes).length === 0) {
154
- const autoExposes = await getExposes();
155
- if (Object.keys(autoExposes).length > 0) {
156
- mfMerged.exposes = { ...mfMerged.exposes ?? {}, ...autoExposes };
161
+ const root = api.context.rootPath;
162
+ const mfMerged = { ...mf };
163
+ if (!autoWhenEmpty || mfMerged.exposes == null || Object.keys(mfMerged.exposes).length === 0) {
164
+ const autoExposes = await getExposes();
165
+ if (Object.keys(autoExposes).length > 0) {
166
+ const normalized = {};
167
+ for (const [k, rel] of Object.entries(autoExposes)) {
168
+ normalized[k] = normalizeExposePath(root, rel);
157
169
  }
170
+ mfMerged.exposes = { ...mfMerged.exposes ?? {}, ...normalized };
171
+ }
172
+ } else if (mfMerged.exposes) {
173
+ const normalized = {};
174
+ for (const [k, rel] of Object.entries(mfMerged.exposes)) {
175
+ normalized[k] = normalizeExposePath(root, String(rel));
176
+ }
177
+ mfMerged.exposes = normalized;
178
+ }
179
+ if (!autoWhenEmpty || mfMerged.remotes == null || Object.keys(mfMerged.remotes).length === 0) {
180
+ const autoRemotes = await getRemotes();
181
+ if (Object.keys(autoRemotes).length > 0) {
182
+ mfMerged.remotes = { ...mfMerged.remotes ?? {}, ...autoRemotes };
158
183
  }
159
- if (!autoWhenEmpty || mfMerged.remotes == null || Object.keys(mfMerged.remotes).length === 0) {
160
- const autoRemotes = await getRemotes();
161
- if (Object.keys(autoRemotes).length > 0) {
162
- mfMerged.remotes = { ...mfMerged.remotes ?? {}, ...autoRemotes };
184
+ }
185
+ if (!mfMerged.name) {
186
+ mfMerged.name = getPackageName(root) || path3.basename(root).replace(/\W+/g, "");
187
+ }
188
+ if (cssInjection !== "none") {
189
+ const cssAbs = path3.resolve(root, cssEntry);
190
+ const hasCss = fs3.existsSync(cssAbs);
191
+ const tempDir = path3.resolve(root, ".mf-auto");
192
+ fs3.mkdirSync(tempDir, { recursive: true });
193
+ if (cssInjection === "wrapper" && mfMerged.exposes) {
194
+ const wrapped = {};
195
+ for (const [key, rel] of Object.entries(
196
+ mfMerged.exposes
197
+ )) {
198
+ const targetAbs = path3.resolve(root, String(rel).replace(/^\.\//, ""));
199
+ const proxyFile = path3.join(
200
+ tempDir,
201
+ `expose_${key.replace(/[./]/g, "_")}.ts`
202
+ );
203
+ const lines = [];
204
+ if (hasCss) {
205
+ lines.push(`import ${JSON.stringify(cssAbs.replace(/\\/g, "/"))};`);
206
+ } else {
207
+ lines.push(`/* no css: ${cssEntry} not found */`);
208
+ }
209
+ const target = JSON.stringify(targetAbs.replace(/\\/g, "/"));
210
+ lines.push(`export * from ${target};`);
211
+ lines.push(`export { default } from ${target};`);
212
+ lines.push(`// generated by plugin-mf-auto`);
213
+ fs3.writeFileSync(proxyFile, lines.join(os.EOL), "utf8");
214
+ const relFromRoot = path3.relative(root, proxyFile).replace(/\\/g, "/");
215
+ wrapped[key] = `./${relFromRoot}`;
163
216
  }
217
+ mfMerged.exposes = wrapped;
164
218
  }
165
- if (!mfMerged.name) {
166
- mfMerged.name = getPackageName(api.context.rootPath) || path3.basename(api.context.rootPath).replace(/\W+/g, "");
219
+ if (cssInjection === "styles-expose" && hasCss) {
220
+ const stylesFile = path3.join(tempDir, `styles_expose.ts`);
221
+ fs3.writeFileSync(
222
+ stylesFile,
223
+ `import ${JSON.stringify(cssAbs.replace(/\\/g, "/"))};
224
+ // generated by plugin-mf-auto`,
225
+ "utf8"
226
+ );
227
+ const relFromRoot = path3.relative(root, stylesFile).replace(/\\/g, "/");
228
+ mfMerged.exposes = {
229
+ ...mfMerged.exposes ?? {},
230
+ [stylesExposeKey]: `./${relFromRoot}`
231
+ };
167
232
  }
168
- config.plugins ||= [];
169
- config.plugins.push(pluginModuleFederation(mfMerged));
170
- return config;
171
- });
233
+ }
234
+ if (typeof mfMerged.dts === "undefined") {
235
+ mfMerged.dts = true;
236
+ }
237
+ const mfPlugin = pluginModuleFederation(mfMerged);
238
+ await mfPlugin.setup?.(api);
172
239
  }
173
240
  };
174
241
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ptech-preset",
3
- "version": "1.1.4",
3
+ "version": "1.1.5",
4
4
  "description": "Auto Module.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",