ennuicastr / libavjs-webcodecs-polyfill

A polyfill for the WebCodecs API. No, really.
82 stars 8 forks source link

Migrate building to rollup with mjs support #13

Closed Donil closed 1 year ago

Donil commented 1 year ago

Hi! That is another one PR - adding support of MJS (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) bundle.

I made this with rollup since it's really good build tool and browserify is not needed anymore since we have ES2015 modules.

So for now we have two bundles:

  1. libavjs-webcodecs-polyfill.min.js - UMD bundle (the same as you have now)
  2. libavjs-webcodecs-polyfill.min.mjs - MJS bundle that is friendly for codebases that need to distribute as mjs

Take a look on it and say if you are interested with that way

Yahweasel commented 1 year ago

Y'know what everybody loves?

People being opinionated about build systems.

Donil commented 1 year ago

Y'know what everybody loves?

People being opinionated about build systems.

Sorry, it seems I incorrectly explained the goal of this PR. The main point is getting MJS bundle. I have not found the way to do that with browserfy.

I can rework this PR to do that with browserify if you could help me with browserify. Thanks!

Donil commented 1 year ago

One more target - generating type declaration files. Now you provide main.ts as types (in the package.json). This approach makes integration with TS a little bit harder - TS of the host codebase runs type checking also on this library. But if the current library provides d.ts files this will work well without replacing simple import * as LibAVWebCodecs from 'libavjs-webcodecs-polyfill'; with

import type * as LibAVWebCodecsJS from 'libavjs-webcodecs-polyfill';

let LibAVWebCodecs: typeof LibAVWebCodecsJS;
Yahweasel commented 1 year ago

I believe the answer for .mjs is esmify. Not sure what the answer is for .d.ts, but I'm sure tsify does it.

Donil commented 1 year ago

Yes, generating d.ts files is not problem with browserfy, but generating .mjs file is not possible I think. esmify transfrorm from "import / export" to browserify but not to bundle with "import/export". Correct me if I'm wrong

Donil commented 1 year ago

It seems I've found the reason on why I can not use current UMD bundle with my case - I'm working on JS library and want to build it into the signle JS file (as you library does). For that I have to embed libavjs-webcodecs-polyfill into my bundle. But since polyfill is standalone UMD module it has this code:

if (typeof exports === "object" && typeof module !== "undefined") {
  module.exports = f();
}

So it just overrides my ESM module exports and breaks it. I resolved it with https://www.npmjs.com/package/@rollup/plugin-commonjs for my case so I'm closing this PR and will return with new PR soon - to add d.ts files generation :)