-
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
[Feature request] Add flag for optionally disabling tree-shaking #458
Comments
Related issue: #371 Although it'd be preferable to have no optimizations or minification take place in the absence of the |
this is actually required for some modules that define number of functions, but do not reference them at all and instead register them for usage during runtime. example is at the moment esbuild drops all those functions from bundle. (this also means it's not a feature request, it's becomes a bug). |
I'd like to get to the root of the problem and understand why this is happening instead of just adding a flag to ignore the issue. I have so far found these issues that come up when using esbuild with that package:
|
I'm already having a conversation with package maintainers trying to change it, there are several issue with that package definition (for example, nodejs specific requires in a browser package) - so far, the reply has been "since it works with webpack, it must be issue with your bundler" |
Yeah I'm still not totally sure if it's an issue with esbuild or not. The |
it's also very inconsistent, for example: first import is import * as tf from '@tensorflow/tfjs/dist/tf.es2017.js'; // works
import * as tf from '@tensorflow/tfjs/dist/index.js'; // doesn't work and then load wasm module after that, but the opposite works: import { setWasmPaths } from '@tensorflow/tfjs-backend-wasm/dist/index.js'; // works
import { setWasmPaths } from '@tensorflow/tfjs-backend-wasm/dist/tf-backend-wasm.es2017.js'; // doesn't work (this inconsistency is nothing to do with esbuild, it's the same when i use it in script tag with type module) |
Wait I'm confused. It doesn't even work with Webpack. Here's the example from their readme: // Import @tensorflow/tfjs or @tensorflow/tfjs-core
import * as tf from '@tensorflow/tfjs';
// Adds the WASM backend to the global backend registry.
import '@tensorflow/tfjs-backend-wasm';
// Set the backend to WASM and wait for the module to be ready.
tf.setBackend('wasm').then(() => main()); Bundling that with |
the webpack config tfjs quotes is documented here: https://github.com/tensorflow/tfjs/tree/master/tfjs-backend-wasm/starter/webpack but regarding code, at the very minimum you need to check if best is to call but in general, see my first note: |
i've created a simple test and seems like latest version you just published fixes tree shaking issues, all variations now work - great stuff! what remains is the minify problem #538 https://gist.github.com/vladmandic/3097052d32a902239e9b855af8fd8425 |
actually, there is one more border case where tree shaking is overly aggressive. this works: import * as tf from '@tensorflow/tfjs/dist/index.js';
import { setWasmPaths } from '@tensorflow/tfjs-backend-wasm/dist/index.js'; but if i create a export * as tf from '@tensorflow/tfjs/dist/index.js';
export { setWasmPaths } from '@tensorflow/tfjs-backend-wasm/dist/index.js'; and then use it in rest of the code as: import { tf, setWasmPaths } from './tf.js' it appears to load, but it will fail on first tensor operation as 'not registered' but then when i do this, it works again export * as tf from '@tensorflow/tfjs/dist/index.js';
export * as wasm from '@tensorflow/tfjs-backend-wasm/dist/index.js'; basically, indirectly exporting single function makes esbuild think all other functions are not used and drops them. |
I can't reproduce those results. Here's what I tried: // index.js
import * as tf from '@tensorflow/tfjs/dist/index.js';
import { setWasmPaths } from '@tensorflow/tfjs-backend-wasm/dist/index.js'; // index2.js
import { tf, setWasmPaths } from './tf.js' // tf.js
export * as tf from '@tensorflow/tfjs/dist/index.js';
export { setWasmPaths } from '@tensorflow/tfjs-backend-wasm/dist/index.js'; Versions:
Commands:
Both files out.js and out2.js are byte-wise identical and have 2,353,824 bytes. It could be different if there is more in the file that actually uses |
there's something weird, i also can't reproduce with a test app, but clearly see bundle size difference on my real app. in any case, i like the behavior you describe with btw, thanks for amazing support! |
A flag to disable respecting tree shaking annotations has now been released: |
Feature request
A command line flag to opt-out of tree-shaking (or opt-in to tree-shaking, that defaults
true
). An alternative could also to only enable tree-shaking if the--minify
option is also present.Motivation
Attempting to use esbuild as a replacement for concat_js / Webpack when using Karma, the test runner for Angular.
When Angular compiles templates to TS, it marks the generated metadata functions as pure as these aren't needed in production builds, however in some situations they are needed during tests.
Using esbuild to generate a testing bundle will currently remove the function calls that are marked as pure (rightly so), but for this use case they are needed.
The text was updated successfully, but these errors were encountered: