Skip to content
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

Bare imports should always be executed first #2598

Open
arcanis opened this issue Oct 10, 2022 · 5 comments
Open

Bare imports should always be executed first #2598

arcanis opened this issue Oct 10, 2022 · 5 comments

Comments

@arcanis
Copy link

arcanis commented Oct 10, 2022

Consider the following code:

import './polyfill';
import {foo} from './user-lib';

foo();

Esbuild inlines ./polyfill, causing the file to turn into:

import {
  foo
} from "./chunk-EN3LV7DE.js";

// polyfill.js
Object.somePolyfilledFunction = () => {
};

// main.js
foo();

This is invalid, as it means that the polyfill won't be available inside chunk-EN3LV7DE, causing the output to crash.

Esbuild should at least assume that the bare import, having no imported symbols, must have a significance in terms of side-effects and ensure that it gets evaluated at the right time.

@hyrious
Copy link

hyrious commented Oct 10, 2022

#399

Note that this problem also exists in Rollup.

@arcanis
Copy link
Author

arcanis commented Oct 10, 2022

I checked #399 but since it mentioned multiple entry points I wasn't sure if this was a different issue or not 🤔

@hanayashiki
Copy link

seems you can't specify the order of execution of ecma modules per spec

@hanayashiki
Copy link

you can keep your polyfill while delay the run ofpiece of code that depends on it (e.g. lazy evaluation)

@arcanis
Copy link
Author

arcanis commented Jul 7, 2023

Yes, that's what I currently do, but it's not ideal: the browser has to finish executing polyfills before even starting to fetch the actual application. It adds a small delay and indirection; I'm a little worried a browser will eventually show a white flash on reload if the delay is long enough.

hyf0 added a commit to rolldown/rolldown that referenced this issue Jul 10, 2024
<!-- Thank you for contributing! -->

### Description

This feature try to solve the execution order problem caused by rollup
and esbuild's code splitting logic. The nature of these problem is that
the execution order of shared modules are hoisted unexpectedly.

- evanw/esbuild#399
- evanw/esbuild#2598

This is done by adding helper function to simulate the original the
semantic esm input while keep the static linking between modules.

<!-- Please insert your description here and provide especially info
about the "what" this PR is solving -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants