ptech-preset 1.1.3 → 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 +10 -0
- package/dist/index.js +98 -16
- package/package.json +1 -1
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,5 +1,7 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import path3 from "path";
|
|
3
|
+
import fs3 from "fs";
|
|
4
|
+
import os from "os";
|
|
3
5
|
|
|
4
6
|
// src/collectAutoExposes.ts
|
|
5
7
|
import path from "path";
|
|
@@ -103,6 +105,26 @@ async function collectAutoRemotes(opts = {}) {
|
|
|
103
105
|
}
|
|
104
106
|
|
|
105
107
|
// src/index.ts
|
|
108
|
+
function getPackageName(root) {
|
|
109
|
+
try {
|
|
110
|
+
const pkgPath = path3.resolve(root, "package.json");
|
|
111
|
+
if (fs3.existsSync(pkgPath)) {
|
|
112
|
+
const pkg = JSON.parse(fs3.readFileSync(pkgPath, "utf8"));
|
|
113
|
+
if (pkg.name && typeof pkg.name === "string") {
|
|
114
|
+
return pkg.name.replace(/\W+/g, "");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
} catch (err) {
|
|
118
|
+
console.warn("[plugin-mf-auto] Cannot read package.json:", err);
|
|
119
|
+
}
|
|
120
|
+
return void 0;
|
|
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
|
+
}
|
|
106
128
|
function pluginMFAuto(opts) {
|
|
107
129
|
const {
|
|
108
130
|
baseDir = "src",
|
|
@@ -111,6 +133,9 @@ function pluginMFAuto(opts) {
|
|
|
111
133
|
envPrefix = "REMOTE_",
|
|
112
134
|
manifestPathOrUrl,
|
|
113
135
|
autoWhenEmpty = true,
|
|
136
|
+
cssInjection = "none",
|
|
137
|
+
cssEntry = "src/index.css",
|
|
138
|
+
stylesExposeKey = "./styles",
|
|
114
139
|
mf
|
|
115
140
|
} = opts;
|
|
116
141
|
if (!mf || typeof mf !== "object") {
|
|
@@ -133,27 +158,84 @@ function pluginMFAuto(opts) {
|
|
|
133
158
|
return { ...a, ...b };
|
|
134
159
|
};
|
|
135
160
|
const getRemotes = async () => collectAutoRemotes({ envPrefix, manifestPathOrUrl });
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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);
|
|
142
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));
|
|
143
176
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
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 };
|
|
183
|
+
}
|
|
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}`;
|
|
148
216
|
}
|
|
217
|
+
mfMerged.exposes = wrapped;
|
|
149
218
|
}
|
|
150
|
-
if (
|
|
151
|
-
|
|
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
|
+
};
|
|
152
232
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}
|
|
233
|
+
}
|
|
234
|
+
if (typeof mfMerged.dts === "undefined") {
|
|
235
|
+
mfMerged.dts = true;
|
|
236
|
+
}
|
|
237
|
+
const mfPlugin = pluginModuleFederation(mfMerged);
|
|
238
|
+
await mfPlugin.setup?.(api);
|
|
157
239
|
}
|
|
158
240
|
};
|
|
159
241
|
}
|