ptech-preset 1.1.4 → 1.1.6
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 +10 -0
- package/dist/index.js +101 -18
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
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
|
+
/** Ép tách chunk cho từng expose */
|
|
17
|
+
separateExposes?: boolean;
|
|
18
|
+
separateExposeChunkPrefix?: string;
|
|
19
|
+
/** options chuyển nguyên vẹn sang @module-federation/rsbuild-plugin */
|
|
10
20
|
mf: Record<string, any>;
|
|
11
21
|
};
|
|
12
22
|
declare function pluginMFAuto(opts: MfAutoOptions): RsbuildPlugin;
|
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,13 @@ 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
|
+
}
|
|
128
|
+
var escapeRegExp = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
121
129
|
function pluginMFAuto(opts) {
|
|
122
130
|
const {
|
|
123
131
|
baseDir = "src",
|
|
@@ -126,6 +134,11 @@ function pluginMFAuto(opts) {
|
|
|
126
134
|
envPrefix = "REMOTE_",
|
|
127
135
|
manifestPathOrUrl,
|
|
128
136
|
autoWhenEmpty = true,
|
|
137
|
+
cssInjection = "none",
|
|
138
|
+
cssEntry = "src/index.css",
|
|
139
|
+
stylesExposeKey = "./styles",
|
|
140
|
+
separateExposes = true,
|
|
141
|
+
separateExposeChunkPrefix = "mf-expose-",
|
|
129
142
|
mf
|
|
130
143
|
} = opts;
|
|
131
144
|
if (!mf || typeof mf !== "object") {
|
|
@@ -139,8 +152,7 @@ function pluginMFAuto(opts) {
|
|
|
139
152
|
async setup(api) {
|
|
140
153
|
const { pluginModuleFederation } = await import("@module-federation/rsbuild-plugin");
|
|
141
154
|
const getExposes = async () => {
|
|
142
|
-
if (exposesMode === "jsdoc")
|
|
143
|
-
return collectAutoExposes({ baseDir, globs });
|
|
155
|
+
if (exposesMode === "jsdoc") return collectAutoExposes({ baseDir, globs });
|
|
144
156
|
if (exposesMode === "wrapper")
|
|
145
157
|
return collectAutoExposesWithWrapper({ baseDir, globs });
|
|
146
158
|
const a = await collectAutoExposes({ baseDir, globs });
|
|
@@ -148,27 +160,98 @@ function pluginMFAuto(opts) {
|
|
|
148
160
|
return { ...a, ...b };
|
|
149
161
|
};
|
|
150
162
|
const getRemotes = async () => collectAutoRemotes({ envPrefix, manifestPathOrUrl });
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
163
|
+
const root = api.context.rootPath;
|
|
164
|
+
const mfMerged = { ...mf };
|
|
165
|
+
if (!autoWhenEmpty || mfMerged.exposes == null || Object.keys(mfMerged.exposes).length === 0) {
|
|
166
|
+
const autoExposes = await getExposes();
|
|
167
|
+
if (Object.keys(autoExposes).length > 0) {
|
|
168
|
+
const normalized = {};
|
|
169
|
+
for (const [k, rel] of Object.entries(autoExposes)) {
|
|
170
|
+
normalized[k] = normalizeExposePath(root, rel);
|
|
157
171
|
}
|
|
172
|
+
mfMerged.exposes = { ...mfMerged.exposes ?? {}, ...normalized };
|
|
173
|
+
}
|
|
174
|
+
} else if (mfMerged.exposes) {
|
|
175
|
+
const normalized = {};
|
|
176
|
+
for (const [k, rel] of Object.entries(mfMerged.exposes)) {
|
|
177
|
+
normalized[k] = normalizeExposePath(root, String(rel));
|
|
158
178
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
179
|
+
mfMerged.exposes = normalized;
|
|
180
|
+
}
|
|
181
|
+
if (!autoWhenEmpty || mfMerged.remotes == null || Object.keys(mfMerged.remotes).length === 0) {
|
|
182
|
+
const autoRemotes = await getRemotes();
|
|
183
|
+
if (Object.keys(autoRemotes).length > 0) {
|
|
184
|
+
mfMerged.remotes = { ...mfMerged.remotes ?? {}, ...autoRemotes };
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (!mfMerged.name) {
|
|
188
|
+
mfMerged.name = getPackageName(root) || path3.basename(root).replace(/\W+/g, "");
|
|
189
|
+
}
|
|
190
|
+
if (cssInjection !== "none") {
|
|
191
|
+
const cssAbs = path3.resolve(root, cssEntry);
|
|
192
|
+
const hasCss = fs3.existsSync(cssAbs);
|
|
193
|
+
const tempDir = path3.resolve(root, ".mf-auto");
|
|
194
|
+
fs3.mkdirSync(tempDir, { recursive: true });
|
|
195
|
+
if (cssInjection === "wrapper" && mfMerged.exposes) {
|
|
196
|
+
const wrapped = {};
|
|
197
|
+
for (const [key, rel] of Object.entries(
|
|
198
|
+
mfMerged.exposes
|
|
199
|
+
)) {
|
|
200
|
+
const targetAbs = path3.resolve(root, String(rel).replace(/^\.\//, ""));
|
|
201
|
+
const proxyFile = path3.join(
|
|
202
|
+
tempDir,
|
|
203
|
+
`expose_${key.replace(/[./]/g, "_")}.ts`
|
|
204
|
+
);
|
|
205
|
+
const lines = [];
|
|
206
|
+
if (hasCss) {
|
|
207
|
+
lines.push(`import ${JSON.stringify(cssAbs.replace(/\\/g, "/"))};`);
|
|
208
|
+
} else {
|
|
209
|
+
lines.push(`/* no css: ${cssEntry} not found */`);
|
|
210
|
+
}
|
|
211
|
+
const target = JSON.stringify(targetAbs.replace(/\\/g, "/"));
|
|
212
|
+
lines.push(`export * from ${target};`);
|
|
213
|
+
lines.push(`export { default } from ${target};`);
|
|
214
|
+
lines.push(`// generated by plugin-mf-auto`);
|
|
215
|
+
fs3.writeFileSync(proxyFile, lines.join(os.EOL), "utf8");
|
|
216
|
+
const relFromRoot = path3.relative(root, proxyFile).replace(/\\/g, "/");
|
|
217
|
+
wrapped[key] = `./${relFromRoot}`;
|
|
163
218
|
}
|
|
219
|
+
mfMerged.exposes = wrapped;
|
|
220
|
+
}
|
|
221
|
+
if (cssInjection === "styles-expose" && hasCss) {
|
|
222
|
+
const stylesFile = path3.join(tempDir, `styles_expose.ts`);
|
|
223
|
+
fs3.writeFileSync(
|
|
224
|
+
stylesFile,
|
|
225
|
+
`import ${JSON.stringify(cssAbs.replace(/\\/g, "/"))};
|
|
226
|
+
// generated by plugin-mf-auto`,
|
|
227
|
+
"utf8"
|
|
228
|
+
);
|
|
229
|
+
const relFromRoot = path3.relative(root, stylesFile).replace(/\\/g, "/");
|
|
230
|
+
mfMerged.exposes = {
|
|
231
|
+
...mfMerged.exposes ?? {},
|
|
232
|
+
[stylesExposeKey]: `./${relFromRoot}`
|
|
233
|
+
};
|
|
164
234
|
}
|
|
165
|
-
|
|
166
|
-
|
|
235
|
+
}
|
|
236
|
+
if (typeof mfMerged.dts === "undefined") mfMerged.dts = true;
|
|
237
|
+
const mfPlugin = pluginModuleFederation(mfMerged);
|
|
238
|
+
await mfPlugin.setup?.(api);
|
|
239
|
+
if (separateExposes && mfMerged.exposes) {
|
|
240
|
+
const force = {};
|
|
241
|
+
for (const [key, rel] of Object.entries(
|
|
242
|
+
mfMerged.exposes
|
|
243
|
+
)) {
|
|
244
|
+
const abs = path3.resolve(root, String(rel).replace(/^\.\//, "")).replace(/\\/g, "/");
|
|
245
|
+
const safe = separateExposeChunkPrefix + key.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
246
|
+
force[safe] = new RegExp(`${escapeRegExp(abs)}$`);
|
|
167
247
|
}
|
|
168
|
-
config
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
248
|
+
api.modifyRsbuildConfig((config) => {
|
|
249
|
+
(config.performance ??= {}).chunkSplit ??= {};
|
|
250
|
+
const cs = config.performance.chunkSplit;
|
|
251
|
+
cs.forceSplitting = { ...cs.forceSplitting || {}, ...force };
|
|
252
|
+
return config;
|
|
253
|
+
});
|
|
254
|
+
}
|
|
172
255
|
}
|
|
173
256
|
};
|
|
174
257
|
}
|