create-module-federation 0.0.0 → 0.11.0

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.
Files changed (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +40 -0
  3. package/dist/LICENSE +21 -0
  4. package/dist/index.js +366 -0
  5. package/package.json +44 -8
  6. package/templates/consumer-modern-ts/modern.config.ts +15 -0
  7. package/templates/consumer-modern-ts/module-federation.config.ts.handlebars +12 -0
  8. package/templates/consumer-modern-ts/src/routes/page.tsx +21 -0
  9. package/templates/consumer-rsbuild-ts/module-federation.config.ts.handlebars +12 -0
  10. package/templates/consumer-rsbuild-ts/rsbuild.config.ts +8 -0
  11. package/templates/consumer-rsbuild-ts/src/App.tsx +12 -0
  12. package/templates/lib-common/module-federation.config.ts.handlebars +17 -0
  13. package/templates/lib-common/rslib.config.ts +52 -0
  14. package/templates/lib-common/src/index.css +53 -0
  15. package/templates/lib-common/src/index.tsx +19 -0
  16. package/templates/lib-common/tsconfig.json +14 -0
  17. package/templates/modern-common/.browserslistrc +5 -0
  18. package/templates/modern-common/.nvmrc +1 -0
  19. package/templates/modern-common/.vscode/extensions.json +14 -0
  20. package/templates/modern-common/.vscode/settings.json +88 -0
  21. package/templates/modern-common/README.md +37 -0
  22. package/templates/modern-common/biome.json +34 -0
  23. package/templates/modern-common/package.json.handlebars +45 -0
  24. package/templates/modern-common/src/modern-app-env.d.ts +3 -0
  25. package/templates/modern-common/src/modern.runtime.ts +3 -0
  26. package/templates/modern-common/src/routes/index.css +41 -0
  27. package/templates/modern-common/src/routes/layout.tsx +9 -0
  28. package/templates/modern-common/tsconfig.json +15 -0
  29. package/templates/provider-modern-ts/modern.config.ts +18 -0
  30. package/templates/provider-modern-ts/module-federation.config.ts.handlebars +12 -0
  31. package/templates/provider-modern-ts/src/components/ProviderComponent.css +52 -0
  32. package/templates/provider-modern-ts/src/components/ProviderComponent.tsx +19 -0
  33. package/templates/provider-modern-ts/src/routes/page.tsx +21 -0
  34. package/templates/provider-rsbuild-ts/module-federation.config.ts.handlebars +12 -0
  35. package/templates/provider-rsbuild-ts/rsbuild.config.ts +11 -0
  36. package/templates/provider-rsbuild-ts/src/App.tsx +12 -0
  37. package/templates/provider-rsbuild-ts/src/components/ProviderComponent.css +52 -0
  38. package/templates/provider-rsbuild-ts/src/components/ProviderComponent.tsx +19 -0
  39. package/templates/provider-rslib-storybook-ts/.storybook/main.ts.handlebars +41 -0
  40. package/templates/provider-rslib-storybook-ts/README.md +27 -0
  41. package/templates/provider-rslib-storybook-ts/package.json.handlebars +50 -0
  42. package/templates/provider-rslib-storybook-ts/stories/Index.stories.tsx +11 -0
  43. package/templates/provider-rslib-ts/README.md +19 -0
  44. package/templates/provider-rslib-ts/package.json.handlebars +35 -0
  45. package/templates/rsbuild-common/README.md +29 -0
  46. package/templates/rsbuild-common/package.json.handlebars +22 -0
  47. package/templates/rsbuild-common/public/.gitkeep +0 -0
  48. package/templates/rsbuild-common/src/App.css +15 -0
  49. package/templates/rsbuild-common/src/bootstrap.tsx +13 -0
  50. package/templates/rsbuild-common/src/env.d.ts +1 -0
  51. package/templates/rsbuild-common/src/index.tsx +1 -0
  52. package/templates/rsbuild-common/tsconfig.json +26 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present hanric(2heal1)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,40 @@
1
+ <p align="center">
2
+ <a href="https://module-federation.io/" target="blank"><img src="https://module-federation.io/module-federation.svg" alt="Module Federation 2.0" /></a>
3
+ </p>
4
+
5
+ # create-module-federation
6
+
7
+ Create a new Module Federation project.
8
+
9
+ <p>
10
+ <a href="https://npmjs.com/package/create-module-federation">
11
+ <img src="https://img.shields.io/npm/v/create-module-federation?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
12
+ </a>
13
+ <img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" />
14
+ <a href="https://npmcharts.com/compare/create-module-federation?minimal=true"><img src="https://img.shields.io/npm/dm/create-module-federation.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="downloads" /></a>
15
+ </p>
16
+
17
+ ## Usage
18
+
19
+ Using `npm create`:
20
+
21
+ ```bash
22
+ npm create module-federation@latest
23
+ ```
24
+
25
+ Using CLI flags:
26
+
27
+ ```bash
28
+ npx create-module-federation --dir my-project --template provider-modern --name provider
29
+
30
+ # Using abbreviations
31
+ npx create-module-federation -d my-project -t provider-modern -n provider
32
+ ```
33
+
34
+ ## Documentation
35
+
36
+ See [Documentation](https://module-federation.io/guide/start/quick-start).
37
+
38
+ ## License
39
+
40
+ [MIT](https://github.com/module-federation/core/blob/main/LICENSE).
package/dist/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present hanric(2heal1)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.js ADDED
@@ -0,0 +1,366 @@
1
+ #!/usr/bin/env node
2
+ import * as __WEBPACK_EXTERNAL_MODULE_path__ from "path";
3
+ import * as __WEBPACK_EXTERNAL_MODULE_fs__ from "fs";
4
+ import * as __WEBPACK_EXTERNAL_MODULE_child_process__ from "child_process";
5
+ import * as __WEBPACK_EXTERNAL_MODULE_url__ from "url";
6
+ import * as __WEBPACK_EXTERNAL_MODULE__clack_prompts_3cae1695__ from "@clack/prompts";
7
+ import * as __WEBPACK_EXTERNAL_MODULE_minimist__ from "minimist";
8
+ import * as __WEBPACK_EXTERNAL_MODULE_rslog__ from "rslog";
9
+ import * as __WEBPACK_EXTERNAL_MODULE_glob__ from "glob";
10
+ import * as __WEBPACK_EXTERNAL_MODULE_fs_extra_ce68a66b__ from "fs-extra";
11
+ import * as __WEBPACK_EXTERNAL_MODULE_handlebars__ from "handlebars";
12
+ const IMAGE_EXT_LIST = [
13
+ '.jpg',
14
+ '.jpeg',
15
+ '.png',
16
+ '.gif',
17
+ '.bmp',
18
+ '.ico',
19
+ '.icon',
20
+ '.mpt',
21
+ '.psd',
22
+ '.wmf'
23
+ ];
24
+ const FS_RESOURCE = '_mf_fs_resource';
25
+ class FsResource {
26
+ constructor(filePath, resourceKey){
27
+ this._type = FS_RESOURCE;
28
+ this.filePath = filePath;
29
+ this.resourceKey = resourceKey;
30
+ }
31
+ async value() {
32
+ const resourceFileExt = __WEBPACK_EXTERNAL_MODULE_path__["default"].extname(this.filePath);
33
+ if (IMAGE_EXT_LIST.includes(resourceFileExt)) {
34
+ const buffer = await __WEBPACK_EXTERNAL_MODULE_fs_extra_ce68a66b__["default"].readFile(__WEBPACK_EXTERNAL_MODULE_path__["default"].resolve(this.filePath));
35
+ return {
36
+ content: buffer
37
+ };
38
+ }
39
+ const text = await __WEBPACK_EXTERNAL_MODULE_fs_extra_ce68a66b__["default"].readFile(__WEBPACK_EXTERNAL_MODULE_path__["default"].resolve(this.filePath), 'utf8');
40
+ return {
41
+ content: text
42
+ };
43
+ }
44
+ }
45
+ const promisifyGlob = (pattern, options)=>new Promise((resolve, reject)=>{
46
+ (0, __WEBPACK_EXTERNAL_MODULE_glob__["default"])(pattern, options, (err, files)=>null === err ? resolve(files) : reject(err));
47
+ });
48
+ class FsMaterial {
49
+ constructor(basePath){
50
+ this.basePath = basePath;
51
+ }
52
+ get(resourceKey) {
53
+ return new FsResource(__WEBPACK_EXTERNAL_MODULE_path__["default"].resolve(this.basePath, resourceKey), resourceKey);
54
+ }
55
+ async find(globStr, options) {
56
+ const matches = await promisifyGlob(globStr, {
57
+ cwd: __WEBPACK_EXTERNAL_MODULE_path__["default"].resolve(this.basePath),
58
+ nodir: options?.nodir,
59
+ dot: options?.dot,
60
+ ignore: options?.ignore
61
+ });
62
+ return matches.reduce((pre, cur)=>{
63
+ pre[cur] = new FsResource(__WEBPACK_EXTERNAL_MODULE_path__["default"].resolve(this.basePath, cur), cur);
64
+ return pre;
65
+ }, {});
66
+ }
67
+ }
68
+ function renderString(template, fullData) {
69
+ return __WEBPACK_EXTERNAL_MODULE_handlebars__["default"].compile(template)(fullData) || '';
70
+ }
71
+ async function outputFs(file, content, outputPath, options) {
72
+ const filePath = __WEBPACK_EXTERNAL_MODULE_path__["default"].resolve(outputPath, file.toString());
73
+ await __WEBPACK_EXTERNAL_MODULE_fs_extra_ce68a66b__["default"].mkdirp(__WEBPACK_EXTERNAL_MODULE_path__["default"].dirname(filePath));
74
+ await __WEBPACK_EXTERNAL_MODULE_fs_extra_ce68a66b__["default"].writeFile(filePath, content, options);
75
+ }
76
+ class HandlebarsAPI {
77
+ async renderTemplate(templateResource, target, outputFilePath, parameters = {}) {
78
+ if (templateResource._type !== FS_RESOURCE) throw new Error('resource not match');
79
+ const resourceValue = await templateResource.value();
80
+ if ('string' != typeof resourceValue.content) throw new Error(`resource.value is not string, resourceValue=${resourceValue}`);
81
+ await outputFs(target, templateResource.resourceKey.endsWith('.handlebars') ? renderString(resourceValue.content, parameters) : resourceValue.content, outputFilePath, {
82
+ encoding: 'utf-8'
83
+ });
84
+ }
85
+ async renderTemplateDir(material, findGlob, target, outputFilePath, options) {
86
+ const resourceMap = await material.find(findGlob, {
87
+ nodir: true,
88
+ ...options
89
+ });
90
+ await Promise.all(Object.keys(resourceMap).map(async (resourceKey)=>{
91
+ await this.renderTemplate(material.get(resourceKey), target(resourceKey), outputFilePath, options?.parameters);
92
+ }));
93
+ }
94
+ }
95
+ const create_filename = (0, __WEBPACK_EXTERNAL_MODULE_url__.fileURLToPath)(import.meta.url);
96
+ const create_dirname = __WEBPACK_EXTERNAL_MODULE_path__["default"].dirname(create_filename);
97
+ const packageDir = __WEBPACK_EXTERNAL_MODULE_path__["default"].resolve(create_dirname, '..');
98
+ const OTHER_TYPE = {
99
+ zephyr: {
100
+ label: 'zephyr',
101
+ packageName: 'zephyr-apps@latest',
102
+ hint: 'create zephyr-apps@latest'
103
+ }
104
+ };
105
+ function upperFirst(str) {
106
+ return str.charAt(0).toUpperCase() + str.slice(1);
107
+ }
108
+ function logHelpMessage(name, templates) {
109
+ __WEBPACK_EXTERNAL_MODULE_rslog__.logger.log(`
110
+ Usage: create-${name} [options]
111
+
112
+ Options:
113
+
114
+ -h, --help display help for command
115
+ -d, --dir create project in specified directory
116
+ -t, --template specify the template to use
117
+ -n, --name specify the mf name
118
+ -r, --role specify the mf role type: provider or consumer
119
+ --override override files in target directory
120
+
121
+ Templates:
122
+ ${templates.filter((t)=>!t.startsWith('zephyr')).join(', ')}
123
+ `);
124
+ }
125
+ function pkgFromUserAgent(userAgent) {
126
+ if (!userAgent) return;
127
+ const pkgSpec = userAgent.split(' ')[0];
128
+ const pkgSpecArr = pkgSpec.split('/');
129
+ return {
130
+ name: pkgSpecArr[0],
131
+ version: pkgSpecArr[1]
132
+ };
133
+ }
134
+ function cancelAndExit() {
135
+ (0, __WEBPACK_EXTERNAL_MODULE__clack_prompts_3cae1695__.cancel)('Operation cancelled.');
136
+ process.exit(0);
137
+ }
138
+ function checkCancel(value) {
139
+ if ((0, __WEBPACK_EXTERNAL_MODULE__clack_prompts_3cae1695__.isCancel)(value)) cancelAndExit();
140
+ return value;
141
+ }
142
+ function formatProjectName(input) {
143
+ const formatted = input.trim().replace(/\/+$/g, '');
144
+ return {
145
+ packageName: input,
146
+ targetDir: formatted
147
+ };
148
+ }
149
+ function isEmptyDir(path) {
150
+ const files = __WEBPACK_EXTERNAL_MODULE_fs__["default"].readdirSync(path);
151
+ return 0 === files.length || 1 === files.length && '.git' === files[0];
152
+ }
153
+ async function getAppTemplateName({ roleType, framework }, { template }) {
154
+ if (template) return `${template}-ts`;
155
+ return `${roleType}-${framework}-ts`;
156
+ }
157
+ async function getLibTemplateName({ template, role }) {
158
+ if (template) return `${template}-ts`;
159
+ const templateName = checkCancel(await (0, __WEBPACK_EXTERNAL_MODULE__clack_prompts_3cae1695__.select)({
160
+ message: 'Select template',
161
+ options: [
162
+ {
163
+ value: 'rslib',
164
+ label: 'Rslib'
165
+ }
166
+ ]
167
+ }));
168
+ const tools = checkCancel(await (0, __WEBPACK_EXTERNAL_MODULE__clack_prompts_3cae1695__.multiselect)({
169
+ message: 'Select development tools (Use <space> to select, <enter> to continue)',
170
+ required: false,
171
+ options: [
172
+ {
173
+ value: 'storybook',
174
+ label: 'Storybook'
175
+ }
176
+ ].filter(Boolean)
177
+ }));
178
+ const roleType = role || "provider";
179
+ if (!tools || !Object.keys(tools).length) return `${roleType}-${templateName}-ts`;
180
+ return `${roleType}-${templateName}-${tools[0]}-ts`;
181
+ }
182
+ function getTemplateName({ projectType, roleType, framework }, args) {
183
+ if ('app' === projectType) return getAppTemplateName({
184
+ roleType,
185
+ framework
186
+ }, args);
187
+ return getLibTemplateName(args);
188
+ }
189
+ function getTemplateDir(templateName) {
190
+ return `templates/${templateName}/`;
191
+ }
192
+ async function getProjectType(template) {
193
+ if (!template) return checkCancel(await (0, __WEBPACK_EXTERNAL_MODULE__clack_prompts_3cae1695__.select)({
194
+ message: 'Please select the type of project you want to create:',
195
+ options: [
196
+ {
197
+ value: "app",
198
+ label: 'Application'
199
+ },
200
+ {
201
+ value: "lib",
202
+ label: 'Lib'
203
+ },
204
+ {
205
+ value: "zephyr",
206
+ label: 'Zephyr Powered (Learn more at https://zephyr-cloud.io)'
207
+ }
208
+ ]
209
+ }));
210
+ if (template.includes('lib')) return "lib";
211
+ return "app";
212
+ }
213
+ async function forgeTemplate({ projectType, argv, templateParameters, distFolder }) {
214
+ let framework = 'modern';
215
+ let roleType = "provider";
216
+ if (!argv?.template && 'app' === projectType) {
217
+ framework = checkCancel(await (0, __WEBPACK_EXTERNAL_MODULE__clack_prompts_3cae1695__.select)({
218
+ message: 'Select template',
219
+ options: [
220
+ {
221
+ value: 'modern',
222
+ label: 'Modern.js Framework'
223
+ },
224
+ {
225
+ value: 'rsbuild',
226
+ label: 'Rsbuild'
227
+ }
228
+ ]
229
+ }));
230
+ roleType = argv.role || checkCancel(await (0, __WEBPACK_EXTERNAL_MODULE__clack_prompts_3cae1695__.select)({
231
+ message: 'Please select the role of project you want to create:',
232
+ initialValue: 'provider',
233
+ options: [
234
+ {
235
+ value: 'consumer',
236
+ label: 'Consumer'
237
+ },
238
+ {
239
+ value: 'provider',
240
+ label: 'Provider'
241
+ }
242
+ ]
243
+ }));
244
+ }
245
+ const templateName = await getTemplateName({
246
+ projectType,
247
+ framework,
248
+ roleType
249
+ }, argv);
250
+ const material = new FsMaterial(packageDir);
251
+ const renderTemplate = async (templateDir)=>{
252
+ const templatePattern = `${templateDir}**/*`;
253
+ const resourceMap = await material.find(templatePattern, {
254
+ nodir: true,
255
+ dot: true
256
+ });
257
+ const parameters = {
258
+ ...templateParameters
259
+ };
260
+ await Promise.all(Object.keys(resourceMap).map(async (resourceKey)=>{
261
+ const target = resourceKey.replace(templateDir, "").replace('.handlebars', "");
262
+ const handlebarsAPI = new HandlebarsAPI();
263
+ await handlebarsAPI.renderTemplate(material.get(resourceKey), target, distFolder, {
264
+ ...parameters
265
+ });
266
+ }));
267
+ };
268
+ const templateDir = getTemplateDir(templateName);
269
+ let commonTemplateDir = '';
270
+ if ("lib" === projectType) commonTemplateDir = 'templates/lib-common/';
271
+ else if ('app' === projectType) commonTemplateDir = `templates/${framework}-common/`;
272
+ if (commonTemplateDir) await renderTemplate(commonTemplateDir);
273
+ await renderTemplate(templateDir);
274
+ }
275
+ async function create({ name, templates }) {
276
+ const argv = (0, __WEBPACK_EXTERNAL_MODULE_minimist__["default"])(process.argv.slice(2), {
277
+ alias: {
278
+ h: 'help',
279
+ d: 'dir',
280
+ t: 'template',
281
+ n: 'name',
282
+ r: 'role'
283
+ }
284
+ });
285
+ console.log('');
286
+ __WEBPACK_EXTERNAL_MODULE_rslog__.logger.greet(`◆ Create ${upperFirst(name)} Project`);
287
+ if (argv.help) {
288
+ logHelpMessage(name, templates);
289
+ return;
290
+ }
291
+ const cwd = process.cwd();
292
+ const pkgInfo = pkgFromUserAgent(process.env['npm_config_user_agent']);
293
+ const pkgManager = pkgInfo ? pkgInfo.name : 'npm';
294
+ const mfVersion = "0.11.0";
295
+ argv.template = templates.includes(argv.template || '') ? argv.template : void 0;
296
+ const projectType = await getProjectType(argv.template);
297
+ if ("zephyr" === projectType) {
298
+ const zephyrPackage = OTHER_TYPE['zephyr'].packageName;
299
+ const zephyrCommand = `${pkgManager} create ${zephyrPackage}`;
300
+ (0, __WEBPACK_EXTERNAL_MODULE__clack_prompts_3cae1695__.note)(`Running: ${zephyrCommand}`, 'Launching Zephyr setup');
301
+ (0, __WEBPACK_EXTERNAL_MODULE_child_process__.spawnSync)(pkgManager, [
302
+ 'create',
303
+ zephyrPackage
304
+ ], {
305
+ stdio: 'inherit',
306
+ shell: true
307
+ });
308
+ (0, __WEBPACK_EXTERNAL_MODULE__clack_prompts_3cae1695__.outro)('Done.');
309
+ return;
310
+ }
311
+ const mfName = argv.name || checkCancel(await (0, __WEBPACK_EXTERNAL_MODULE__clack_prompts_3cae1695__.text)({
312
+ message: 'Please input Module Federation name:',
313
+ placeholder: 'mf_project_name',
314
+ defaultValue: 'mf_project_name',
315
+ validate (value) {
316
+ if (0 === value.length) return 'Name is required';
317
+ }
318
+ }));
319
+ const { targetDir } = formatProjectName(__WEBPACK_EXTERNAL_MODULE_path__["default"].join(argv.dir || mfName));
320
+ const distFolder = __WEBPACK_EXTERNAL_MODULE_path__["default"].isAbsolute(targetDir) ? targetDir : __WEBPACK_EXTERNAL_MODULE_path__["default"].join(cwd, targetDir);
321
+ if (!argv.override && __WEBPACK_EXTERNAL_MODULE_fs__["default"].existsSync(distFolder) && !isEmptyDir(distFolder)) {
322
+ const option = checkCancel(await (0, __WEBPACK_EXTERNAL_MODULE__clack_prompts_3cae1695__.select)({
323
+ message: `"${targetDir}" is not empty, please choose:`,
324
+ options: [
325
+ {
326
+ value: 'yes',
327
+ label: 'Continue and override files'
328
+ },
329
+ {
330
+ value: 'no',
331
+ label: 'Cancel operation'
332
+ }
333
+ ]
334
+ }));
335
+ if ('no' === option) cancelAndExit();
336
+ }
337
+ await forgeTemplate({
338
+ projectType,
339
+ argv,
340
+ templateParameters: {
341
+ mfName,
342
+ mfVersion
343
+ },
344
+ distFolder
345
+ });
346
+ const nextSteps = [
347
+ `cd ${targetDir}`,
348
+ `${pkgManager} install`,
349
+ `${pkgManager} run ${"lib" === projectType ? 'mf-dev' : 'dev'}`
350
+ ];
351
+ (0, __WEBPACK_EXTERNAL_MODULE__clack_prompts_3cae1695__.note)(nextSteps.join('\n'), 'Next steps');
352
+ (0, __WEBPACK_EXTERNAL_MODULE__clack_prompts_3cae1695__.outro)('Done.');
353
+ }
354
+ const TEMPLATES = [
355
+ 'provider-modern',
356
+ 'provider-rsbuild',
357
+ 'provider-rslib',
358
+ 'provider-rslib-storybook',
359
+ 'consumer-modern',
360
+ 'consumer-rsbuild'
361
+ ];
362
+ create({
363
+ name: 'Module Federation',
364
+ templates: TEMPLATES
365
+ });
366
+ export { TEMPLATES };
package/package.json CHANGED
@@ -1,11 +1,47 @@
1
1
  {
2
2
  "name": "create-module-federation",
3
- "version": "0.0.0",
4
- "main": "index.js",
5
- "scripts": {
6
- "test": "echo \"Error: no test specified\" && exit 1"
3
+ "description": "Create a new Module Federation project",
4
+ "public": true,
5
+ "sideEffects": false,
6
+ "version": "0.11.0",
7
+ "license": "MIT",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/module-federation/core/",
11
+ "directory": "packages/create-module-federation"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "type": "module",
17
+ "main": "./dist/index.js",
18
+ "engines": {
19
+ "node": ">=16.7.0"
20
+ },
21
+ "bin": {
22
+ "create-module-federation": "./dist/index.js"
7
23
  },
8
- "author": "",
9
- "license": "ISC",
10
- "description": ""
11
- }
24
+ "files": [
25
+ "templates",
26
+ "dist"
27
+ ],
28
+ "dependencies": {
29
+ "@clack/prompts": "^0.8.2",
30
+ "execa": "5.1.1",
31
+ "fs-extra": "9.1.0",
32
+ "minimist": "^1.2.8",
33
+ "rslog": "^1.2.3",
34
+ "glob": "7.2.0",
35
+ "handlebars": "4.7.7"
36
+ },
37
+ "devDependencies": {
38
+ "@types/glob": "7.2.0",
39
+ "@types/minimist": "^1.2.5",
40
+ "@types/fs-extra": "9.0.6",
41
+ "@rslib/core": "^0.3.1",
42
+ "rsbuild-plugin-publint": "^0.2.1"
43
+ },
44
+ "scripts": {
45
+ "build": "rslib build"
46
+ }
47
+ }
@@ -0,0 +1,15 @@
1
+ import { appTools, defineConfig } from '@modern-js/app-tools';
2
+ import { moduleFederationPlugin } from '@module-federation/modern-js';
3
+
4
+ // https://modernjs.dev/en/configure/app/usage
5
+ export default defineConfig({
6
+ runtime: {
7
+ router: true,
8
+ },
9
+ plugins: [
10
+ appTools({
11
+ bundler: 'rspack', // Set to 'webpack' to enable webpack
12
+ }),
13
+ moduleFederationPlugin(),
14
+ ],
15
+ });
@@ -0,0 +1,12 @@
1
+ import { createModuleFederationConfig } from '@module-federation/modern-js';
2
+
3
+ export default createModuleFederationConfig({
4
+ name: '{{mfName}}',
5
+ remotes: {
6
+ 'provider': 'rslib_provider@https://unpkg.com/module-federation-rslib-provider@latest/dist/mf/mf-manifest.json',
7
+ },
8
+ shared: {
9
+ react: { singleton: true },
10
+ 'react-dom': { singleton: true },
11
+ },
12
+ });
@@ -0,0 +1,21 @@
1
+ import { Helmet } from '@modern-js/runtime/head';
2
+ import './index.css';
3
+ import Provider from 'provider';
4
+
5
+ const Index = () => (
6
+ <div className="container-box">
7
+ <Helmet>
8
+ <link
9
+ rel="icon"
10
+ type="image/x-icon"
11
+ href="https://lf3-static.bytednsdoc.com/obj/eden-cn/uhbfnupenuhf/favicon.ico"
12
+ />
13
+ </Helmet>
14
+
15
+ <div className="landing-page">
16
+ <Provider />
17
+ </div>
18
+ </div>
19
+ );
20
+
21
+ export default Index;
@@ -0,0 +1,12 @@
1
+ import { createModuleFederationConfig } from '@module-federation/rsbuild-plugin';
2
+
3
+ export default createModuleFederationConfig({
4
+ name: '{{mfName}}',
5
+ remotes: {
6
+ 'provider': 'rslib_provider@https://unpkg.com/module-federation-rslib-provider@latest/dist/mf/mf-manifest.json',
7
+ },
8
+ shared: {
9
+ react: { singleton: true },
10
+ 'react-dom': { singleton: true },
11
+ },
12
+ });
@@ -0,0 +1,8 @@
1
+ import { defineConfig } from '@rsbuild/core';
2
+ import { pluginReact } from '@rsbuild/plugin-react';
3
+ import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
4
+ import moduleFederationConfig from './module-federation.config';
5
+
6
+ export default defineConfig({
7
+ plugins: [pluginReact(), pluginModuleFederation(moduleFederationConfig)],
8
+ });
@@ -0,0 +1,12 @@
1
+ import './App.css';
2
+ import Provider from 'provider';
3
+
4
+ const App = () => {
5
+ return (
6
+ <div className="content">
7
+ <Provider />
8
+ </div>
9
+ );
10
+ };
11
+
12
+ export default App;
@@ -0,0 +1,17 @@
1
+ import {createModuleFederationConfig} from '@module-federation/rsbuild-plugin';
2
+ import pkg from './package.json';
3
+
4
+ export default createModuleFederationConfig({
5
+ name: pkg.name,
6
+ exposes: {
7
+ '.': './src/index.tsx',
8
+ },
9
+ shared: {
10
+ react: {
11
+ singleton: true,
12
+ },
13
+ 'react-dom': {
14
+ singleton: true,
15
+ },
16
+ },
17
+ })
@@ -0,0 +1,52 @@
1
+ import { pluginReact } from '@rsbuild/plugin-react';
2
+ import { defineConfig } from '@rslib/core';
3
+ import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
4
+ import moduleFederationConfig from './module-federation.config';
5
+ import pkg from './package.json';
6
+
7
+ const shared = {
8
+ dts: {
9
+ bundle: false,
10
+ },
11
+ };
12
+
13
+ export default defineConfig({
14
+ lib: [
15
+ {
16
+ ...shared,
17
+ format: 'esm',
18
+ output: {
19
+ distPath: {
20
+ root: './dist/esm',
21
+ },
22
+ },
23
+ },
24
+ {
25
+ ...shared,
26
+ format: 'cjs',
27
+ output: {
28
+ distPath: {
29
+ root: './dist/cjs',
30
+ },
31
+ },
32
+ },
33
+ {
34
+ ...shared,
35
+ format: 'mf',
36
+ output: {
37
+ // set unpkg cdn as assetPrefix if you want to publish
38
+ assetPrefix:
39
+ process.env.NODE_ENV === 'production'
40
+ ? `https://unpkg.com/${pkg.name}@latest/dist/mf/`
41
+ : undefined,
42
+ distPath: {
43
+ root: './dist/mf',
44
+ },
45
+ },
46
+ },
47
+ ],
48
+ server: {
49
+ port: 3001,
50
+ },
51
+ plugins: [pluginReact(), pluginModuleFederation(moduleFederationConfig)],
52
+ });