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

Can't use dynamic imports with swc #1508

Closed
petercoulton opened this issue Oct 11, 2021 · 5 comments
Closed

Can't use dynamic imports with swc #1508

petercoulton opened this issue Oct 11, 2021 · 5 comments
Milestone

Comments

@petercoulton
Copy link
Contributor

Search Terms

  • swc dynamic imports
  • swc 'regenerator-runtime'

Expected Behavior

Dynamic imports should continue to work after switching to the swc transpiler.

Actual Behavior

Throws Error: Cannot find module 'regenerator-runtime

Steps to reproduce the problem

const example = async () => {
    const other = await import('./other')
    console.log(other)
}

example().then(() => console.log(`done`))

See TypeStrong/ts-node-repros#20 for full example.

Minimal reproduction

TypeStrong/ts-node-repros#20

Specifications

  • ts-node version: v10.2.1
  • node version: v16.8.0
  • TypeScript version: 4.4.3
  • tsconfig.json, if you're using one:
{
  "extends": "ts-node/node16/tsconfig.json",

  "ts-node": {
    "transpileOnly": true,
    "transpiler": "ts-node/transpilers/swc-experimental"
  },

  "include": ["**/*.ts"],
  "exclude": ["node_modules"]
}

  • Operating system and version: macOS 11.6 Big Sur
  • If Windows, are you using WSL or WSL2?: no
@cspotcode
Copy link
Collaborator

Does swc's transpiled output have a runtime dependency on regenerator-runtime?

@petercoulton
Copy link
Contributor Author

@cspotcode My application doesn't use regenerator-runtime directly, and the code runs correctly with ts-node without enabling swc, but it seems that swc expects regenerator-runtime to be available.

@cspotcode
Copy link
Collaborator

That would fall into the same category as tslib and @swc/helpers. Sometimes changing compiler options necessitates that additional dependencies be available.

Notably, even if we declared them as peerDependencies, they would still need to be declared in your package.json since they are being imported/required by your code as opposed to by ts-node. I believe certain flavors of yarn enforce this.

We don't want to declare them as peerDependencies because then they get eagerly installed by npm even in projects that don't need them.

We will accept a PR adding this to our docs, probably belongs on this page where we mention @swc/helpers

https://typestrong.org/ts-node/docs/transpilers#bundled-swc-integration

petercoulton added a commit to petercoulton/ts-node that referenced this issue Oct 11, 2021
@petercoulton
Copy link
Contributor Author

petercoulton commented Oct 11, 2021

Okay, that makes sense. Adding regenerator-runtime as a dev dependency seems to work. I've created a PR #1509 that updates the transpilers docs page with the additional dependency.

@cspotcode
Copy link
Collaborator

Thanks. It also occurred to me, we can detect when an import fails and log a helpful warning. For example, when a file compiled by ts-node attempts to require('regenerator-runtime') and fails, we can detect this and log a helpful warning suggesting that the user should install regenerator-runtime. I've added a note about this to #1504

cspotcode added a commit that referenced this issue Oct 17, 2021
* docs: regenerator-runtime dependency for swc (#1508)

* docs(transpilers): update advice for installing regenerator-runtime

* Update transpilers.md

Co-authored-by: Andrew Bradley <[email protected]>
@cspotcode cspotcode added this to the 10.3.0 milestone Oct 22, 2021
@cspotcode cspotcode modified the milestones: 10.3.0, 10.3.1 Oct 22, 2021
cspotcode added a commit that referenced this issue Oct 22, 2021
* Fix inconsistent reference to ts-node in docs

* docs: regenerator-runtime dependency for swc (#1508) (#1509)

* docs: regenerator-runtime dependency for swc (#1508)

* docs(transpilers): update advice for installing regenerator-runtime

* Update transpilers.md

Co-authored-by: Andrew Bradley <[email protected]>

Co-authored-by: Peter Coulton <[email protected]>
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

2 participants