greggman / twgl.js

A Tiny WebGL helper Library
http://twgljs.org
MIT License
2.61k stars 258 forks source link

ESM Version? #146

Closed strutcode closed 4 years ago

strutcode commented 4 years ago

Is there any interest in providing an ES6 module version of the library to enable tree shaking? The structure of the library makes it ideal for such an application and the source files are already partially using module syntax.

greggman commented 4 years ago

You have any idea which is better?

A. rollup the src to a single file, twgl.module.js, add `"module": "dist/twgl.module.js" to package.json

B. just include the src folder in files, add `"module": "src/twgl.js" to package.json

zorro-fr24 commented 4 years ago

I can report at least that tree-shaking works with webpack/babel if you selectively import from src/twgl.js, eg:

import {
    createFramebufferInfo,
    createTexture,
    resizeFramebufferInfo,
    setTextureFromArray,
    drawBufferInfo,
    bindFramebufferInfo
} from 'twgl/twgl.js';

original file sizes:

image

vs actual code bundled

image

strutcode commented 4 years ago

@zorro-fr24 Thanks for the tip! I did toy around with that since I noticed the source was written in es module syntax but there are two problems:

  1. The npm package doesn't include the source so you would have to use this repo as your dependency
  2. The Typescript definitions aren't in the src directory so TS complains about the lack of typings and doesn't provide completeion

@greggman I think option A would be nicer since you can always just reference twgl.js as your import, instead of finding the particular thing you're looking for from the source files or doing a bunch of import as stuff. I would be happy with either option though!

greggman commented 4 years ago

I went for option a. I tested and it still shakes the tree

strutcode commented 4 years ago

You are awesome! Thanks so much for doing that. I was going to offer to contribute a pull request but it seems like you have it all under control. Consider this case closed.