swc-project / swc-node

Faster ts-node without typecheck
MIT License
1.76k stars 73 forks source link

Experimental esm loader #643

Closed Brooooooklyn closed 1 year ago

Brooooooklyn commented 2 years ago
Multiply commented 2 years ago

We're eagerly waiting for top-level await support.

Can we do anything to help test this?

DenisGorbachev commented 2 years ago

Stuck on ESM support as well, looking forward to this PR merge!

sam-goodwin commented 2 years ago

Is something blocking this change from being merged?

osdiab commented 1 year ago

also waiting for this!

weyert commented 1 year ago

Any way how to progress this PR?

foray1010 commented 1 year ago

esm/esm.js is not included in v1.6.0, so @swc-node/register/esm does not work

> cat node_modules/@swc-node/register/package.json | rg version
  "version": "1.6.0",

> ls node_modules/@swc-node/register
LICENSE  README.md  index.js  lib  node_modules  package.json  read-default-tsconfig.d.ts  register.d.ts

Also, seems node -r @swc-node/register/esm is using require(), it failed to recognize /esm

Error: Package subpath './esm' is not defined by "exports" in node_modules/@swc-node/register/package.json
jensbodal commented 1 year ago

Can someone shed some light on how to use this now that it's fixed?

test.ts

import got from 'got';

const { data } = await got
  .post('https://httpbin.org/anything', {
    json: {
      hello: 'world',
    },
  })
  .json<{data: unknown}>();

console.log(data);
node -r @swc/register test.ts
node:internal/errors:490
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /foo/test.ts
erfanium commented 1 year ago

@jensbodal you should use loader for esm. read the docs

node --loader @swc-node/register/esm test.ts
jensbodal commented 1 year ago

Thank you that worked

And my bad I see it in the docs here: https://github.com/swc-project/swc-node#usage

weyert commented 1 year ago

Can we use both -r and --loader at the same time? Or does that not make sense?

erfanium commented 1 year ago

@weyert yes you can use. but i can't tell you something makes sense or not without seeing the actual code.