11import fs from "fs" ;
22import path from "path" ;
33import { normalizePath } from "vite" ;
4+ import MagicString from "magic-string" ;
45
56/**
67 * OpenScript Component Auto-Import Plugin
@@ -59,7 +60,12 @@ export function openScriptComponentPlugin(options = {}) {
5960 load ( id ) {
6061 if ( id === resolvedVirtualModuleId ) {
6162 // Generate virtual module that imports all components
62- return generateVirtualModule ( componentsDir , components , autoRegister ) ;
63+ return generateVirtualModule (
64+ config . root ,
65+ componentsDir ,
66+ components ,
67+ autoRegister
68+ ) ;
6369 }
6470 } ,
6571
@@ -74,32 +80,39 @@ export function openScriptComponentPlugin(options = {}) {
7480 const className = classMatch [ 1 ] ;
7581
7682 // If code already sets this.name explicitly, skip (simple check)
77- // We still use the runtime check (!this.name) to be safe, but this avoids double injection if we run multiple times
7883 if ( code . includes ( `this.name = "${ className } "` ) ) return ;
7984
85+ const s = new MagicString ( code ) ;
86+
8087 if ( code . includes ( "constructor" ) ) {
8188 // Inject after super()
82- // Matches super(...) or super() with optional semicolon
83- return code . replace (
84- / ( s u p e r \s * \( [ ^ ) ] * \) \s * ; ? ) / ,
85- `$1\n if (!this.name) this.name = "${ className } ";`
86- ) ;
89+ const superMatch = code . match ( / ( s u p e r \s * \( [ ^ ) ] * \) \s * ; ? ) / ) ;
90+ if ( superMatch ) {
91+ const index = superMatch . index + superMatch [ 0 ] . length ;
92+ s . appendRight (
93+ index ,
94+ `\n if (!this.name) this.name = "${ className } ";`
95+ ) ;
96+ }
8797 } else {
8898 // No constructor, inject one
89- // Find the first opening brace after class definition
9099 const classDef = classMatch [ 0 ] ;
91100 const openBraceIndex = code . indexOf ( "{" , code . indexOf ( classDef ) ) ;
92101
93102 if ( openBraceIndex !== - 1 ) {
94- return (
95- code . slice ( 0 , openBraceIndex + 1 ) +
96- `\n constructor() { super(); this.name = "${ className } "; }` +
97- code . slice ( openBraceIndex + 1 )
103+ s . appendRight (
104+ openBraceIndex + 1 ,
105+ `\n constructor() { super(); this.name = "${ className } "; }`
98106 ) ;
99107 }
100108 }
101109
102- return code ;
110+ if ( s . hasChanged ( ) ) {
111+ return {
112+ code : s . toString ( ) ,
113+ map : s . generateMap ( { source : id , includeContent : true } ) ,
114+ } ;
115+ }
103116 } ,
104117
105118 // HMR support
@@ -195,9 +208,14 @@ export {};
195208/**
196209 * Generate virtual module content that imports and registers all components
197210 */
198- function generateVirtualModule ( componentsDir , components , autoRegister ) {
211+ function generateVirtualModule ( root , componentsDir , components , autoRegister ) {
199212 const imports = components
200- . map ( ( c ) => `import ${ c . name } from '../${ componentsDir } /${ c . path } ';` )
213+ . map ( ( c ) => {
214+ const absolutePath = normalizePath (
215+ path . resolve ( root , componentsDir , c . path )
216+ ) ;
217+ return `import ${ c . name } from '${ absolutePath } ';` ;
218+ } )
201219 . join ( "\n" ) ;
202220
203221 const exports = components . map ( ( c ) => c . name ) . join ( ", " ) ;
0 commit comments