Skip to content

More external packages #790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions js/src/cli-util/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export async function bundleCommand(args: BundleArgs) {
mode: "bundle",
files: args.files,
tsconfig: args.tsconfig,
externalPackages: args.external_packages,
});

try {
Expand Down
1 change: 1 addition & 0 deletions js/src/cli-util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface AuthArgs {
export interface CompileArgs {
tsconfig?: string;
terminate_on_failure: boolean;
external_packages?: string[];
}

export interface RunArgs extends CommonArgs, AuthArgs, CompileArgs {
Expand Down
54 changes: 41 additions & 13 deletions js/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,18 +295,21 @@ async function initFile({
bundleFile,
tsconfig,
plugins,
externalPackages,
}: {
inFile: string;
outFile: string;
bundleFile: string;
tsconfig?: string;
plugins?: PluginMaker[];
externalPackages?: string[];
}): Promise<FileHandle> {
const buildOptions = buildOpts({
fileName: inFile,
outFile,
tsconfig,
plugins,
externalPackages,
});
const ctx = await esbuild.context(buildOptions);

Expand Down Expand Up @@ -343,6 +346,7 @@ async function initFile({
outFile: bundleFile,
tsconfig,
plugins,
externalPackages,
}),
external: [],
write: true,
Expand Down Expand Up @@ -709,18 +713,32 @@ async function collectFiles(
// In addition to marking node_modules external, explicitly mark
// our packages (braintrust and autoevals) external, in case they're
// installed in a relative path.
const markKnownPackagesExternalPlugin = {
name: "make-known-packages-external",
setup(build: esbuild.PluginBuild) {
// Mark known packages as external
const knownPackagesFilter =
/^(braintrust|autoevals|@braintrust\/|config|lightningcss)/;
build.onResolve({ filter: knownPackagesFilter }, (args) => ({
path: args.path,
external: true,
}));
},
};
function createMarkKnownPackagesExternalPlugin(
additionalPackages: string[] = [],
) {
return {
name: "make-known-packages-external",
setup(build: esbuild.PluginBuild) {
// Mark known packages as external
const knownPackages = [
"braintrust",
"autoevals",
"@braintrust/",
"config",
"lightningcss",
"@mapbox/node-pre-gyp",
...additionalPackages,
];
const knownPackagesFilter = new RegExp(
`^(${knownPackages.map((pkg) => pkg.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|")})`,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't test but perhaps this so that you don't catch all package names that happen to have these prefixes

Suggested change
"@braintrust/",
"config",
"lightningcss",
"@mapbox/node-pre-gyp",
...additionalPackages,
];
const knownPackagesFilter = new RegExp(
`^(${knownPackages.map((pkg) => pkg.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|")})`,
"@braintrust",
"config",
"lightningcss",
"@mapbox/node-pre-gyp",
...additionalPackages,
];
const knownPackagesFilter = new RegExp(
`^(${knownPackages.map((pkg) => pkg.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|")})(?:/|$)`,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking a look!

);
build.onResolve({ filter: knownPackagesFilter }, (args) => ({
path: args.path,
external: true,
}));
},
};
}

// Inspired by and modified from https://github.com/evanw/esbuild/issues/1051
const nativeNodeModulesPlugin = {
Expand Down Expand Up @@ -792,15 +810,17 @@ function buildOpts({
outFile,
tsconfig,
plugins: argPlugins,
externalPackages,
}: {
fileName: string;
outFile: string;
tsconfig?: string;
plugins?: PluginMaker[];
externalPackages?: string[];
}): esbuild.BuildOptions {
const plugins = [
nativeNodeModulesPlugin,
markKnownPackagesExternalPlugin,
createMarkKnownPackagesExternalPlugin(externalPackages),
...(argPlugins || []).map((fn) => fn(fileName)),
];
return {
Expand All @@ -823,11 +843,13 @@ export async function initializeHandles({
mode,
plugins,
tsconfig,
externalPackages,
}: {
files: string[];
mode: "eval" | "bundle";
plugins?: PluginMaker[];
tsconfig?: string;
externalPackages?: string[];
}): Promise<Record<string, FileHandle>> {
const files: Record<string, boolean> = {};
const inputPaths = inputFiles.length > 0 ? inputFiles : ["."];
Expand Down Expand Up @@ -870,6 +892,7 @@ export async function initializeHandles({
bundleFile,
plugins,
tsconfig,
externalPackages,
}),
);
}
Expand Down Expand Up @@ -930,6 +953,7 @@ async function run(args: RunArgs) {
mode: "eval",
tsconfig: args.tsconfig,
plugins,
externalPackages: args.external_packages,
});

if (args.dev) {
Expand Down Expand Up @@ -1000,6 +1024,10 @@ function addCompileArgs(parser: ArgumentParser) {
parser.add_argument("--tsconfig", {
help: "Specify a custom tsconfig.json file to use.",
});
parser.add_argument("--external-packages", {
nargs: "*",
help: "Additional packages to mark as external during bundling. These packages will not be included in the bundle and must be available at runtime. Use this to resolve bundling errors with native modules or problematic dependencies. Example: --external-packages sqlite3 fsevents @mapbox/node-pre-gyp",
});
}

async function main() {
Expand Down
Loading