-
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
Allow plugin resolve callbacks to mark a module as side-effect free #1009
Comments
I'm temporally solving this by transforming the output again by ts loader (leaving const { transformSync } = require("esbuild");
const code = `
// proxy:ORIGINAL
import {serverFn} from "./server-side";
import {clientFn} from "./client-side";
function client() {
clientFn();
}
export {
client
};
`;
const result = transformSync(code, {
format: "esm",
loader: "ts",
});
console.log(result.code);
|
@buffaybu we have a nearly identical plugin. If you set package.json |
@evanw I can implement this, were you thinking of adding a field like |
Adding a SideEffectFree property to OnResolve that plugins can set to indicate that the resolved module is side effect free. Fixes evanw#1009 Co-authored-by: Adam Gaynor <adamsg9425@gmail.com>
With v0.12.9 and const esbuild = require("esbuild");
const originalCode = `
import { serverFn } from "./server-side";
import { clientFn } from "./client-side";
export function server() {
serverFn();
}
export function client() {
clientFn();
}
`;
async function main() {
const result = await esbuild.build({
stdin: {
contents: `export { client } from 'ORIGINAL'`,
loader: "js",
},
bundle: true,
format: "esm",
write: false,
plugins: [
{
name: "only-export-client",
setup: (build) => {
build.onResolve({ filter: /.*/ }, (args) => {
if (args.path === "ORIGINAL") {
return {
path: "ORIGINAL",
namespace: "proxy",
sideEffects: false,
};
}
return {
external: true,
sideEffects: false,
};
});
build.onLoad({ filter: /.*/, namespace: "proxy" }, async (args) => {
return {
contents: originalCode,
loader: "js",
};
});
},
},
],
});
console.log(`// v${esbuild.version}\n`);
console.log(result.outputFiles[0].text);
}
main();
Import of Update: After reading #1241 (comment) the above behavior now makes sense to me that |
I'm using esbuild to eliminate unneeded code by bundling a virtual entry that only re-exports the needed part and mark every imports as external.
An example that transforms
originalCode
to only keep client side code:The output is:
./server-side
is still imported and I guess the reason is that esbuild doesn't know whether the module is side-effect free or not.So if it's about side-effect, is it possible to allow plugin resolve callbacks to mark a module as side-effect free? Or is there any other solution to solve this problem?
The text was updated successfully, but these errors were encountered: