Skip to content

Commit

Permalink
feat: support config mountElementId
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Dec 20, 2024
1 parent 75bc491 commit ab948c7
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 21 deletions.
6 changes: 3 additions & 3 deletions .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
"ignore": [
"@examples/hackernews",
"@examples/normal",
"@examples/qiankun-master",
"@examples/qiankun-slave",
"@examples/shadcn-cli",
"@examples/ssr",
"@examples/tailwindcss",
"@examples/with-antdx",
"@examples/qiankun-master",
"@examples/qiankun-slave"
"@examples/with-antdx"
]
}
5 changes: 5 additions & 0 deletions .changeset/shy-countries-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@umijs/tnf': patch
---

feat: support config mountElementId
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ The configuration passed to lessLoader.

In addition to supporting numbers, delay also supports string ranges, such as delay: '500-1000', which randomly selects a value between 500ms and 1000ms.And allowing the configuration to be overridden by the url parameter, such as /api/users?delay=3000.

### mountElementId

- Type: `string`
- Default: `'root'`

The mount element id.

### plugins

- Type: `Plugin[]`
Expand Down
5 changes: 5 additions & 0 deletions examples/qiankun-master/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# qiankun-master

Notice:

- Please run `pnpm run dev` in `qiankun-slave` first.
4 changes: 3 additions & 1 deletion examples/qiankun-slave/.tnfrc.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export default {};
export default {
mountElementId: 'sub-app-container',
};
2 changes: 1 addition & 1 deletion examples/qiankun-slave/mako.config.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"umd": "foooo-[name]"
"umd": "foooo"
}
13 changes: 0 additions & 13 deletions examples/qiankun-slave/src/index.html

This file was deleted.

1 change: 1 addition & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export async function loadConfig(opts: ConfigOpts): Promise<Config> {
['click-to-react-component', require.resolve('click-to-react-component')],
...(config.alias || []),
];
config.mountElementId = config.mountElementId || 'root';
return config;
}

Expand Down
1 change: 1 addition & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const ConfigSchema = z
plugins: z.array(z.any()).optional(),
})
.optional(),
mountElementId: z.string().optional(),
plugins: z.array(PluginSchema).optional(),
reactCompiler: z
.object({
Expand Down
3 changes: 3 additions & 0 deletions src/html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ test('should use default HTML when document.html does not exist', async () => {
apply: vi.fn().mockImplementation(({ memo }) => Promise.resolve(memo)),
},
pluginContext: {},
config: {
mountElementId: 'root',
},
} as any;
const result = await buildHtml(mockContext);
expect(result).toContain('<div id="root"></div>');
Expand Down
8 changes: 6 additions & 2 deletions src/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const DEFAULT_HTML = `
<link rel="stylesheet" href="/client.css" />
</head>
<body>
<div id="root"></div>
<div id="<%= mountElementId %>"></div>
<script src="/client.js"></script>
</body>
</html>
Expand All @@ -29,9 +29,13 @@ export async function buildHtml(ctx: Context) {
);
// TODO: support index.ejs
const htmlPath = path.join(cwd, 'src', 'index.html');
const html = fs.existsSync(htmlPath)
let html = fs.existsSync(htmlPath)
? fs.readFileSync(htmlPath, 'utf-8')
: DEFAULT_HTML;
html = html.replace(
/<%= mountElementId %>/g,
ctx.config.mountElementId || 'root',
);
// TODO: support HtmlTagDescriptor[] & { html: string, tags: HtmlTagDescriptor[] }
const result = await ctx.pluginManager.apply({
hook: 'transformIndexHtml',
Expand Down
3 changes: 2 additions & 1 deletion src/sync/write_client_entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function createClient() {
const srcClientPath = path.join(cwd, 'src/client.tsx');
const clientPath = fs.existsSync(srcClientPath) ? srcClientPath : './client';
const relativeClientPath = path.relative(cwd, clientPath);
const mountElementId = config.mountElementId!;
writeFileSync(
clientEntry,
`
Expand Down Expand Up @@ -96,7 +97,7 @@ if (client.createClient) {
if (window.__TSR__) {
ReactDOM.hydrateRoot(document, elements);
} else {
ReactDOM.createRoot(document.getElementById('root')!).render(elements);
ReactDOM.createRoot(document.getElementById('${mountElementId}')!).render(elements);
}
}
`,
Expand Down

0 comments on commit ab948c7

Please sign in to comment.