Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

AVA requiring @std/esm does not work #197

Closed
billyjanitsch opened this issue Dec 21, 2017 · 5 comments
Closed

AVA requiring @std/esm does not work #197

billyjanitsch opened this issue Dec 21, 2017 · 5 comments

Comments

@billyjanitsch
Copy link

Here is a minimal test repo using AVA and @std/esm. To reproduce the issue, run npm install then npm test.

lib/ contains an ESM build. index.js activates @std/esm and re-exports the default export of lib/. In package.json, module points to lib/, and main points to index.js with "@std/esm": "cjs" for CJS compat. As I understand it, this is a standard setup.

AVA is configured to require @std/esm, and to inherit the Babel config in .babelrc to transpile its test files (which would be necessary, for instance, if the test files used JSX). This config does not transpile ESM -> CJS. However, npm test fails:

/Users/billy/work/ava-esm/test.js:1
(function (exports, require, module, __filename, __dirname) { import test from 'ava';
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:599:28)
    at extensions.(anonymous function) (/Users/billy/work/ava-esm/node_modules/require-precompiled/index.js:13:11)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/billy/work/ava-esm/node_modules/ava/lib/process-adapter.js:100:4)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)

This suggests that @std/esm is not working correctly on the test file.

If you remove the line which tells AVA to use .babelrc, npm test passes because AVA falls back to its default Babel config which transpiles ESM -> CJS.

@billyjanitsch billyjanitsch changed the title AVA requiring @std/esm does not work when delegating to a Babel config AVA requiring @std/esm does not work Dec 21, 2017
@jdalton
Copy link
Member

jdalton commented Dec 22, 2017

Thanks @billyjanitsch!

Our scenario test missed the "inherit" option. It looks like ava/lib/process-adapter.js is the issue:

const precompiled = opts.precompiled[filename];

if (precompiled) {
  sourceMapCache.set(filename, path.join(cacheDir, `${precompiled}.js.map`));
  return fs.readFileSync(path.join(cacheDir, `${precompiled}.js`), 'utf8');
}

If precompiled was null then it would allow @std/esm to be used... but as it is, it gets trapped by the precompiled block. The --no-cache option and config equiv has no affect either. \cc @motss.

It doesn't look like there is a way to disable this precompilation of ava at the moment.
Removed ava from the tips section for now.

@billyjanitsch
Copy link
Author

That's too bad. Thanks for looking into it, @jdalton.

@jdalton
Copy link
Member

jdalton commented Dec 22, 2017

Ah, it's not that bad. I mean, packages using @std/esm still work with AVA. It's just AVA handles ESM tightly on test files at the moment. I think they've eventually get more configurable but as it stands ESM still works with AVA so it's all good.

@jamestalmage
Copy link

See avajs/ava#1618

@jdalton
Copy link
Member

jdalton commented Dec 24, 2017

Thank you @jamestalmage!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

3 participants