Skip to content

Support loading of ESM files #13

Open
@novemberborn

Description

It's unclear to me how we would load compiled ESM files.

Our current approach is to install a hook on require.extensions but this is ignored for ESM files: https://github.com/avajs/babel/blob/c9de6fc63f9bc61abcdfbc99ebf734c1ad5a9210/index.js#L366:L369

Assuming that paths in the Babel output are relative, we could simply import() any given file and assume that everything else will follow. However this means code runs from a different directory leading to subtle failures.

Once we resolve this, we could add a default mjs extension, though this would be a breaking change.

Activity

fregante

fregante commented on Sep 8, 2020

@fregante
Contributor

Possibly related. I'm using Babel to support JSX files. I get:

  Uncaught exception in tests/index.jsx

  ./tests/index.jsx:13
  const _require = (0, _module.createRequire)(import.meta.url);
                                                     ^^^^

  SyntaxError: Cannot use 'import.meta' outside a module

Then I disable the ES Modules compilation with:

        "presets": [
          ["module:@ava/babel/stage-4", false]
        ]

and I get:

  Uncaught exception in tests/index.jsx

  ./tests/index.jsx:1
  import { createRequire } from 'module';
  ^^^^^^

  SyntaxError: Cannot use import statement outside a module

I think this is due to:

However this means code runs from a different directory leading to subtle failures

In my case, I have type:module in my package.json, so it should also be copied to the temporary directory.

fregante

fregante commented on Sep 8, 2020

@fregante
Contributor

Is AVA using @babel/register? Because I think this is not possible at the moment, according to the docs:

Note: @babel/register does not support compiling native Node.js ES modules on the fly, since currently there is no stable API for intercepting ES modules loading.

novemberborn

novemberborn commented on Sep 8, 2020

@novemberborn
MemberAuthor

Is AVA using @babel/register?

No, we install a hook to rewrite require() calls.

novemberborn

novemberborn commented on Sep 8, 2020

@novemberborn
MemberAuthor

I think this is due to:

However this means code runs from a different directory leading to subtle failures

In my case, I have type:module in my package.json, so it should also be copied to the temporary directory.

Yes but with our Babel support we still require() rather than import(). That's what this issue is about.

richardeschloss

richardeschloss commented on Mar 21, 2021

@richardeschloss

Hi I'm interested in the progress of this issue but thought I'd chime in. I'm actually able to get tests to successfully run on ava when the package type is "module". However, I'm struggling to gain coverage with nyc.

As a very simple example, suppose I have src file:
./src/hello.js:

export function hello() {
  return 'world';
}

And my test:
./test/hello.js:

import test from 'ava'
import { hello } from '../src/hello.js''

test('see me', (t) => {
  t.is(hello(), 'world')
})

package type is set to "module". node version 14.16.0.

The result: passing tests.

now, when I try to gather code coverage, running nyc ava on it's own fails to cover the files.

.nycrc:

{
  "all": false,
  "reporter": ["html", "text"],
  "exclude": ["node_modules"],
  "include": ["src/hello.js"],
  "extension": [".js"]
}

I tried using Istanbul's "esm-loader":

cross-env 'NODE_OPTIONS=--experimental-loader @istanbuljs/esm-loader-hook' nyc ava

Screenshot from 2021-03-20 20-49-26

I appreciate your help with this issue!

richardeschloss

richardeschloss commented on Mar 21, 2021

@richardeschloss

For what it's worth, I was able to get coverage with the existing .nycrc using c8.

c8 ava ## worked with the same .nycrc as before.

vjpr

vjpr commented on Sep 25, 2021

@vjpr

Just use --experimental-loader. Good docs here: https://nodejs.org/api/esm.html#esm_loaders

Is there a workaround at the moment?

removed this from To do in ESM supporton Jul 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Support loading of ESM files · Issue #13 · avajs/babel