add exports field to package.json #2744

Merged
BrianHung merged 2 commits from exports-field-package into master 2022-07-17 21:51:39 +00:00
BrianHung commented 2022-07-15 22:53:59 +00:00 (Migrated from github.com)
No description provided.
SheetJSDev commented 2022-07-17 21:02:37 +00:00 (Migrated from github.com)

What tools or platforms are affected by the change?

What tools or platforms are affected by the change?
BrianHung commented 2022-07-17 21:12:26 +00:00 (Migrated from github.com)

Running into an issue using SheetJS with vite: https://github.com/vitejs/vite/discussions/9149
Vite is using the CJS distribution of SheetJS and is incorrectly converting that to ESM. This config change will allow Vite to use the ESM distribution of SheetJS directly.

Running into an issue using SheetJS with vite: https://github.com/vitejs/vite/discussions/9149 Vite is using the CJS distribution of SheetJS and is incorrectly converting that to ESM. This config change will allow Vite to use the ESM distribution of SheetJS directly.
SheetJSDev commented 2022-07-17 21:28:57 +00:00 (Migrated from github.com)

The last time we seriously looked into modules / ESM support, tooling looked for the module field in package.json. This sufficed:

	"main": "xlsx.js",
	"module": "xlsx.mjs",

If Vite requires something extra, changes can be made as long as they don't break older NodeJS versions or other bundlers.

There are two changes here: type: "module" and exports.

Based on the NodeJS docs, type: "module" affects interpretations of .js files, but there is no confusion here: the .js files are CommonJS and the .mjs files are ESM.

With the goal of persuading Vite to use xlsx.mjs when importing, would the exports field suffice (is "type": "module" also needed)?

The last time we seriously looked into modules / ESM support, tooling looked for the `module` field in package.json. This sufficed: ```json "main": "xlsx.js", "module": "xlsx.mjs", ``` If Vite requires something extra, changes can be made as long as they don't break older NodeJS versions or other bundlers. There are two changes here: `type: "module"` and `exports`. [Based on the NodeJS docs](https://nodejs.org/api/packages.html#type), `type: "module"` affects interpretations of `.js` files, but there is no confusion here: the `.js` files are CommonJS and the `.mjs` files are ESM. With the goal of persuading Vite to use `xlsx.mjs` when importing, would the `exports` field suffice (is `"type": "module"` also needed)?
BrianHung commented 2022-07-17 21:46:58 +00:00 (Migrated from github.com)

I got that off the Vite docs which recommends the following for libraries:

https://vitejs.dev/guide/build.html#library-mode

{
  "name": "my-lib",
  "type": "module",
  "files": ["dist"],
  "main": "./dist/my-lib.umd.cjs",
  "module": "./dist/my-lib.js",
  "exports": {
    ".": {
      "import": "./dist/my-lib.js",
      "require": "./dist/my-lib.umd.cjs"
    }
  }
}

After reading the NodeJS docs, type: module isn't useful here if the source code isn't written in esm and if it isn't meant to be directly used (?). I'll update the PR.

I got that off the Vite docs which recommends the following for libraries: https://vitejs.dev/guide/build.html#library-mode ```js { "name": "my-lib", "type": "module", "files": ["dist"], "main": "./dist/my-lib.umd.cjs", "module": "./dist/my-lib.js", "exports": { ".": { "import": "./dist/my-lib.js", "require": "./dist/my-lib.umd.cjs" } } } ``` After reading the NodeJS docs, `type: module` isn't useful here if the source code isn't written in esm and if it isn't meant to be directly used (?). I'll update the PR.
SheetJSDev commented 2022-07-26 02:57:42 +00:00 (Migrated from github.com)

We pushed 0.18.10 (mainly to make sure that the package.json changes worked)

Does it work in your testing with Vite?

We pushed 0.18.10 (mainly to make sure that the package.json changes worked) Does it work in your testing with Vite?
BrianHung commented 2022-07-26 04:06:22 +00:00 (Migrated from github.com)

We pushed 0.18.10 (mainly to make sure that the package.json changes worked)

Does it work in your testing with Vite?

Yep, thanks for the quick resolution 🤗.

> We pushed 0.18.10 (mainly to make sure that the package.json changes worked) > > > > Does it work in your testing with Vite? Yep, thanks for the quick resolution 🤗.
Sign in to join this conversation.
No description provided.