Skip to content

Commit

Permalink
fix: fix errors in package source files when users go to definition i…
Browse files Browse the repository at this point in the history
…n VSCode
  • Loading branch information
stainless-bot authored and rattrayalex committed Jul 10, 2023
1 parent 1d5f87d commit 7904179
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ node scripts/make-dist-package-json.cjs > dist/package.json
tsc-multi
# copy over handwritten .js/.mjs/.d.ts files
cp src/_shims/*.{d.ts,js,mjs} dist/_shims
tsc-alias -p tsconfig.build.json --resolve-full-paths
tsc-alias -p tsconfig.build.json
# we need to add exports = module.exports = Anthropic TypeScript to index.js;
# No way to get that from index.ts because it would cause compile errors
# when building .mjs
Expand Down
21 changes: 21 additions & 0 deletions scripts/replace-self-referencing-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });

const path = require('path');
const distSrcDir = path.resolve(__dirname, '..', 'dist', 'src');

function replaceSelfReferencingImports({ orig, file, config }) {
// replace self-referencing imports in source files to reduce errors users will
// see if they go to definition
if (!file.startsWith(distSrcDir)) return orig;
return orig.replace(/['"]([^"'\r\n]+)['"]/, (match, importPath) => {
if (!importPath.startsWith('@anthropic-ai/sdk/')) return match;
let relativePath = path.relative(
path.dirname(file),
path.join(distSrcDir, importPath.substring('@anthropic-ai/sdk/'.length)),
);
if (!relativePath.startsWith('.')) relativePath = `./${relativePath}`;
return JSON.stringify(relativePath);
});
}
exports.default = replaceSelfReferencingImports;
12 changes: 12 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@
"outDir": "dist",
"pretty": true,
"sourceMap": true
},
"tsc-alias": {
"resolveFullPaths": true,
"fileExtensions": {
"inputGlob": "{mjs,cjs,js,jsx,mts,cts,ts,tsx}"
},
"replacers": {
"replace-absolute-imports": {
"enabled": true,
"file": "./scripts/replace-self-referencing-imports.js"
}
}
}
}

0 comments on commit 7904179

Please sign in to comment.