-
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
[QUESTION] Why is this code not tree shaked? #756
Comments
It is retained because it contains code that can potentially have side effects. The two expressions retaining LitElement["finalized"] = true;
LitElement.render = render2; For example, the superclass called If you would like for |
a opt-in "loose" mode would be quite useful. while many things are possible, the overwhelming majority are unusual/improbable. |
I know it's super invasive doing it like this, but what's your opinion about the improvement? The scripts now look like this: "scripts": {
"mark-side-effects": "node ./mark-side-effects.js",
"prebuild": "npm run mark-side-effects",
"build": "esbuild ./src/my-component.js --bundle --outdir=dist",
"prestart": "npm run build && cp src/*.html dist",
"start": "npx servor dist"
},
...
"sideEffectsPackages": {
"lit-element": false
},
... And I've written const fs = require("fs");
const sideEffectsPackages = require("./package.json").sideEffectsPackages;
if (sideEffectsPackages) {
Object.keys(sideEffectsPackages).forEach(packageName => {
const packagePath = `./node_modules/${packageName}/package.json`;
const packageJSON = JSON.parse(fs.readFileSync(packagePath));
packageJSON.sideEffects = sideEffectsPackages[packageName];
fs.writeFileSync(packagePath, JSON.stringify(packageJSON));
})
} Now the bundle works as expected by your explanation @evanw , thank you for taking the time to clarify it for me. |
@evanw Of course, instead of actually overwriting packages' package.json files... maybe there's a way to do it with an esbuild plugin (that I can work on myself)? |
OK, I'm reading the code for https://github.com/evanw/esbuild/blob/master/internal/resolver/resolver.go and the plugins documentation and there's definitely no way to do it with a plugin. As described:
Would you consider adding an advanced option to mark sideEffects values from within esbuild? require('esbuild').build({
entryPoints: ['app.js'],
bundle: true,
outfile: 'out.js',
sideEffects: {
'lit-element': false
},
}).catch(() => process.exit(1))
|
Closing because the question has been answered (by the author!) and a rough solution has been given by myself in the conversation. A feature to solve the issue (which is completely outside the responsibility of the builder/bundler but a nice-to-have nonetheless) has already been proposed as well. Thanks @evanw ! |
I've got a simple Web Component defined like this:
And
esbuild
is building with this script:The class
LitElement
is not being used at all, however the output includes the whole ofLitElement
's class definition: https://esbuild-tree-shaking-example.glitch.me/my-component.jsYou've got the example at Glitch here:
https://glitch.com/edit/#!/esbuild-tree-shaking-example
What am I missing? 😅
The text was updated successfully, but these errors were encountered: