11import { mkdir , writeFile } from 'node:fs/promises' ;
22import { join } from 'node:path' ;
3- import { STATIC_DIR , VIRTUAL_DEMO_DIR } from './constants' ;
3+ import { VIRTUAL_DEMO_DIR } from './constants' ;
44import type { CustomEntry , DemoInfo } from './types' ;
55import { toValidVarName } from './utils' ;
66
7+ function reactEntry ( { demoPath } : CustomEntry ) {
8+ return `
9+ import { createRoot } from 'react-dom/client';
10+ import Demo from ${ JSON . stringify ( demoPath ) } ;
11+ const container = document.getElementById('root');
12+ createRoot(container).render(<Demo />);
13+ ` ;
14+ }
15+
716export async function generateEntry (
817 globalDemos : DemoInfo ,
918 framework : 'react' | 'solid' ,
1019 customEntry ?: ( meta : CustomEntry ) => string ,
1120) {
1221 const sourceEntry : Record < string , string > = { } ;
13- const entryCssPath = join ( STATIC_DIR , 'global-styles' , 'entry.css' ) ;
22+
23+ const generateEntry = ( meta : CustomEntry ) => {
24+ return customEntry
25+ ? customEntry ( meta )
26+ : framework === 'react'
27+ ? reactEntry ( meta )
28+ : '' ;
29+ } ;
1430
1531 await mkdir ( VIRTUAL_DEMO_DIR , { recursive : true } ) ;
1632
@@ -25,21 +41,7 @@ export async function generateEntry(
2541 const followPromiseList = followDemos . map ( async demo => {
2642 const { id, path : demoPath } = demo ;
2743 const entry = join ( VIRTUAL_DEMO_DIR , `${ id } .entry.tsx` ) ;
28- const reactEntry = `
29- import { createRoot } from 'react-dom/client';
30- import ${ JSON . stringify ( entryCssPath ) } ;
31- import Demo from ${ JSON . stringify ( demoPath ) } ;
32- const container = document.getElementById('root');
33- createRoot(container).render(<Demo />);
34- ` ;
35- const entryContent = customEntry
36- ? customEntry ( {
37- entryCssPath,
38- demoPath,
39- } )
40- : framework === 'react'
41- ? reactEntry
42- : '' ;
44+ const entryContent = generateEntry ( { demoPath } ) ;
4345 await writeFile ( entry , entryContent ) ;
4446 sourceEntry [ id ] = entry ;
4547 } ) ;
@@ -53,9 +55,8 @@ export async function generateEntry(
5355 if ( fixedDemos . length === 0 ) {
5456 return ;
5557 }
56- const reactContent = `
57- import { createRoot } from 'react-dom/client';
58- import ${ JSON . stringify ( entryCssPath ) } ;
58+
59+ const appContent = `
5960 ${ fixedDemos
6061 . map ( ( demo , index ) => {
6162 return `import Demo_${ index } from ${ JSON . stringify ( demo . path ) } ` ;
@@ -73,14 +74,18 @@ export async function generateEntry(
7374 </div>
7475 )
7576 }
76- const container = document.getElementById('root');
77- createRoot(container).render(<App />);
77+ export default App;
7878 ` ;
7979
80- const renderContent = reactContent ;
8180 const id = `_${ toValidVarName ( pageName ) } ` ;
81+ const demoPath = join ( VIRTUAL_DEMO_DIR , `${ id } .app.tsx` ) ;
82+ const entryContent = generateEntry ( { demoPath } ) ;
8283 const entry = join ( VIRTUAL_DEMO_DIR , `${ id } .entry.tsx` ) ;
83- await writeFile ( entry , renderContent ) ;
84+
85+ await Promise . all ( [
86+ writeFile ( demoPath , appContent ) ,
87+ writeFile ( entry , entryContent ) ,
88+ ] ) ;
8489 sourceEntry [ id ] = entry ;
8590 } ) ( ) ;
8691
0 commit comments