Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove core-js and provide transpiled ES Module output #192

Merged
merged 3 commits into from
May 11, 2020
Merged

Conversation

goto-bus-stop
Copy link
Contributor

This does two things 'restore' size and bundler support to v1.x levels:

Disable @babel/preset-env "useBuiltIns"

useBuiltIns injects loads of polyfills that are installed into the
environment. core-js polyfills are very thorough, but that sometimes
means that it includes things that are actually totally unnecessary.

For example, tus-js-client@2 shipped with a Symbol polyfill, even though
it doesn't use Symbols anywhere. But transpiled array rest spread syntax uses
Symbol.iterator, so preset-env figures that Symbol is in use,
and adds the polyfill. This requires core-js to override things like
Object.getOwnPropertyNames to exclude polyfilled Symbols, etc, and it
just spirals out of control until you have 50KB of polyfills just for
array spread syntax and Promises :)

The only feature that tus-js-client actually appears to require is
Promise, which we can reasonably expect users to polyfill (and is already
documented as a requirement).

Transpile package.json "module"

"module" and "main" are both commonly used by bundlers. To support
the widest range of browsers, they should both be transpiled to ES5.
Now lib.esm contains ES5 syntax + ES Modules syntax, so bundlers with
ES Modules support can use it. For the browserify build scripts, we need
to add @babel/plugin-transform-modules-commonjs manually via a command
line parameter.

It's possible and not weird to use newer syntax, but best if people opt
in to it, which they can do with a resolve alias.
In webpack for example:

resolve: {
  alias: {
    'tus-js-client': path.join(__dirname, 'node_modules/tus-js-client/lib')
  }
}

`useBuiltIns` injects loads of polyfills that are installed into the
environment. core-js polyfills are very thorough, but that sometimes
means that it includes things that are actually totally unnecessary.

For example, tus-js-client@2 shipped with a Symbol polyfill, even though
it doesn't use Symbols anywhere. Transpiled array rest spread syntax uses
`Symbol.iterator`, though, so preset-env figures that `Symbol` is in use,
and adds the polyfill. This requires core-js to override things like
`Object.getOwnPropertyNames` to exclude polyfilled Symbols, etc, and it
just spirals out of control until you have 50KB of polyfills just for
array spread syntax and Promises :)

The only feature that tus-js-client _actually_ appears to require is
`Promise`, which we can reasonably expect users to polyfill.
`"module"` and `"main"` are both commonly used by bundlers. To support
the widest range of browsers, they should be transpiled to ES5. It's
possible and not weird to use newer syntax, but best if people opt in
to it, which they can do with a resolve alias. In webpack for example:

```js
resolve: {
  alias: {
    'tus-js-client': path.join(__dirname, 'node_modules/tus-js-client/lib')
  }
}
```
@goto-bus-stop goto-bus-stop requested a review from Acconut May 11, 2020 08:49
Comment on lines +11 to +13
expect(/^tus::fingerprintA::/.test(key1)).toBe(true);
expect(/^tus::fingerprintA::/.test(key2)).toBe(true);
expect(/^tus::fingerprintB::/.test(key3)).toBe(true);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding a polyfill for startsWith, I just used an older way of doing the same thing. This should prob continue to be done for test code, else we might have more polyfills in the tests than we expect users to have, and we could accidentally depend on them in the library code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely agree, you are right! 👍

@Acconut
Copy link
Member

Acconut commented May 11, 2020

Thank you very, very much!

@Acconut Acconut merged commit 6a3f5e5 into master May 11, 2020
@goto-bus-stop goto-bus-stop deleted the un-core-js branch May 11, 2020 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants