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

__esm() initialisation never called with export * from statement. #1490

Closed
EHadoux opened this issue Aug 2, 2021 · 1 comment
Closed

__esm() initialisation never called with export * from statement. #1490

EHadoux opened this issue Aug 2, 2021 · 1 comment

Comments

@EHadoux
Copy link

EHadoux commented Aug 2, 2021

Hey, do you have any idea of why an import of an ESM lib can turn into a var init_somthg = __esm() import, with init_somthg() that is never called anywhere?

So, my smallest example for now:

import { PDF, getToc } from 'documents';

(async () => {
  const file = 'myfile.pdf';
  const pdf = new PDF(file);
  await getToc(pdf);
})();

PDF is a class and getToc is a function.

The problematic part is once bundled with esbuild --bundle --target=node14 --platform=node --loader:.ts=ts:

// ../documents/dist/fuzzy/index.js (It's an external lib but in an NPM workspace, hence the ../documents)
var init_fuzzy2 = __esm({
  "../documents/dist/fuzzy/index.js"() {
    init_toc();
  }
});

The documents lib in itself is build with tsc with target=ES2020 and module=ES2020, with type: module and sideEffects: false in package.json.

It's fuzzy2 because getToc uses a library that itself has fuzzy in its path. I've tried changing the directory name, it's reflected in the generated code (e.g. init_fuzzyy) but it still doesn't get called.
The thing is that init_fuzzy (the import of that library) is being called, but not init_fuzzy2, so I end up with a bunch of uninitialised things. getToc uses top-level variables that are defined in the file where it's defined, but that are not exported (which shouldn't make a difference because they get defined in the body of init_fuzzy2, but because it is not called, these variables are not initialised).
However, if I manually add init_fuzzy2(); somewhere in the bundled output, it works.
I'll post more info if you need, but I got to redact a lot of it. Cheers!

@EHadoux
Copy link
Author

EHadoux commented Aug 2, 2021

Interestingly, if I import everything like that instead:

import { PDF } from 'documents/dist/PDF/pdf';
import { getToc } from 'documents/dist/fuzzy/toc';

(async () => {
  const file = 'myfile.pdf';
  const pdf = new PDF(file);
  await getToc(pdf);
})();

There is no __esm() anymore, everything is just moved to the top-level (the variables as well) and it just works.

EDIT: I think I found it. Here is my hierarchy for the documents lib:

src
|- fuzzy 
|--- index.ts
|--- toc.ts
|- PDF
|--- index.ts
|--- pdf.ts
|- index.ts

Everything is exported explicitly in src/fuzzy/index.ts and src/PDF/index.ts (i.e. export { getToc } from './toc.ts') but I had a star export in src/index.ts (i.e. export * from './fuzzy').
By changing it to an explicit export (export { getToc } from './fuzzy'), the same code is generated, but init_fuzzy2 is called this time and everything works.

@EHadoux EHadoux changed the title __esm() initialisation never called __esm() initialisation never called with export * from statement. Aug 2, 2021
@EHadoux EHadoux closed this as completed Nov 30, 2023
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

1 participant