The mini and core versions are not supported for usage with bundlers #2977

Closed
opened 2023-08-18 15:09:22 +00:00 by stof · 4 comments

The dist/xlsx.core.min.js and dist/xlsx.mini.min.js files are not exposed in the package export, which means that bundlers support package exports (for instance webpack) will report them as non-existent.

Side note: it would be great to have them in mjs format.

The `dist/xlsx.core.min.js` and `dist/xlsx.mini.min.js` files are not exposed in the package export, which means that bundlers support package exports (for instance webpack) will report them as non-existent. Side note: it would be great to have them in mjs format.
Owner

How can this be reproduced (e.g. what version of webpack) and what is the recommended package.json pattern (should it be under import or require or another key)?

dist/xlsx.core.min.js, for example, is the rough equivalent of a glob import of the module script.

PS: It feels like these packaging patterns change every year :(

How can this be reproduced (e.g. what version of webpack) and what is the recommended `package.json` pattern (should it be under `import` or `require` or another key)? `dist/xlsx.core.min.js`, for example, is the rough equivalent of a glob import of the module script. PS: It feels like these packaging patterns change every year :(
Author

Well, if you add the exports section in a package.json, any non-exported pattern becomes private. And this section has been added. That's why the packaging changes. You cannot do only half of the change...

Good to know that core is equivalent in size to using the mjs version.

Regarding the mini version, it should be a new section (similar to how ./dist/cpexcel.js is registered in it for instance). Inside that object, the import key should reference the mjs version and the require key should reference the cjs version (it can be an UMD version as UMD supports being imported as CJS). The types key should probably reference a new mini.d.ts file that would contain only the types for the mini API (the index.d.ts could then have export * from './mini.js' instead of duplicating them all, with the node-only APIs staying in index.d.ts

Well, if you _add_ the `exports` section in a package.json, any non-exported pattern becomes private. And this section has been added. That's why the packaging changes. You cannot do only half of the change... Good to know that core is equivalent in size to using the mjs version. Regarding the mini version, it should be a new section (similar to how `./dist/cpexcel.js` is registered in it for instance). Inside that object, the `import` key should reference the mjs version and the `require` key should reference the cjs version (it can be an UMD version as UMD supports being imported as CJS). The `types` key should probably reference a new `mini.d.ts` file that would contain only the types for the mini API (the `index.d.ts` could then have `export * from './mini.js'` instead of duplicating them all, with the node-only APIs staying in `index.d.ts`
Owner

Starting with ./dist/xlsx.core.min.js, it looks like the following works when manually editing package.json in a project:

		"./dist/xlsx.core.min.js": {
			"import": "./dist/xlsx.core.min.js",
			"require": "./dist/xlsx.core.min.js",
			"types": "./types/index.d.ts"
		},

This was tested by creating a small project based on the Webpack static asset demo, adding the aforementioned lines to package.json in the exports field, and adding these lines to src/index.js:

import { version } from "xlsx/dist/xlsx.core.min.js";
console.log(version);

The require equivalent also appears to work:

const XLSX = require("xlsx/dist/xlsx.core.min.js");
console.log(XLSX.version);

If manually making those changes works in your setup, we can add those blocks for each script (core / full / mini, with and without the .js extension)

Starting with `./dist/xlsx.core.min.js`, it looks like the following works when manually editing package.json in a project: ```js "./dist/xlsx.core.min.js": { "import": "./dist/xlsx.core.min.js", "require": "./dist/xlsx.core.min.js", "types": "./types/index.d.ts" }, ``` This was tested by creating a small project based on the [Webpack static asset demo](https://docs.sheetjs.com/docs/demos/static/webpack#webpack-5-demo), adding the aforementioned lines to `package.json` in the `exports` field, and adding these lines to `src/index.js`: ```js import { version } from "xlsx/dist/xlsx.core.min.js"; console.log(version); ``` The `require` equivalent also appears to work: ```js const XLSX = require("xlsx/dist/xlsx.core.min.js"); console.log(XLSX.version); ``` If manually making those changes works in your setup, we can add those blocks for each script (core / full / mini, with and without the `.js` extension)
Owner

The 6 aliases have been added.

As a smoke test, the attached build throws unique error messages for each dist script and for the main/module scripts. Testing with the demo from https://docs.sheetjs.com/docs/demos/frontend/bundler#webpack :

// xlsx.mjs
import { utils, version, writeFileXLSX } from 'xlsx';
import { utils, version, writeFileXLSX } from 'xlsx/xlsx.mjs';

// xlsx.js
const { utils, version, writeFileXLSX } = require("xlsx");
const { utils, version, writeFileXLSX } = require("xlsx/xlsx.js");

// xlsx.full.min.js
import { utils, version, writeFileXLSX } from 'xlsx/dist/xlsx.full.min';
import { utils, version, writeFileXLSX } from 'xlsx/dist/xlsx.full.min.js';
const { utils, version, writeFileXLSX } = require("xlsx/dist/xlsx.full.min");
const { utils, version, writeFileXLSX } = require("xlsx/dist/xlsx.full.min.js");

// xlsx.core.min.js
import { utils, version, writeFileXLSX } from 'xlsx/dist/xlsx.core.min';
import { utils, version, writeFileXLSX } from 'xlsx/dist/xlsx.core.min.js';
const { utils, version, writeFileXLSX } = require("xlsx/dist/xlsx.core.min");
const { utils, version, writeFileXLSX } = require("xlsx/dist/xlsx.core.min.js");

// xlsx.mini.min.js
import { utils, version, writeFileXLSX } from 'xlsx/dist/xlsx.mini.min';
import { utils, version, writeFileXLSX } from 'xlsx/dist/xlsx.mini.min.js';
const { utils, version, writeFileXLSX } = require("xlsx/dist/xlsx.mini.min");
const { utils, version, writeFileXLSX } = require("xlsx/dist/xlsx.mini.min.js");
The 6 aliases have been added. As a smoke test, the attached build throws unique error messages for each dist script and for the main/module scripts. Testing with the demo from https://docs.sheetjs.com/docs/demos/frontend/bundler#webpack : ```js // xlsx.mjs import { utils, version, writeFileXLSX } from 'xlsx'; import { utils, version, writeFileXLSX } from 'xlsx/xlsx.mjs'; // xlsx.js const { utils, version, writeFileXLSX } = require("xlsx"); const { utils, version, writeFileXLSX } = require("xlsx/xlsx.js"); // xlsx.full.min.js import { utils, version, writeFileXLSX } from 'xlsx/dist/xlsx.full.min'; import { utils, version, writeFileXLSX } from 'xlsx/dist/xlsx.full.min.js'; const { utils, version, writeFileXLSX } = require("xlsx/dist/xlsx.full.min"); const { utils, version, writeFileXLSX } = require("xlsx/dist/xlsx.full.min.js"); // xlsx.core.min.js import { utils, version, writeFileXLSX } from 'xlsx/dist/xlsx.core.min'; import { utils, version, writeFileXLSX } from 'xlsx/dist/xlsx.core.min.js'; const { utils, version, writeFileXLSX } = require("xlsx/dist/xlsx.core.min"); const { utils, version, writeFileXLSX } = require("xlsx/dist/xlsx.core.min.js"); // xlsx.mini.min.js import { utils, version, writeFileXLSX } from 'xlsx/dist/xlsx.mini.min'; import { utils, version, writeFileXLSX } from 'xlsx/dist/xlsx.mini.min.js'; const { utils, version, writeFileXLSX } = require("xlsx/dist/xlsx.mini.min"); const { utils, version, writeFileXLSX } = require("xlsx/dist/xlsx.mini.min.js"); ```
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#2977
No description provided.