Skip to content

Commit 7904179

Browse files
stainless-botrattrayalex
authored andcommitted
fix: fix errors in package source files when users go to definition in VSCode
1 parent 1d5f87d commit 7904179

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ node scripts/make-dist-package-json.cjs > dist/package.json
2626
tsc-multi
2727
# copy over handwritten .js/.mjs/.d.ts files
2828
cp src/_shims/*.{d.ts,js,mjs} dist/_shims
29-
tsc-alias -p tsconfig.build.json --resolve-full-paths
29+
tsc-alias -p tsconfig.build.json
3030
# we need to add exports = module.exports = Anthropic TypeScript to index.js;
3131
# No way to get that from index.ts because it would cause compile errors
3232
# when building .mjs
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
Object.defineProperty(exports, '__esModule', { value: true });
3+
4+
const path = require('path');
5+
const distSrcDir = path.resolve(__dirname, '..', 'dist', 'src');
6+
7+
function replaceSelfReferencingImports({ orig, file, config }) {
8+
// replace self-referencing imports in source files to reduce errors users will
9+
// see if they go to definition
10+
if (!file.startsWith(distSrcDir)) return orig;
11+
return orig.replace(/['"]([^"'\r\n]+)['"]/, (match, importPath) => {
12+
if (!importPath.startsWith('@anthropic-ai/sdk/')) return match;
13+
let relativePath = path.relative(
14+
path.dirname(file),
15+
path.join(distSrcDir, importPath.substring('@anthropic-ai/sdk/'.length)),
16+
);
17+
if (!relativePath.startsWith('.')) relativePath = `./${relativePath}`;
18+
return JSON.stringify(relativePath);
19+
});
20+
}
21+
exports.default = replaceSelfReferencingImports;

tsconfig.build.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,17 @@
1616
"outDir": "dist",
1717
"pretty": true,
1818
"sourceMap": true
19+
},
20+
"tsc-alias": {
21+
"resolveFullPaths": true,
22+
"fileExtensions": {
23+
"inputGlob": "{mjs,cjs,js,jsx,mts,cts,ts,tsx}"
24+
},
25+
"replacers": {
26+
"replace-absolute-imports": {
27+
"enabled": true,
28+
"file": "./scripts/replace-self-referencing-imports.js"
29+
}
30+
}
1931
}
2032
}

0 commit comments

Comments
 (0)