-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Tree shaking library builds doesn't work #8676
Comments
I guess the question is why an exports namespace object is included here at all in the first place... |
Maybe you can work around that by using a slightly different @PURE comment that uses an IIFE? export const library_function = /* @__PURE__*/ (() =>
catch_errors(() => {
console.log("I just did very complex stuff. Imagine there are very many functions like me!")
})
)() It's a bit ugly, but it worked in my case (except that the imported library does not have an ESM version, so I had to alias it away, additionally). That syntax has the additional benefit of flagging an entire code block as pure, avoiding having to write stuff like this: import {Type} from "@sinclair/typebox"
export const SomeModel = /* @__PURE__*/ Type.Object({
fieldA: /* @__PURE__*/ Type.String(),
fieldB: /* @__PURE__*/ Type.String(),
fieldC: /* @__PURE__*/ Type.Number(),
}) |
That works! But I spent like one hour on changing IMO the damage of breaking tree shaking is bigger than the damage caused by having the wrong |
The pure So something like the highlighted line here: Becomes something like |
I'm currently writing a babel transformer with chatGPT to fix the pure comment part of this because I had to write another one to rewrite some imports because most of our customers struggle with transpiling javascript. I just wanted to ask: what you're saying with
is that all calls to |
Nevermind, I answered my question myself: it doesn't always suffice, sometimes there are also edit: nevermind on the nevermind, I think there are always also export statements - my files had the wrong contents (commonjs and esmodule mixed up) |
I also have the same issue. export * from './big'; // contains 1 big constant
export * from './helpers'; // contains 2 functions In my library package.json I have: {
"type": "module",
"source": "src/index.ts",
"main": "dist/parcel-lib.cjs",
"module": "dist/parcel-lib.mjs",
"types": "dist/index.d.ts",
} If I use my library in my app that does not make use of the constant (originally located in the |
Also getting same/similar issue. Named export functions that we don't import keep included in the build. |
Oh, in my case, this is because I build 2 files in one command like this: {
"build-site-js": "parcel build assets/js/site.js assets/js/site-jquery.js --dist-dir js/min --no-source-maps"
} So the But splitting that command into below, fix my issue: {
"build-site-jquery": "parcel build assets/js/site-jquery.js --dist-dir js/min --no-source-maps",
"build-site-js": "parcel build assets/js/site.js --dist-dir js/min --no-source-maps"
} By splitting it, functions that I don't import in |
Don't think this has been fixed unfortunately |
🐛 bug report
When building a library using parcel and then building something that consumes that library, using parcel, tree shaking is not working how it should
🎛 Configuration (.babelrc, package.json, cli command)
A yarn workspace with an inner library package that's built as library and an outer package that's built for the browser, see https://github.com/danieltroger/parcel-tree-shaking-bug
Run
yarn rebuild-everything
to rebuild the library and code consuming it.🤔 Expected Behavior
Given that we're just doing
and our library is pure - it only contains functions, except for this function call which has a pure comment - the browser build (this file) should be an empty file.
😯 Current Behavior
The browser build (this file) contains the whole library code.
💁 Possible Solution
Two things need to be done to achieve the desired 0 byte browser output in the example repo:
$parcel$export
needs to be prefixed with a pure comment/*@__PURE__*/ (0, $b208948437c85312$export$f5e092cdfed98805)
needs to be preserved as part of the function name by parcel when it does the(0, function_name)
thing, which I understand it needs to do so the function has the correct value forthis
. So it should be(0, /*@__PURE__*/ $b208948437c85312$export$f5e092cdfed98805)
instead.For further clarity, see this diff, which is how I would propose parcel to modify it's output for the library build: danieltroger/parcel-tree-shaking-bug@fbca029
🔦 Context
We're starting to use Yarn's workspace features. We're splitting up big parts of our repo into different packages since we want to publish packages on npm and they need to be able to import our, right now private, code which they depend on from a public package. After days of agonising updating of import statements and type error fixing we have given up on splitting up our codebase into multiple packages. This is because we got a lot of circular dependencies by the imports, which broke
yarn serve
(it would be awesome if parcel would warn for this and show where they are!). Instead we will move everything into one huge package.Here it is paramount though that tree shaking works. Right now, even importing nothing from the utility package we have created adds 125.53KB to whatever is using it. I have debugged it and it seems like it boils down to the two issues mentioned here. I would very much appreciate if this could be fixed in the near future. If my proposed solution works I imagine I could even do it myself given the locations in the parcel code where the pure comments should be added.
💻 Code Sample
https://github.com/danieltroger/parcel-tree-shaking-bug
🌍 Your Environment
The text was updated successfully, but these errors were encountered: