-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 treeShaking: false
option
#604
Comments
I've beed down this rabbit hole before.. Setting the import Counter from './Counter.svelte'; Would turn in to: import './Counter.svelte'; |
For the time being, I "fixed" this by saving & preprending the original Ideally, this would still be done at esbuild's level since it actually deals with the contents... more than a RegExp, at least. Producing an accurate sourcemap is among the reasons |
This is what the official TypeScript compiler does too, right? How do Svelte and Vue use the official TypeScript compiler? |
TypeScript doesn't treeshake by default. When transforming a file, it will strip types and leave the transpiled JS of whatever was given. |
Here's what I used for testing: // example.ts
import Counter from './Counter.svelte';
export let name = 'world'; Here's what you get with export var name = 'world'; Here's what you get with import './Counter.svelte';
export var name = 'world'; |
Hmm.. sorry, not sure where I got that idea from then. Would this still be something you'd consider adding? The alternatives are to trust a RegExp, or to use Also, on a related note, esbuild seems to reserve files' basenames as internal variables, which then increments a variable identifier with the same name: // input
import { foo } from './src/data';
export let value = foo;
let data = value * 3;
// esbuild output
import {foo} from "./src/data";
export let value = foo;
let data2 = value * 3; //<~ data2
// tsc output (tsc --module esnext)
import { foo } from './src/data';
export var value = foo;
var data = value * 3; Also true with |
@evanw |
Right, I checked before my last comment to make sure I wasn't missing an alternative 😆 It's an AST visitor |
I'm not planning on exposing an API for AST manipulation in esbuild itself, so the same approach won't work with esbuild. And I'm not keen on adding another option for this since esbuild wasn't designed for partial module compilation so it's technically not a supported use case. I just made a change that should hopefully make this possible in the next release. The change is described in detail in the release notes. Basically just set I also changed the automatically-generated import namespace variables to be prefixed with |
This is great, thank you! |
Heads up that I'm planning on changing this behavior to align with the behavior of the official TypeScript compiler. They are now releasing a flag for this called |
At least in
transform
andtransformSync
...Ideally, with
transform*
, files are processed one at a time for raw translations. While most cases will benefit from treeshaking, it doesn't fit every case. For example, Svelte and Vue files will import components within a<script>
block, and then the framework itself is making the connection between<script>
and<tempate>
scopes.Here's an extremely simple Svelte example:
When the
<script>
tag is processed on its own,esbuild.transform
produces this output:Consequently, this means that the rest of the Svelte component is working with an undefined
Counter
reference.Because
transform*
is not following and/or bundling files together, IMO it's totally acceptable to leave theimport Counter from './Counter.svelte';
as is – entrusting the dev knows what they're doing and/or entrusting something else to take care of that later on.,In this particular case, Svelte and/or Rollup would pick up the
Counter.svelte
path in a later pass.The text was updated successfully, but these errors were encountered: