emmanueltouzery / prelude-ts

Functional programming, immutable collections and FP constructs for typescript and javascript
ISC License
377 stars 21 forks source link

Switch custom node object inspection to use Symbol.for #53

Closed wuzzeb closed 3 years ago

wuzzeb commented 3 years ago

util.inspect.custom is a symbol provided by node which allows custom printing in the repl but is not available in the browser. Bundlers like webpack polyfill this by default but other bundlers like vite do not (although vite can be configured to polyfill it). But, to avoid the need to polyfill at all, nodejs also exposes the symbol at Symbol.for("nodejs.util.inspect.custom") on node v11 and up.

https://nodejs.org/api/util.html#util_util_inspect_custom

To avoid the need to polyfill, switch Value.ts to call Symbol.for("nodejs.util.inspect.custom"). In node, this will use the correct symbol to allow inspection and printing on the repl, while in the browser it will create a new harmless symbol. As an added benefit, the return type of Symbol.for is already a unique symbol so no need for the explicit definition.

Note that Symbol.for is not available in IE11 and will need to be polyfilled if you want to still support IE11.

Fixes #52

wuzzeb commented 3 years ago

Hmm, the test passes on my computer but fails on circleci

wuzzeb commented 3 years ago

Oh, the circleCI build is using Node v8 but the symbol was added in Node v11.

Do you still want to support node 8 and 10 or just update to node 12?

emmanueltouzery commented 3 years ago

Sorry for being so slow to react. Thank you for your efforts. Node 8 and 10 are end-of-life so requiring node 12 is not a problem.

emmanueltouzery commented 3 years ago

yeah that's good. this failure is unrelated. i'll just double check that the code still works in the node REPL as expected then merge and release soon after.

emmanueltouzery commented 3 years ago

well, from my testing you didn't only fix the bundling with Vite, but also the nicer nodejs REPL display of prelude-ts with newer versions of node! So thanks a lot!