Skip to content

Commit ab948c7

Browse files
committed
feat: support config mountElementId
1 parent 75bc491 commit ab948c7

File tree

12 files changed

+37
-21
lines changed

12 files changed

+37
-21
lines changed

.changeset/config.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
"ignore": [
1919
"@examples/hackernews",
2020
"@examples/normal",
21+
"@examples/qiankun-master",
22+
"@examples/qiankun-slave",
2123
"@examples/shadcn-cli",
2224
"@examples/ssr",
2325
"@examples/tailwindcss",
24-
"@examples/with-antdx",
25-
"@examples/qiankun-master",
26-
"@examples/qiankun-slave"
26+
"@examples/with-antdx"
2727
]
2828
}

.changeset/shy-countries-compare.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@umijs/tnf': patch
3+
---
4+
5+
feat: support config mountElementId

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ The configuration passed to lessLoader.
140140

141141
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.
142142

143+
### mountElementId
144+
145+
- Type: `string`
146+
- Default: `'root'`
147+
148+
The mount element id.
149+
143150
### plugins
144151

145152
- Type: `Plugin[]`

examples/qiankun-master/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# qiankun-master
2+
3+
Notice:
4+
5+
- Please run `pnpm run dev` in `qiankun-slave` first.

examples/qiankun-slave/.tnfrc.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
export default {};
1+
export default {
2+
mountElementId: 'sub-app-container',
3+
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"umd": "foooo-[name]"
2+
"umd": "foooo"
33
}

examples/qiankun-slave/src/index.html

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/config/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export async function loadConfig(opts: ConfigOpts): Promise<Config> {
4141
['click-to-react-component', require.resolve('click-to-react-component')],
4242
...(config.alias || []),
4343
];
44+
config.mountElementId = config.mountElementId || 'root';
4445
return config;
4546
}
4647

src/config/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export const ConfigSchema = z
7171
plugins: z.array(z.any()).optional(),
7272
})
7373
.optional(),
74+
mountElementId: z.string().optional(),
7475
plugins: z.array(PluginSchema).optional(),
7576
reactCompiler: z
7677
.object({

src/html.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ test('should use default HTML when document.html does not exist', async () => {
3939
apply: vi.fn().mockImplementation(({ memo }) => Promise.resolve(memo)),
4040
},
4141
pluginContext: {},
42+
config: {
43+
mountElementId: 'root',
44+
},
4245
} as any;
4346
const result = await buildHtml(mockContext);
4447
expect(result).toContain('<div id="root"></div>');

src/html.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const DEFAULT_HTML = `
1414
<link rel="stylesheet" href="/client.css" />
1515
</head>
1616
<body>
17-
<div id="root"></div>
17+
<div id="<%= mountElementId %>"></div>
1818
<script src="/client.js"></script>
1919
</body>
2020
</html>
@@ -29,9 +29,13 @@ export async function buildHtml(ctx: Context) {
2929
);
3030
// TODO: support index.ejs
3131
const htmlPath = path.join(cwd, 'src', 'index.html');
32-
const html = fs.existsSync(htmlPath)
32+
let html = fs.existsSync(htmlPath)
3333
? fs.readFileSync(htmlPath, 'utf-8')
3434
: DEFAULT_HTML;
35+
html = html.replace(
36+
/<%= mountElementId %>/g,
37+
ctx.config.mountElementId || 'root',
38+
);
3539
// TODO: support HtmlTagDescriptor[] & { html: string, tags: HtmlTagDescriptor[] }
3640
const result = await ctx.pluginManager.apply({
3741
hook: 'transformIndexHtml',

src/sync/write_client_entry.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export function createClient() {
4343
const srcClientPath = path.join(cwd, 'src/client.tsx');
4444
const clientPath = fs.existsSync(srcClientPath) ? srcClientPath : './client';
4545
const relativeClientPath = path.relative(cwd, clientPath);
46+
const mountElementId = config.mountElementId!;
4647
writeFileSync(
4748
clientEntry,
4849
`
@@ -96,7 +97,7 @@ if (client.createClient) {
9697
if (window.__TSR__) {
9798
ReactDOM.hydrateRoot(document, elements);
9899
} else {
99-
ReactDOM.createRoot(document.getElementById('root')!).render(elements);
100+
ReactDOM.createRoot(document.getElementById('${mountElementId}')!).render(elements);
100101
}
101102
}
102103
`,

0 commit comments

Comments
 (0)