gray-matter icon indicating copy to clipboard operation
gray-matter copied to clipboard

Require Buffer rather than expecting a global

Open kemitchell opened this issue 4 years ago • 2 comments

This tiny PR adds const Buffer = require('buffer').Buffer; to the top of lib/util.js, so that util.js can call Buffer.from without relying on Buffer being a global. This allows code bundlers and other static analyzers to detect the use of Buffer and shim it (e.g. with npm:buffer) if it is not part of the target environment, such as when bundling for web browsers.

I suspect this approach might satisfy some of the people describing hold-ups in #68. I have been able to use this fork to bundle gray-matter with esbuild without error.

I would be comfortable publishing this with a patch-level version bump.

kemitchell avatar Dec 21 '21 23:12 kemitchell

I just upgraded react-scripts to v5 and hit #68. For now, I added temporarily global.Buffer = global.Buffer || require('buffer').Buffer; in my codebase, but would appreciate this PR be merged to solve the issue.

firmart avatar Dec 23 '21 02:12 firmart

@jonschlinkert Are you still maintaining this library? It would be quite helpful to get this merged so it can work with modern WebPack 5+, Vite, and so on, which no longer include node shims by default. There are also several other PRs that help keep this library in shape that you might want look at.

segevfiner avatar Apr 20 '23 19:04 segevfiner