eligrey / eligrey/FileSaver.js

When loading via plain script tag FileSaver.saveAs is not a function. Again.

Open
#500 4 comments 16 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
22k
Forks
4.3k
PR merge metrics
No merged PRs in 30d

Description

When loading dist/FileSaver.js via ``, `FileSaver.saveAs()` is not available. Here are the facts:

- `FileSaver` is an empty object `{}`
- `_global`, which refers to the `Window` object is in the global namespace
- `saveAs()` is also in the global namespace, rather than as a member of `FileSaver`

The culprit is in the packaging:

https://github.com/eligrey/FileSaver.js/blob/be09a02e8cd3fe7046a692b19f6aa9631d0146c7/dist/FileSaver.js#L7-L11

Note that:

1. `mod.exports` is set to empty object `{}`
2. Then `factory()` is called _without arguments_, and is thus unable to populate `mod.exports` as that reference is out of scope.
3. Finally, `window.FileSaver` is set to `mod.exports`, **which is an empty object `{}` at this point**.
4. Thus, `FileSaver` is just an empty object. This seems like a bug.

If this in intended behavior, it clashes with the docs. The only way to use `FileSaver` in a page that does not have module infrastructure is to use `window.saveAs()`, which seems really strange. Probably risky too of getting clobbered by some other library.

This seems related to #253, which was closed by @jimmywarting, presumably after a fix. However, this seems to be new behavior that is plaguing `v2.0.0`.

If this behavior is intentional, it's certainly strange (and where is it documented)?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.