@@ -27,14 +27,40 @@ const getPackageName = async () => {
2727 return "package-name" ;
2828 }
2929} ;
30-
3130const _addUseStatement = async (
32- basePath : string ,
31+ pathOrFile : string ,
3332 type : "server" | "client" ,
33+ isFile ?: boolean ,
3434) => {
35- const fullPath = path . join ( __dirname , basePath ) ;
36- const files = fs . readdirSync ( fullPath ) ;
35+ const fullPath = path . join ( __dirname , pathOrFile ) ;
36+
37+ // Use provided isFile parameter if available, otherwise detect from extension
38+ const shouldHandleAsFile =
39+ isFile ?? ( fullPath . endsWith ( ".js" ) || fullPath . endsWith ( ".mjs" ) ) ;
3740
41+ if ( shouldHandleAsFile ) {
42+ // Try both .js and .mjs if no extension is provided
43+ const possiblePaths =
44+ fullPath . endsWith ( ".js" ) || fullPath . endsWith ( ".mjs" )
45+ ? [ fullPath ]
46+ : [ `${ fullPath } .js` , `${ fullPath } .mjs` ] ;
47+
48+ for ( const filePath of possiblePaths ) {
49+ if ( fs . existsSync ( filePath ) ) {
50+ let content = await readFile ( filePath , "utf-8" ) ;
51+ content = `"use ${ type } ";\n${ content } ` ;
52+ fs . writeFileSync ( filePath , content , "utf-8" ) ;
53+ }
54+ }
55+ return ;
56+ }
57+
58+ // Handle directory case
59+ if ( ! fs . existsSync ( fullPath ) ) {
60+ throw new Error ( `Directory not found: ${ fullPath } ` ) ;
61+ }
62+
63+ const files = fs . readdirSync ( fullPath ) ;
3864 for ( const file of files ) {
3965 if ( file . endsWith ( ".js" ) || file . endsWith ( ".mjs" ) ) {
4066 const filePath = path . join ( fullPath , file ) ;
@@ -68,7 +94,7 @@ const linkSelf = async () => {
6894export default defineConfig ( {
6995 async onSuccess ( ) {
7096 // If you want need to add a use statement to files, you can use the following code:
71- // await _addUseStatement(' dist/react', ' client' );
97+ await _addUseStatement ( " dist/index" , " client" , true ) ;
7298
7399 await linkSelf ( ) ;
74100 } ,
0 commit comments