-
-
Notifications
You must be signed in to change notification settings - Fork 8k
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
Requesting ECMAScript module (ESM) bundle support #2033
Comments
CommonJS requires no language changes and thus is compatible with older IE and other environments like PhotoShop extensions. ESM breaks older browsers and legacy environments with the introduction of the new That said, is there some stopgap measure? Can we create a dummy index.mjs file that imports the standalone packaged dist script? We could also generate a script that just concatenates the dist script with "export default XLSX;" |
Yep, I understand the goal is wide support, but the stopgap solution you've suggested seems reasonable to me! |
Was this issue solved? I work with Angular as well :) |
@SheetJSDev what needs to be done to get any of those two stopgap measures done? Would it be possible to publish the dist script concatenated with the default export as a separate package or..? |
I beleive you should still have a version with ECMAScript. |
I'm running into this same issue. The resulting file is very large (725kB) and slow. If we could get this working as an ES module, it would help a lot. |
Any advance on this? |
Team I get this warning, what should be done? no clear answer |
There's nothing to do as long as they don't update the librairie. It still works tought. It's just a warning to tell you it might take longer to load the app |
Adding my vote. As a sort of workaround, you can use lazy loading modules to at least prevent it from being include in the initial download. You need to make sure that any refences to XLSX are isolated to the lazily loaded module. You can use webpack-bundle-analyzer to analyze your app and see what's included in the main.js file. |
The write codecs and utility functions are easy to split, but the read codecs are harder. The whole point of It is theoretically possible to modularize as follows:
This would imply end-user code that looks like: import { read as read_XLSX, write as write_XLSX } from 'xlsx/codecs/xlsx';
import { read as read_XLS } from 'xlsx/codecs/xls';
import { readFile, writeFile, read, write, load_read_codec, load_write_codec } from 'xlsx';
[read_XLSX, read_XLS].forEach(c => load_read_codec(c));
[write_XLSX].forEach(c => load_write_codec(c))
const good_wb = readFile("valid.xlsx"); // assuming valid.xlsx is a valid XLSX file, returns a workbook
try {
const bad_wb = readFile("invalid.htm"); // if the HTML codec is not loaded, throw an error
} catch(e) { }
XLSX.writeFile(good_wb, "out.xlsx"); // succeeds since XLSX write codec was loaded
try {
XLSX.writeFile(good_wb, "out.xls"); // fails since XLS write codec was not loaded
} catch(e) { } |
Hi all, i've same issue on my angular 11 app, is there now a final solution ? |
The codec loading version is cool. It looks very similar to angular: import { registerLocaleData } from '@angular/common';
import localeCs from '@angular/common/locales/cs';
registerLocaleData(localeCs); If the error in the reading/writing file would have info about the missing codec, it could be loaded asychronousy. async function writeFile(data, filename) {
try {
return XLSX.writeFile(data, filename);
} catch(e) {
if (e instance MissingCodecError) {
// If we know that codec is missing try to load it
return import(`xlsx/codecs/${e.missingCodec}`)
// Install it
.then(module => load_write_codec(module.default))
// Then retry it
.then(() => writeFile(data, filename));
} else {
// If it is some other error, just pass it out
throw e;
}
}
} |
A rough cut of both Browser and NodeJS ESM has been committed and will be pushed in the next release. |
I'm still seeing this warning in the latest release (0.17.3) when using Angular. Anything I should be doing differently than just using it directly (like below)?
|
The ESM builds use the .mjs extension. TypeScript recently added support for the extension, but since the latest version of angular still pins to typescript versions below 4.4.0 it is currently not possible to use it. |
adding this as an (hopefully) final update to this for angular: the latest RC release for angular 14 supports typescript 4.7.0, which officially added support for |
In 0.18.1 the module field was added to package.json. Testing against the angular2+ demo, |
oh wow, wasn't aware about that change ! just tested with 0.18.5, the issue is definitely fixed in angular :D 0.17.4
0.18.5
|
If anyone else is coming here trying to understand how to migrate, this is what I had to change in unicode-org/cldr#2153 : -import XLSX from "xlsx";
+import * as XLSX from "xlsx"; |
The original issue has been updated with some comments and a link to the current documentation. |
@srl295 |
Please install as described in https://docs.sheetjs.com/docs/getting-started/installation/frameworks Note that ViteJS support requires 0.18.10+ . Please raise a new issue if you encounter any problems. |
EDIT: Please read and follow the instructions at https://docs.sheetjs.com/docs/getting-started/installation/frameworks before raising a new issue.
When this issue was raised, the module only shipped with a CommonJS / UMD build. Projects using Webpack or other bundlers used the default import syntax to pull the CommonJS build:
v0.18.1 activated the ESM build for common tooling. See https://docs.sheetjs.com/docs/installation/frameworks for more details. The literal equivalent of the old import is
Tree Shaking is supported with the ESM build!. If a project is only exporting XLSX files (using
utils
andwriteFile
), using named imports withwriteFileXLSX
drastically reduces bundle size:Sheet.js is providing the following warning in the Angular CLI for the new v10 release:
Starting with Angular 10 the Angular CLI now provide warnings for CommonJS modules. Read more about it here:
https://blog.angular.io/version-10-of-angular-now-available-78960babd41
And here:
https://angular.io/guide/build#configuring-commonjs-dependencies
They reference the following article regarding the issues with CommonJS:
https://web.dev/commonjs-larger-bundles/
Additionally I've seen a few discussions by Angular staff members on Github discussing potentially dropping CommonJS support by default, and making it an opt-in feature in the future.
I realize the Angular team is taking a very strong armed approach here, but their reasoning seems solid. Needless to say I'd love to see ESM support if possible. Thanks!
The text was updated successfully, but these errors were encountered: