@@ -7,6 +7,7 @@ import { fileURLToPath } from 'node:url';
77const __filename = fileURLToPath ( import . meta. url ) ;
88const __dirname = path . dirname ( __filename ) ;
99const distDir = path . resolve ( __dirname , '..' , 'dist' ) ;
10+ const SELF_PACKAGE_NAME = '@framers/agentos' ;
1011
1112function collectJsFiles ( dirPath ) {
1213 const entries = fs . readdirSync ( dirPath , { withFileTypes : true } ) ;
@@ -23,6 +24,25 @@ function collectJsFiles(dirPath) {
2324}
2425
2526function resolveSpecifier ( filePath , specifier ) {
27+ if ( specifier === SELF_PACKAGE_NAME || specifier . startsWith ( `${ SELF_PACKAGE_NAME } /` ) ) {
28+ const relativeExportPath =
29+ specifier === SELF_PACKAGE_NAME ? 'index' : specifier . slice ( SELF_PACKAGE_NAME . length + 1 ) ;
30+ const targetCandidates = [
31+ path . resolve ( distDir , `${ relativeExportPath } .js` ) ,
32+ path . resolve ( distDir , relativeExportPath , 'index.js' ) ,
33+ ] ;
34+ const targetPath = targetCandidates . find ( ( candidate ) => fs . existsSync ( candidate ) ) ;
35+ if ( ! targetPath ) {
36+ return specifier ;
37+ }
38+
39+ let relativePath = path . relative ( path . dirname ( filePath ) , targetPath ) . replace ( / \\ / g, '/' ) ;
40+ if ( ! relativePath . startsWith ( '.' ) ) {
41+ relativePath = `./${ relativePath } ` ;
42+ }
43+ return relativePath ;
44+ }
45+
2646 if ( ! specifier . startsWith ( '.' ) ) {
2747 return specifier ;
2848 }
@@ -52,8 +72,8 @@ function rewriteSpecifiers(filePath) {
5272 let changed = false ;
5373
5474 const patterns = [
55- / ( f r o m \s + [ ' " ] ) ( \. { 1 , 2 } \/ [ ^ ' " ] + ) ( [ ' " ] ) / g ,
56- / ( i m p o r t \( \s * [ ' " ] ) ( \. { 1 , 2 } \/ [ ^ ' " ] + ) ( [ ' " ] \s * \) ) / g
75+ / ( ^ \s * (?: i m p o r t | e x p o r t ) \s [ ^ ' " \n ] * f r o m \s + [ ' " ] ) ( [ ^ ' " ] + ) ( [ ' " ] ) / gm ,
76+ / ( i m p o r t \( \s * [ ' " ] ) ( [ ^ ' " ] + ) ( [ ' " ] \s * \) ) / g
5777 ] ;
5878
5979 for ( const pattern of patterns ) {
@@ -101,4 +121,3 @@ console.log(`[agentos fix-esm-imports] Processed ${jsFiles.length} files under $
101121if ( copiedExtensionSecrets ) {
102122 console . log ( '[agentos fix-esm-imports] Mirrored extension-secrets.json into dist/config for public package exports.' ) ;
103123}
104-
0 commit comments