Skip to content

Commit cd22430

Browse files
committed
fix(build): rewrite agentos self-package imports
1 parent a472a53 commit cd22430

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

scripts/fix-esm-imports.mjs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { fileURLToPath } from 'node:url';
77
const __filename = fileURLToPath(import.meta.url);
88
const __dirname = path.dirname(__filename);
99
const distDir = path.resolve(__dirname, '..', 'dist');
10+
const SELF_PACKAGE_NAME = '@framers/agentos';
1011

1112
function collectJsFiles(dirPath) {
1213
const entries = fs.readdirSync(dirPath, { withFileTypes: true });
@@ -23,6 +24,25 @@ function collectJsFiles(dirPath) {
2324
}
2425

2526
function 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-
/(from\s+['"])(\.{1,2}\/[^'"]+)(['"])/g,
56-
/(import\(\s*['"])(\.{1,2}\/[^'"]+)(['"]\s*\))/g
75+
/(^\s*(?:import|export)\s[^'"\n]*from\s+['"])([^'"]+)(['"])/gm,
76+
/(import\(\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 $
101121
if (copiedExtensionSecrets) {
102122
console.log('[agentos fix-esm-imports] Mirrored extension-secrets.json into dist/config for public package exports.');
103123
}
104-

0 commit comments

Comments
 (0)