Skip to content

Commit

Permalink
chore: modify tempplate
Browse files Browse the repository at this point in the history
  • Loading branch information
2heal1 committed Jan 21, 2025
1 parent 5af6ae0 commit a533fff
Show file tree
Hide file tree
Showing 21 changed files with 107 additions and 444 deletions.
7 changes: 5 additions & 2 deletions packages/create-module-federation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Create a new Module Federation project",
"public": true,
"sideEffects": false,
"version": "0.0.0",
"version": "0.0.0-1737428758075",
"license": "MIT",
"repository": {
"type": "git",
Expand All @@ -18,11 +18,14 @@
"build": "rslib build"
},
"main": "./dist/index.js",
"engines": {
"node": ">=16.7.0"
},
"bin": {
"create-module-federation": "./dist/index.js"
},
"files": [
"template",
"templates",
"dist"
],
"dependencies": {
Expand Down
19 changes: 9 additions & 10 deletions packages/create-module-federation/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,16 @@
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"executor": "nx:run-commands",
"options": {
"jestConfig": "packages/create-module-federation/jest.config.js",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
"parallel": false,
"dependsOn": [
{
"target": "build",
"dependencies": true
}
],
"commands": ["echo 'no test'"]
}
},
"pre-release": {
Expand Down
76 changes: 24 additions & 52 deletions packages/create-module-federation/src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
import {
cancel,
isCancel,
Expand All @@ -18,6 +19,10 @@ import { logger } from 'rslog';
import { FsMaterial } from './materials/FsMaterial';
import { HandlebarsAPI } from './handlebars';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const packageDir = path.resolve(__dirname, '..');

type Argv = {
help?: boolean;
dir?: string;
Expand Down Expand Up @@ -93,9 +98,11 @@ function checkCancel<T>(value: unknown) {
* 5. Input: '/root/path/to/foo'
* Output: folder -> `'/root/path/to/foo'` folder, `package.json#name` -> `foo`
*/
function formatDir(input: string) {
function formatProjectName(input: string) {
const formatted = input.trim().replace(/\/+$/g, '');
return {
targetDir: input.trim().replace(/\/+$/g, ''),
packageName: input,
targetDir: formatted,
};
}

Expand All @@ -118,37 +125,6 @@ async function getAppTemplateName(
return `${template}-ts`;
}

let providerInfo: ProviderInfo = {
name: '',
entry: '',
};

if (roleType === 'consumer') {
const providerName = checkCancel<string>(
await text({
message:
'Please input your provider name (You can skip by press "enter"):',
defaultValue: '',
}),
);

if (providerName) {
providerInfo.name = providerName;

const providerEntry = checkCancel<string>(
await text({
message: `Please input your provider("${providerName}") entry:`,
validate(value) {
if (value.length === 0) {
return 'Entry is required';
}
},
}),
);
providerInfo.entry = providerEntry;
}
}

return `${roleType}-${framework}-ts`;
}

Expand Down Expand Up @@ -187,7 +163,7 @@ async function getLibTemplateName({ template }: Argv) {
return `${roleType}-${templateName}-ts`;
}

return `${roleType}-${templateName}-[${Object.keys(tools)}]-ts`;
return `${roleType}-${templateName}-${tools[0]}-ts`;
}

function getTemplateName(
Expand Down Expand Up @@ -263,7 +239,7 @@ async function forgeTemplate({
},
argv,
);
const material = new FsMaterial(__dirname);
const material = new FsMaterial(packageDir);

const renderTemplate = async (templateDir: string) => {
const templatePattern = `${templateDir}**/*`;
Expand Down Expand Up @@ -331,24 +307,20 @@ export async function create({
const pkgInfo = pkgFromUserAgent(process.env['npm_config_user_agent']);
const pkgManager = pkgInfo ? pkgInfo.name : 'npm';

const mfName =
argv.dir ??
checkCancel<string>(
await text({
message: 'Please input Module Federation name:',
placeholder: 'mf-project-name',
defaultValue: 'mf-project-name',
validate(value) {
if (value.length === 0) {
return 'Name is required';
}
},
}),
);

const dir = argv.dir || 'mf-project';
const mfName = checkCancel<string>(
await text({
message: 'Please input Module Federation name:',
placeholder: 'mf_project_name',
defaultValue: 'mf_project_name',
validate(value) {
if (value.length === 0) {
return 'Name is required';
}
},
}),
);

const { targetDir } = formatDir(dir);
const { targetDir } = formatProjectName(path.join(argv.dir || '', mfName));
const distFolder = path.isAbsolute(targetDir)
? targetDir
: path.join(cwd, targetDir);
Expand Down
4 changes: 3 additions & 1 deletion packages/create-module-federation/src/handlebars/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export class HandlebarsAPI {
}
await outputFs(
target,
renderString(resourceValue.content, parameters),
templateResource.resourceKey.endsWith('.handlebars')
? renderString(resourceValue.content, parameters)
: resourceValue.content,
outputFilePath,
{ encoding: 'utf-8' },
);
Expand Down
1 change: 1 addition & 0 deletions packages/create-module-federation/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env node
import { create } from './create';

interface Template {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createModuleFederationConfig } from '@module-federation/modern-js';
export default createModuleFederationConfig({
name: '{{mfName}}',
remotes: {
'provider': 'provider@http://localhost:8080/mf-manifest.json',
'provider': 'rslib_provider@https://unpkg.com/module-federation-rslib-provider@latest/dist/mf/mf-manifest.json',
},
shared: {
react: { singleton: true },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Helmet } from '@modern-js/runtime/head';
import './index.css';
import { ComponentInspector } from '../components/ComponentInspector';
import Header from 'provider/Header';
import Description from 'provider/Description';
import Footer from 'provider/Footer';
Expand All @@ -16,17 +15,9 @@ const Index = () => (
</Helmet>

<div className="landing-page">
<ComponentInspector componentName="Header" mfName="provider">
<Header />
</ComponentInspector>

<ComponentInspector componentName="Description" mfName="provider">
<Description />
</ComponentInspector>

<ComponentInspector componentName="Footer" mfName="provider">
<Footer />
</ComponentInspector>
<Header />
<Description />
<Footer />
</div>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createModuleFederationConfig } from '@module-federation/rsbuild-plugin';

export default createModuleFederationConfig({
name: '{{mfName}}',
remotes: {
'provider': 'rslib_provider@https://unpkg.com/module-federation-rslib-provider@latest/dist/mf/mf-manifest.json',
},
shared: {
react: { singleton: true },
'react-dom': { singleton: true },
},
});
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
import './App.css';
import { ComponentInspector } from './components/ComponentInspector';
import Header from 'provider/Header';
import Description from 'provider/Description';
import Footer from 'provider/Footer';

const App = () => {
return (
<div className="content">
<ComponentInspector componentName="Header" mfName="provider">
<Header />
</ComponentInspector>

<ComponentInspector componentName="Description" mfName="provider">
<Description />
</ComponentInspector>

<ComponentInspector componentName="Footer" mfName="provider">
<Footer />
</ComponentInspector>
<Header />
<Description />
<Footer />
</div>
);
};
Expand Down

This file was deleted.

Loading

0 comments on commit a533fff

Please sign in to comment.