lorenzofox3 / zora

Lightest, yet Fastest Javascript test runner for nodejs and browsers
MIT License
537 stars 93 forks source link

fix: use correct default ESM entry #161

Closed doodlewind closed 1 year ago

doodlewind commented 2 years ago

The current default ESM entry in zora is ./es.js, which resolves to the dist/index.cjs bundle. But that is a CommonJS bundle, which doesn't work with Vite. Yet I found another correct setup is also defined in current package.json, which resolves import 'zora/es' statement to dist/index.js, this works well.

So currently for importing zora in Vite, user needs to alias "zora" to "zora/es". This PR is a simple fix.

Current config pasted here:

  "exports": {
    "./package.json": "./package.json",
    ".": {
      // this entry should also import ESM bundle
      "import": "./es.js",
      "require": "./dist/index.cjs"
    },
    "./cjs": "./dist/index.cjs",
    // this works
    "./es": "./dist/index.js"
  },
lorenzofox3 commented 2 years ago

Hi,

This is on purpose for https://github.com/lorenzofox3/zora/issues/155. If that causes trouble to your tool, you should probably directly import the es file which you can easily do with

import {test} from 'zora/es';

@3cp any comment ?

3cp commented 2 years ago

It's unfortunate that it broke vite.

As far as I knew, vite uses two different paths in dev and prod build. In dev build, vite uses esbuild. But in prod build, vite uses rollup.

May I ask does this issue appear in both dev and prod mode? Or just the prod mode?

Another thought is that importing cjs into esm code is well supported by Nodejs, it might be a reasonable feature to ask vite to support it. Both esbuild and rollup are capable to turn cjs code into esm.

lorenzofox3 commented 2 years ago

I reopen the PR then, the time we figure out what's the best approach

Oloompa commented 2 years ago

Hello, I have an issue too after used the new esm typescript tsconfig:

{
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext"
  }
}

After that i have a transpile issue cause zora types are not found anymore.

I found a fix modifying zora package.json like explained here: image

I replaced

{
  "name": "zora",
  "version": "5.1.0",
  "exports": {
    "./package.json": "./package.json",
    ".": {
      "import": "./es.js",
      "require": "./dist/index.cjs"
    },
    "./cjs": "./dist/index.cjs",
    "./es": "./dist/index.js"
  },
}

with (for my ESM need):

{
  "name": "zora",
  "version": "5.1.0",
  "exports": {
    "./package.json": "./package.json",
    ".": {
      "import": {
        "types": "./dist/zora.d.ts",
        "default": "./es.js"
      },
      "require": "./dist/index.cjs"
    },
  },
}
lorenzofox3 commented 1 year ago

closed in favor of #170