[Feature] Allow skipping certain named exports
Hello, it would be amazing if we could pass in a list of names for exports that esbuild would skip entirely when factoring dependencies and tree shaking. This could only work for expressions within the export statement if it made it faster to release. The feature could be added as an option when transpiling files (such as JSX). A use case for this would be in SSR/SSG frameworks where they may have functions that would only run on the server and not the client:
import PersonData from "./data.json" assert { type: "json" };
interface Person {
name: string;
age: number;
}
// This function and dependencies that are only used by it are going to be pruned when tree-shaking.
export function getStaticProps(): Person {
return PersonData;
}
export default function PersonPage(props: Person) {
return <h1>My name is {props.name} and I am {props.age} year{props.age ? "s" : ""} old.</h1>
}
The program would be compiled with the following option:
{
"ignoredExports": ["getStaticProps"]
}
Thank you and I would be willing to take a stab on this if you find it useful as I need it for a framework I am working on for Deno.
Can't you write a plugin for this yourself, without any changes to esbuild? Just have the plugin generate a virtual file that only re-exports some exports.
Can't you write a plugin for this yourself, without any changes to esbuild? Just have the plugin generate a virtual file that only re-exports some exports.
Wouldn't that require parsing the input file twice, though?
Yes, once with your plugin and once with esbuild. That's how you do AST manipulation in esbuild though, since esbuild doesn't have built-in direct AST manipulation. This library would probably be a good way to do that: https://github.com/guybedford/es-module-lexer
Closing as I'm not planning on implementing this within esbuild itself, as this is not how JavaScript works. Importing a file in JavaScript imports all of the code, not just the referenced code. If you only want to import some code and not other code then you should split that code up into multiple files (or use bundling and ensure that the code can be detected as side-effect free).