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 get TS esm working on async esm babel config and esm ava.config.mjs #47

Closed
yuriy-yarosh opened this issue May 22, 2023 · 1 comment

Comments

@yuriy-yarosh
Copy link

yuriy-yarosh commented May 22, 2023

I'm using node 18 LTS and trying to get ESM working.

@novemberborn as per #5.
I don't think @ava/typescript identifies *.mts test files as ES modules properly, after compiling with babel-preset-typescript instead of tsc and "type": "module".

I'm importing shared esm babel and ava configs from separate packages - i.e. monorepo with separate config-testing workspace for ava and config-babel, for babel instrumentation respectively (yarn berry workspaces tbe).
Using experimental @babel/register/experimental-worker.js to be able to load .babel.mjs config.

Everything is "type": "module" in here.

.ava.config.mjs

import path from 'node:path';
import { fileURLToPath } from 'node:url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export default {
  files: [
    'test/**/*.mts',
  ],
  typescript: {
    extensions: [ 'ts', 'mts', 'cts', 'tsx', 'js','jsx', 'cjs', 'mjs' ],
    rewritePaths: {
      'src/': 'dist/',
    },
    compile: false,
  },
  require: [
    `${__dirname}/Register.js`,
  ],
  failFast: true,
  failWithoutAssertions: false,
  verbose: true,
};

.babelrc.mjs

export default {
  sourceType: 'module',
  targets: {
    node: 'current',
    esmodules: true
  },
  env: {
    coverage: {
      plugins: ['istanbul']
    }
  },
  presets: [
    '@babel/preset-typescript',
    [
      '@babel/preset-env',
      {
        loose: true,
        useBuiltIns: false,
        modules: false,
        bugfixes: true,
        targets: {
            node: 'current'
        },
      }
    ]
  ]
};

I'm not sure if worker is able to identify that it's a module over here. I'm passing the js extension but I don't think I'm able to force using ES modules, by default, and fallback to CommonJS if the import had failed.

Maybe it's worth adding 'ts' 'mts' extensions explicitly over here ?

For some reason it doesn't pick .mts text files as ES Modules.

I'd like to keep babel instrumentation due to certain TSC compatibility issues with the existing coverage report tools, including Node internal coverage reporter.

@novemberborn
Copy link
Member

@ava/typescript@^4 recognizes .mts out of the box. However it rewrites to .mjs files, since that's how tsc behaves.

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