Skip to content
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

errors when bundling #217

Closed
tobowers opened this issue Jan 26, 2019 · 5 comments
Closed

errors when bundling #217

tobowers opened this issue Jan 26, 2019 · 5 comments

Comments

@tobowers
Copy link

I was running into the same errors as #199 and #166 . Also see issues at node-cbor too: hildjj/node-cbor#88

I created a very minimal setup that has the same problems: https://github.com/quorumcontrol/minimal-fail-bignumber

Running: https://github.com/quorumcontrol/minimal-fail-bignumber/blob/master/cbor-bignumber/showerror.sh

produces:

+ node dist/index.js
/Users/tobowers/code/minimal-fail-bignumber/cbor-bignumber/dist/index.js:4144
const MINUS_ONE = new bignumber(-1);
                  ^

TypeError: bignumber is not a constructor
    at Object.parcelRequire.1mQP.bignumber.js (/Users/tobowers/code/minimal-fail-bignumber/cbor-bignumber/dist/index.js:4144:19)
    at newRequire (/Users/tobowers/code/minimal-fail-bignumber/cbor-bignumber/dist/index.js:49:24)
    at localRequire (/Users/tobowers/code/minimal-fail-bignumber/cbor-bignumber/dist/index.js:55:14)
    at Object.parcelRequire.b+YO.../vendor/binary-parse-stream (/Users/tobowers/code/minimal-fail-bignumber/cbor-bignumber/dist/index.js:4268:16)
    at newRequire (/Users/tobowers/code/minimal-fail-bignumber/cbor-bignumber/dist/index.js:49:24)
    at localRequire (/Users/tobowers/code/minimal-fail-bignumber/cbor-bignumber/dist/index.js:55:14)
    at Object.parcelRequire.eVKc../utils (/Users/tobowers/code/minimal-fail-bignumber/cbor-bignumber/dist/index.js:4921:17)
    at newRequire (/Users/tobowers/code/minimal-fail-bignumber/cbor-bignumber/dist/index.js:49:24)
    at localRequire (/Users/tobowers/code/minimal-fail-bignumber/cbor-bignumber/dist/index.js:55:14)
    at Object.parcelRequire.dsOm../commented (/Users/tobowers/code/minimal-fail-bignumber/cbor-bignumber/dist/index.js:6434:21)
@MikeMcl
Copy link
Owner

MikeMcl commented Jan 26, 2019

The error is caused by Parcel using the ES module version of this library, i.e. it is adding the bignumber.mjs file to the bundle instead of the bignumber.js file.

This can be seen by renaming the node_modules/bignumber.js/bignumber.mjs file before running

node node_modules/parcel/bin/cli.js build -t node --no-minify --bundle-node-modules index.js

in which case the bignumber.js file is instead bundled and everything is fine.

worked

Parcel exports the default export from the bignumber.mjs file as a named export, so if all the occurrences of require('bignumber.js') in the bundle, dist/index.js, are replaced with require('bignumber.js').default or require('bignumber.js').BigNumber then the bundle will also work as expected.

@MikeMcl
Copy link
Owner

MikeMcl commented Jan 26, 2019

This line in the Parcel source code shows that when it parses the bignumber.js/package.json it will use the file pointed to by the module field in preference to that of the main field, and this does not seem to be configurable.

This means that the only way to solve this issue is for the module field to be removed from the bignumber.js/package.json.

Do I want to do this because of the limitations of Parcel?

@tobowers
Copy link
Author

First, thanks so much for taking a look! Being an OSS dev is often thankless... I'm here thanking you for the project itself and for looking.

My guess is this will end up being a babel.js bug as both webpack and parcel suffer from the same problem here.

@MikeMcl
Copy link
Owner

MikeMcl commented Jan 26, 2019

Further investigation reveals that removing the module field would cause too many issues when bundling this library using webpack and rollup, as existing configuration files would have to be changed.

For my reference, some notes and links:

Bundlers like webpack and rollup seek to use the ES module version of a library as it improves static analysis and tree-shaking, though this library only has a single export so that may not offer much here.

Parcel bundling seems to work okay irrespective of whether consumers use require or import, as long as it is the UMD bignumber.js that is bundled rather than the ES module bignumber.mjs, i.e. as long as there is no module field pointing to an ES module.

Bundlers grab the UMD bignumber.js file if there is no module field, even though the main field is extensionless and the ES module bignumber.mjs is present.

On the module field:
big.js
2ality
pkg.module
rollup compatibility
rollup faqs
webpack support
webpack docs
old proposal
reddit
what for
choose module
bug
webpack #7482
decimal.js #59

Links providing some rationale for currently having an extensionless main field in the package.json and including both a UMD and ES module version of the library in the root directory:

4.4. Shipping both ESM and CJS

Native ES Modules in NodeJS

@tobowers
Copy link
Author

thanks for this!

@MikeMcl MikeMcl closed this as completed Feb 23, 2019
Repository owner deleted a comment from tony-rockx Jan 23, 2021
Repository owner locked and limited conversation to collaborators Jan 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants