donmccurdy / KTX-Parse

KTX 2.0 (.ktx2) parser and serializer.
MIT License
48 stars 7 forks source link

`Buffer.from` usage cause KTX-parse and three.js includes polyfill whe used with `node-polyfill-webpack-plugin` #147

Closed yue4u closed 2 months ago

yue4u commented 2 months ago

Problem

While Buffer.from used in https://github.com/donmccurdy/KTX-Parse/blob/81959373fac956e88f0feaa2bb42dff4bc9542da/src/util.ts#L49-L62 will not be called if TextEncoder exists (which is true in in modern environment), it does not work well when node-polyfill-webpack-plugin is used in the same application.

node-polyfill-webpack-plugin will check and include the buffer polyfill if any Buffer reference exists. In my setup I separated the polyfills as a different chunk to load dynamiclly if some Web API missing, but in KTX-parse's case it's unintened and hard to work around.

The buffer package is (8kb gziped) and modifies Uint8Array's prototype so better be avoided.

This behavior affects both KTX-parse and the compiled version in Three.js.

Possible fixes

As described in https://sindresorhus.com/blog/goodbye-nodejs-buffer avoid the Buffer global.

  1. Both TextDecoder and TextEncoder are exposed as global in Node.js since v11 (2018) so just delete the check.
  2. Update the compiled version in Three.js