-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
externalize all but one package? #2811
Comments
That's just what the The |
I was able to make some progress by running javascript inside the plugin. {
name: 'externalize-everything-except-nanoid',
setup(build) {
build.onResolve({ filter: /(.*)/ }, (args) => {
if (args.kind === 'import-statement') {
return { path: args.path, external: /^(?!(nanoid)$).*$/.test(args.path) };
}
});
},
} but if I do that, it seems like it forgets how to resolve npm packages?
|
{
name: 'externalize-everything-except-nanoid',
setup(build) {
build.onResolve({ filter: /(.*)/ }, (args) => {
if (args.kind === 'import-statement' && args.path !== 'nanoid') {
return { path: args.path, external: true };
}
});
},
} If you return anything, then you're overriding esbuild. You need to not return something to let esbuild do its stuff. |
thanks, got this to work! function externalizeAllPackagesExcept(noExternals: string[]) {
return <esbuild.Plugin>{
name: 'noExternal-plugin',
setup(build) {
if (noExternals.length > 0) {
build.onResolve({ filter: /(.*)/ }, (args) => {
if (args.kind === 'import-statement' && !noExternals.includes(args.path.split('/')[0])) {
return { path: args.path, external: true };
}
});
}
},
};
} esbuild.build({
entryPoints: ['index.js'],
bundle: true,
platform: 'node',
plugins: [externalizeAllPackagesExcept(['nanoid', 'slash', /* ... */])],
}); |
I'm working with a monorepo and I want one package to build another package's files (because reasons). I used @mayank99 's code as a basis for something more specific. It basically externalized all external packages except for packages from // possible locations of `@myProject/` packages
const MY_PROJECT_BASE_PATHS = ["../../../node_modules", "./node_modules"];
function checkFile(filePath) {
try {
fs.accessSync(filePath, fs.constants.F_OK);
return filePath;
} catch (error) {
return undefined;
}
}
function getAbsoluteMyProjectPath(importPath) {
for (const basePath of MY_PROJECT_BASE_PATHS) {
const absolutePath = path.resolve(basePath, importPath + ".ts");
if (checkFile(absolutePath)) {
return absolutePath;
}
}
return undefined;
}
// We don't want esbuild to bundle anything in node_modules except for
// `@myProject/` packages. This plugin will externalize all node_modules packages
// except for `@myProject/` packages.
function externalizePackagesExceptMyProject() {
return {
name: "noExternal-plugin",
setup(build) {
build.onResolve({ filter: /(.*)/ }, (args) => {
console.log(args);
if (args.kind === "import-statement" && args.path[0] !== ".") {
if (args.path.startsWith("@myProject/")) {
const myProjectPath = getAbsolutemMyProjectPath(args.path);
if (myProjectPath) {
return {
path: myProjectPath,
external: false,
};
}
}
return { path: args.path, external: true };
}
});
},
};
} |
fwiw i ended up making some changes to my previous code and published it as a package. https://github.com/mayank99/esbuild-plugin-noexternal |
The
packages: 'external'
option is super handy to externalize all packages, but I would like to add one or two packages as exceptions to this.I tried using regex and complex globs in
external
but it doesn't work.Would be nice if there was something like Vite's
noExternal
property.The text was updated successfully, but these errors were encountered: