-
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
Useless repeated imports when using external modules #475
Comments
Thanks for reporting this. I have wanted to fix this for a while (and also the similar case for |
Related: #382 |
I noticed I have an absolutely huge (near 50mb) The project is here, I have to say that in most ways I find this library a joy to work with - very efficient and generally I find that the configuration options make sense etc. Not sure if I just missed something here. |
Is there a workaround for this? I really don't want to use another bundler. |
@AlvinThorn008 I partially work around this with a plugin specifically geared at adding a shim around my imports to It's made a massive difference to my proejct(s), though.
The interface for writing plugins is liable to change, but it is still considerably less painful than using WebPack. Or rollup. Or any other bundler I've tried thus far. Sorry to the kind and diligent souls who maintain said projects.
I'd previously written that for another project, but you can see it in context in my current version of |
@xinaesthete |
Has there been any update on this? |
Is this in the roadmap somehow? we would like to adopt |
Same issue here. Causes huge problems for projects with sensitive dependencies... wasm, three.js, react etc. Have to go back to other bundlers for now ☹ |
My way of avoiding the "multiple instances of react" is to use React CDN, then use |
Using a CDN & |
Trying to inject
Yep, kinda stuck on how to proceed here. We're largely using |
I've got an app that depends on the solid library. Building with esbuild produces a bundle with code sections like this: // src/app/Main.jsx
init_define_config();
init_web();
init_web();
init_web();
init_solid();
init_dist();
// src/app/parts/Header.jsx
init_define_config();
init_web();
init_web();
init_web();
init_web();
init_web();
init_web();
init_web();
init_web();
// src/app/parts/InfoBox.jsx
init_define_config();
init_web();
init_web();
init_web();
// src/app/parts/ProfileMenu.jsx
init_define_config();
init_web();
init_web();
init_web();
init_web();
init_web();
init_web();
init_web();
init_web();
init_web(); I'm having some difficulty understanding exactly what is going on. Perhaps the JSX transform is producing duplicate imports and esbuild then doesn't consolidate them? |
Have any update about this issues? I have same problem(duplicate import) too😅 |
Indeed, I think this is preventing us from packaging a react component library with esbuild and then being liberal in who can import, e.g., a webpack consumer is giving warnings of Am working on a minimal test case, but basically: Library (esbuild): "module": "dist/index.pure.esm.js",
"scripts": {
"build:esm:js": "esbuild --bundle src/index.js --sourcemap --target=es6 --loader:.js=jsx --format=esm --external:react --external:react-dom --outfile=dist/index.pure.esm.js",
},
"peerDependencies": {
"react": ">=16.8.6",
"react-dom": ">=16.8.6"
},
"devDependencies": {
"react": ">=17.0.2",
"react-dom": ">=17.0.2", Consumer (CRA): "dependencies": {
"@mylib": "file:/mylib",
"react": "^17.0.2",
"react-dom": "^17.0.2", We want to support multiple consumers -- it's an OSS lib -- with at least cjs + esm as officially support. But right now can't get any combo of esbuild component => wp4 CRA consumer. Right now we are trying random Edit: Why I think it may be this -- |
Ah maybe false alarm in our case: facebook/react#13991 (comment) |
I would love an update on this too, I do have several libraries duplicated :( |
Any updates on this? 👀 I have the same issue when building a react component library:
|
Two import statements for the same module does not cause any observable behavior changes (unless your JS runtime is seriously broken). So this is an issue for aesthetics and code size, but it should not impact correctness. If you do have an example case where it impacts correctness, please post it here. |
@evanw we were able to pinpoint the issue now, seems like a react shim clashes with some imports. you're right, it doesn't affect correctness, sorry about all the confusion! |
Any progress ? |
I found in remix app that the generated
|
This was fixed for me in |
I observed this problem because I upgraded Remix from 1.6.x to 1.7 the day before yesterday |
I am also interested in resolving this issue because it makes my resulting javascript file to weight ~100MB. Update: I was able to resolve the issue, I've put details here rails/jsbundling-rails#143 |
This issue is talking about what happens when you bundle multiple files with Are you saying that a significant portion of your 100mb bundle is only external import statements? Even 50mb would be a crazy number of import statements. For example, the text @allomov It sounds like you're somehow getting a 100mb bundle that consists almost completely of duplicated external import statements (since that's what this issue you commented on is about). This is very surprising. Can you describe more how this is happening in your code base? It would be helpful if you could provide a code sample that reproduces this issue. |
@evanw thank you for the answer. My problem was only partly related to
To give you more context, we've migrated a Ruby on Rails application with tons of legacy code, and we could easily switch from webpack to esbuild. I used this article to get an idea of how to do it in a new way https://gorails.com/episodes/esbuild-jsbundling-rails. To get the production build, |
I'm having this issue importing and bundling When running the app locally, you get the following error in the browser console: Uncaught DOMException: Failed to execute 'define' on 'CustomElementRegistry': the name "vaadin-lumo-styles" has already been used with this registry
at http://127.0.0.1:8080/lib/index.js:11413:18
at http://127.0.0.1:8080/lib/index.js:83160:3 The reason for the above error is web-components should only be registered once and the generated bundle imports the following imports multiple times: // node_modules/@vaadin/vaadin-lumo-styles/version.js
var Lumo = class extends HTMLElement {
static get version() {
return "24.0.1";
}
};
customElements.define("vaadin-lumo-styles", Lumo);
...
// node_modules/@vaadin/vaadin-core/node_modules/@vaadin/avatar/node_modules/@vaadin/vaadin-lumo-styles/version.js
var Lumo2 = class extends HTMLElement {
static get version() {
return "24.0.1";
}
};
customElements.define("vaadin-lumo-styles", Lumo2); |
@nsainaney I believe that's irrelevant to this issue. This issue is about external modules (i.e. those that aren't in the bundle). In addition, it looks like esbuild is working correctly in your case. If there are two separate files on the file system and you import both of them, then esbuild will include both files in the bundle. To have that not happen you'd have to either only have one file on the file system (e.g. reconfigure your package manager to not install the same package multiple times) or rewrite the two import paths to point to a single file (e.g. with an esbuild plugin). It would be invalid for esbuild to do this automatically because JavaScript requires each separate module identifier to become a separate module. |
I see - thank you for the prompt response. |
is there any update? |
@rashidul-xs I was able to optimize the file size by inspecting the dependencies and removing duplications. I guess that could be only my case. |
I see this a lot even with internal node packages, here
|
I created a plugin to address this issue. You can find it here: esbuild-plugin-rdi. This ESBuild plugin removes duplicate |
The tests are currently failing as my GitHub actions quota was drained due to a script stuck at postinstall for several hours. However, you can manually verify that test are passing and the plugin works as expected. |
I created a plugin to address this issue. You can find it here: esbuild-plugin-rdi. This ESBuild plugin removes duplicate |
Setup
Actual behavior
And
NB:
x
is imported twice, once asx
and once asx2
Expected behavior
And
NB:
x
is imported only onceThe text was updated successfully, but these errors were encountered: