zora icon indicating copy to clipboard operation
zora copied to clipboard

fix: use correct default ESM entry

Open doodlewind opened this issue 1 year ago • 3 comments

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"
  },

doodlewind avatar Jul 16 '22 10:07 doodlewind

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 ?

lorenzofox3 avatar Jul 17 '22 08:07 lorenzofox3

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.

3cp avatar Jul 17 '22 09:07 3cp

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

lorenzofox3 avatar Jul 17 '22 10:07 lorenzofox3

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"
    },
  },
}

Oloompa avatar Nov 07 '22 15:11 Oloompa

closed in favor of #170

lorenzofox3 avatar Nov 18 '22 10:11 lorenzofox3