swc-project / swc-node

Faster ts-node without typecheck
MIT License
1.68k stars 67 forks source link

need help building example, esmregister + esmock #788

Closed iambumblehead closed 1 day ago

iambumblehead commented 2 weeks ago

When trying to create an example test with swc and esmock, this error occurs around the types definition file,

node --import @swc-node/register/esm-register --test esmock.node-swc.test.ts

> test:current
> node --import @swc-node/register/esm-register --test esmock.node-swc.test.ts

file:///home/bumble/soft/esmock/src/esmock.d.ts:1
type MockMap = { [specifier: string]: any }
     ^^^^^^^

SyntaxError: Unexpected identifier 'MockMap'
    at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:169:18)
    at callTranslator (node:internal/modules/esm/loader:272:14)
    at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:278:30)

Thanks in advance for any advice

Brooooooklyn commented 1 day ago

Fixed in https://github.com/swc-project/swc-node/pull/798

iambumblehead commented 6 hours ago

The specific error described at the top of this issue is resolved, however, after updating @swc-node/register a new issue is encountered,

✖ should mock js when using swc (34.125566ms)
  file:///home/bumble/soft/esmock/tests/local/mainUtil.js:2
  import babelGeneratedDoubleDefault from 'babelGeneratedDoubleDefault';
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  SyntaxError: The requested module 'babelGeneratedDoubleDefault' does not provide an export named 'default'

The error needs context to be understood but, essentially, @swc-node/register encounters an error loading mixed esm/cjs modules.

Slightly difficult to describe the sequence resulting in the error, here is what happens,

  1. 'esmock.node-swc.test.ts' imports '../local/main.js' (esm) link
  2. '../local/main.js' imports './mainUtil.js' (esm) link
  3. 'mainUtil.js' imports 'babelGeneratedDoubleDefault' (cjs) error link
  4. '../package.json' links 'babelGeneratedDoubleDefault' to the local directory package 'babelGeneratedDoubleDefault', link package.json
  5. 'babelGeneratedDoubleDefault' describes a cjs module, link babelGeneratedDoubleDefault/package.json, link babelGeneratedDoubleDefault.js

The other loaders used in the test suite process the same import tree without error https://github.com/iambumblehead/esmock/tree/attempt-enable-swc-tests/tests

Thank you for reading me