ptech-preset 1.1.2 → 1.1.4

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,15 +1,14 @@
1
1
  import { RsbuildPlugin } from '@rsbuild/core';
2
2
 
3
3
  type MfAutoOptions = {
4
- name?: string;
5
- filename?: string;
6
4
  baseDir?: string;
7
5
  globs?: string[];
8
6
  exposesMode?: "jsdoc" | "wrapper" | "both";
9
7
  envPrefix?: string;
10
8
  manifestPathOrUrl?: string;
11
- autoShareReact?: boolean;
9
+ autoWhenEmpty?: boolean;
10
+ mf: Record<string, any>;
12
11
  };
13
- declare function pluginCore(opts?: MfAutoOptions): RsbuildPlugin;
12
+ declare function pluginMFAuto(opts: MfAutoOptions): RsbuildPlugin;
14
13
 
15
- export { type MfAutoOptions, pluginCore as default, pluginCore };
14
+ export { type MfAutoOptions, pluginMFAuto as default, pluginMFAuto };
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // src/index.ts
2
2
  import path3 from "path";
3
+ import fs3 from "fs";
3
4
 
4
5
  // src/collectAutoExposes.ts
5
6
  import path from "path";
@@ -103,23 +104,41 @@ async function collectAutoRemotes(opts = {}) {
103
104
  }
104
105
 
105
106
  // src/index.ts
106
- function pluginCore(opts = {}) {
107
+ function getPackageName(root) {
108
+ try {
109
+ const pkgPath = path3.resolve(root, "package.json");
110
+ if (fs3.existsSync(pkgPath)) {
111
+ const pkg = JSON.parse(fs3.readFileSync(pkgPath, "utf8"));
112
+ if (pkg.name && typeof pkg.name === "string") {
113
+ return pkg.name.replace(/\W+/g, "");
114
+ }
115
+ }
116
+ } catch (err) {
117
+ console.warn("[plugin-mf-auto] Cannot read package.json:", err);
118
+ }
119
+ return void 0;
120
+ }
121
+ function pluginMFAuto(opts) {
107
122
  const {
108
- name,
109
- filename = "static/js/remoteEntry.js",
110
123
  baseDir = "src",
111
124
  globs = ["src/components/**/*.{ts,tsx}"],
112
125
  exposesMode = "both",
113
126
  envPrefix = "REMOTE_",
114
127
  manifestPathOrUrl,
115
- autoShareReact = true
128
+ autoWhenEmpty = true,
129
+ mf
116
130
  } = opts;
131
+ if (!mf || typeof mf !== "object") {
132
+ throw new Error(
133
+ '[plugin-mf-auto] "mf" options is required and must be an object.'
134
+ );
135
+ }
117
136
  return {
118
137
  name: "plugin-mf-auto",
119
138
  apply: "build",
120
139
  async setup(api) {
121
140
  const { pluginModuleFederation } = await import("@module-federation/rsbuild-plugin");
122
- async function getExposes() {
141
+ const getExposes = async () => {
123
142
  if (exposesMode === "jsdoc")
124
143
  return collectAutoExposes({ baseDir, globs });
125
144
  if (exposesMode === "wrapper")
@@ -127,64 +146,34 @@ function pluginCore(opts = {}) {
127
146
  const a = await collectAutoExposes({ baseDir, globs });
128
147
  const b = await collectAutoExposesWithWrapper({ baseDir, globs });
129
148
  return { ...a, ...b };
130
- }
131
- async function getRemotes() {
132
- return collectAutoRemotes({ envPrefix, manifestPathOrUrl });
133
- }
149
+ };
150
+ const getRemotes = async () => collectAutoRemotes({ envPrefix, manifestPathOrUrl });
134
151
  api.modifyRsbuildConfig(async (config) => {
135
- const [exposes, remotes] = await Promise.all([
136
- getExposes(),
137
- getRemotes()
138
- ]);
139
- const sharedDefaults = autoShareReact ? {
140
- react: {
141
- singleton: true,
142
- eager: true,
143
- requiredVersion: false
144
- },
145
- "react-dom": {
146
- singleton: true,
147
- eager: true,
148
- requiredVersion: false
149
- },
150
- "react-router": {
151
- singleton: true,
152
- eager: true,
153
- requiredVersion: false
154
- },
155
- "react-router-dom": {
156
- singleton: true,
157
- eager: true,
158
- requiredVersion: false
159
- },
160
- "@azure/msal-react": {
161
- singleton: true,
162
- eager: true,
163
- requiredVersion: false
164
- },
165
- "@azure/msal-browser": {
166
- singleton: true,
167
- eager: true,
168
- requiredVersion: false
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 };
157
+ }
158
+ }
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 };
169
163
  }
170
- } : void 0;
164
+ }
165
+ if (!mfMerged.name) {
166
+ mfMerged.name = getPackageName(api.context.rootPath) || path3.basename(api.context.rootPath).replace(/\W+/g, "");
167
+ }
171
168
  config.plugins ||= [];
172
- config.plugins.push(
173
- pluginModuleFederation({
174
- name: name ?? path3.basename(api.context.rootPath).replace(/\W+/g, ""),
175
- filename,
176
- exposes,
177
- remotes,
178
- shared: sharedDefaults
179
- })
180
- );
169
+ config.plugins.push(pluginModuleFederation(mfMerged));
181
170
  return config;
182
171
  });
183
172
  }
184
173
  };
185
174
  }
186
- var index_default = pluginCore;
175
+ var index_default = pluginMFAuto;
187
176
  export {
188
177
  index_default as default,
189
- pluginCore
178
+ pluginMFAuto
190
179
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ptech-preset",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "Auto Module.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",