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

[Feature]: support dynamic import by default #27

Closed
hardfist opened this issue Dec 28, 2021 · 2 comments · Fixed by #43
Closed

[Feature]: support dynamic import by default #27

hardfist opened this issue Dec 28, 2021 · 2 comments · Fixed by #43

Comments

@hardfist
Copy link

when I use tsm register to transpile module which contains dynamic import, it's broken

  • src/index.ts
async function main(){
  const mod = await import('./lib');
}
main();
  • src/lib.ts
export const answer = 42;

It's broken
image

It's because esbuild will keep dynamic import for high version of node, even you set format=cjs evanw/esbuild#1636
image

when I set target to node12, it works fine now, because it will transform dynamic import to require

  • tsm.js
module.exports = {
  common: {
   target: 'node12'
  }
}

so I think we could set target=node12 by default to support dynamic import by default

@lukeed
Copy link
Owner

lukeed commented Dec 29, 2021

What versions of Node and esbuild are you using? The support range for import() was fixed here: evanw/esbuild#1772

It's not wise to make node12 the target for all runtimes, because it's untrue for non-12.x runtimes.

@hardfist
Copy link
Author

I'm using [email protected]

lukeed added a commit that referenced this issue Nov 27, 2022
lukeed added a commit that referenced this issue Nov 28, 2022
* chore: add tests to reproduce CJS dynamic-import issue;

- Related #27

* feat: handle import() in CJS & backmap TS->JS resolving;

- attempt to load ".ts", ".jsx" or ".tsx" file when `import` to ".js" does not exist
- attempt to load ".mts" file when `import` to ".mjs" does not exist
- attempt to load ".cts" file when `import` to ".cjs" does not exist
- attempt to load ".tsx" file when `import` to ".jsx" does not exist

* chore: wrap ESM test file in async iife

* derp~!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants