Skip to content

Commit

Permalink
Merge pull request #83 from MattiasBuelens/reduce-bundle-size
Browse files Browse the repository at this point in the history
* Rework variants to have more modern defaults
* Remove the ES2018 variant
* Support importing as ESM in Node
* Minify all published code to reduce the bundle size
  • Loading branch information
MattiasBuelens authored Sep 6, 2021
2 parents 2707913 + 3e1d1e8 commit 2c9c5f3
Show file tree
Hide file tree
Showing 52 changed files with 6,793 additions and 692 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"AbortSignal": false,
"DEBUG": false,
"GCController": false,
"gc": false
"gc": false,
"globalThis": false
},
"rules": {
// Possible errors
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x]
node-version: [12.x, 14.x]
# Run only on non-merged non-draft mergeable pull requests
if: |
!(
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x]
node-version: [12.x, 14.x]
# Skip job if commit message contains "[skip ci]"
if: |
!contains(github.event.head_commit.message, '[skip ci]')
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,5 @@ typings/
/dist/
/lib/
/temp/
/types/ponyfill.d.ts
/types/tsdoc-metadata.json
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@
> - 🏠 Internal
> - 💅 Polish
## Unreleased

* 💥 Rework the list of variants to have more modern defaults.
The table below shows how to upgrade your v3 import to their equivalent v4 import.
See the [migration guide](https://github.com/MattiasBuelens/web-streams-polyfill/blob/next/MIGRATING.md) for more information.
([#82](https://github.com/MattiasBuelens/web-streams-polyfill/issues/82), [#83](https://github.com/MattiasBuelens/web-streams-polyfill/pull/83))
* 💥 Remove the ES2018 variant, in favor of the ES2015 variant.
* 🚀 Support [importing as ESM in Node](https://nodejs.org/api/esm.html).
* 💅 Minify all code in the published package, to reduce the download size.

| v3 import | v4 import | description |
| --- | --- | --- |
| `web-streams-polyfill` | `web-streams-polyfill/polyfill/es5` | ES5+ polyfill |
| `web-streams-polyfill/es6` | `web-streams-polyfill/polyfill` | ES2015+ polyfill |
| `web-streams-polyfill/es2018` | `web-streams-polyfill/polyfill` | ES2015+ polyfill |
| `web-streams-polyfill/ponyfill` | `web-streams-polyfill/es5` | ES5+ ponyfill |
| `web-streams-polyfill/ponyfill/es6` | `web-streams-polyfill` | ES2015+ ponyfill |
| `web-streams-polyfill/ponyfill/es2018` | `web-streams-polyfill` | ES2015+ ponyfill |

## v3.1.1 (2021-09-06)

* 🐛 Fix compatibility with TypeScript's DOM types for `ReadableStream` and `TransformStream` ([#85](https://github.com/MattiasBuelens/web-streams-polyfill/issues/85), [#86](https://github.com/MattiasBuelens/web-streams-polyfill/pull/86))
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2020 Mattias Buelens
Copyright (c) 2021 Mattias Buelens
Copyright (c) 2016 Diwank Singh Tomer

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
40 changes: 40 additions & 0 deletions MIGRATING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Migrating

## Version 3 to 4

Version 4 restructures the library's exports to have more modern defaults, and employs modern best practices for
publishing npm packages.

* The default export (i.e. `import "web-streams-polyfill"`) is now the *ponyfill* variant instead of the *polyfill*
variant, to avoid modifying the global scope. If you do want to install the polyfill on the global scope, switch to
the `polyfill` variant.
* The default export uses ES2015 syntax and targets modern Node and browser environments. If you need to support ES5
environments, switch to either the `es5` or `polyfill/es5` variant.
* The polyfill variant no longer re-exports the ponyfill,
so `import { ReadableStream } from "web-streams-polyfill/polyfill"` won't work. Instead, use the
global `ReadableStream` variable directly after loading the polyfill, or switch to the default (non-polyfill) variant.
* The project
uses [Node's package entry points](https://nodejs.org/api/packages.html#packages_package_entry_points) (`exports`
in `package.json`) to provide each variant as
a [dual module (UMD/ESM)](https://nodejs.org/api/packages.html#packages_dual_commonjs_es_module_packages). You need
Node 12.20.0 or higher to resolve these variants. If you're using a bundler (like webpack or Rollup), you need to make
sure it's up-to-date too.

Version 4 also focuses on reducing the download size of the published npm package.

* All published JavaScript code is now minified, without source maps. If you need to debug the polyfill, you
can [clone it](https://github.com/MattiasBuelens/web-streams-polyfill)
and [build it yourself](https://github.com/MattiasBuelens/web-streams-polyfill/blob/v3.1.0/CONTRIBUTING.md).
* The ES2018 variant was removed, since it had only minor differences with the ES2015 variant. If you were using this
variant, switch to the default export instead.

The following table shows how to upgrade your v3 import to their equivalent v4 import:

| v3 import | v4 import | description |
| --- | --- | --- |
| `web-streams-polyfill` | `web-streams-polyfill/polyfill/es5` | ES5+ polyfill |
| `web-streams-polyfill/es6` | `web-streams-polyfill/polyfill` | ES2015+ polyfill |
| `web-streams-polyfill/es2018` | `web-streams-polyfill/polyfill` | ES2015+ polyfill |
| `web-streams-polyfill/ponyfill` | `web-streams-polyfill/es5` | ES5+ ponyfill |
| `web-streams-polyfill/ponyfill/es6` | `web-streams-polyfill` | ES2015+ ponyfill |
| `web-streams-polyfill/ponyfill/es2018` | `web-streams-polyfill` | ES2015+ ponyfill |
81 changes: 46 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,80 +15,89 @@ Web Streams, based on the WHATWG spec reference implementation.
## Usage

This library comes in multiple variants:
* `web-streams-polyfill`: a polyfill that replaces the native stream implementations.
Recommended for use in web apps supporting older browsers through a `<script>` tag.
* `web-streams-polyfill/es6`: a polyfill targeting ES2015+ environments.
Recommended for use in web apps supporting modern browsers through a `<script>` tag.
* `web-streams-polyfill/es2018`: a polyfill targeting ES2018+ environments.
* `web-streams-polyfill/ponyfill`: a [ponyfill] that provides
the stream implementations without replacing any globals.
Recommended for use in legacy Node applications, or in web libraries supporting older browsers.
* `web-streams-polyfill/ponyfill/es6`: a ponyfill targeting ES2015+ environments.
Recommended for use in Node 6+ applications, or in web libraries supporting modern browsers.
* `web-streams-polyfill/ponyfill/es2018`: a ponyfill targeting ES2018+ environments.
Recommended for use in Node 10+ applications.
* `web-streams-polyfill`: a [ponyfill] that provides the stream implementations
without replacing any globals, targeting ES2015+ environments.
* ✅ Node 6+ through `import` or `require()`
* ✅ Modern web browsers through `import`/`export` or `<script type="module">`
* ✅ Web apps for modern browsers using a bundler (like webpack or Rollup)
* `web-streams-polyfill/es5`: a ponyfill targeting ES5+ environments.
* ✅ Legacy Node through `require()`
* ✅ Legacy web browsers through [AMD][amd]
* ✅ Web apps for legacy browsers using a bundler (like webpack or Rollup)
* `web-streams-polyfill/polyfill`: a polyfill that replaces the native stream implementations,
targeting ES2015+ environments.
* ✅ Modern web browsers through regular `<script>`
* `web-streams-polyfill/polyfill/es5`: a polyfill targeting ES5+ environments.
* ✅ Legacy web browsers through regular `<script>`

Each variant also includes TypeScript type definitions, compatible with the DOM type definitions for streams included in TypeScript.

In version 4, the list of variants was reworked to have more modern defaults and to reduce the download size of the package.
See the [migration guide][migrating] for more information.

Usage as a polyfill:
```html
<!-- option 1: hosted by unpkg CDN -->
<script src="https://unpkg.com/web-streams-polyfill/dist/polyfill.min.js"></script>
<script src="https://unpkg.com/web-streams-polyfill/dist/polyfill.js"></script>
<!-- option 2: self hosted -->
<script src="/path/to/web-streams-polyfill/dist/polyfill.min.js"></script>
<script src="/path/to/web-streams-polyfill/dist/polyfill.js"></script>
<script>
var readable = new ReadableStream();
</script>
```
Usage as a Node module:
```js
var streams = require("web-streams-polyfill/ponyfill");
var streams = require("web-streams-polyfill");
var readable = new streams.ReadableStream();
```
Usage as a ES2015 module:
Usage as a ponyfill from within a ES2015 module:
```js
import { ReadableStream } from "web-streams-polyfill";
const readable = new ReadableStream();
```
Usage as a polyfill from within an ES2015 module:
```js
import { ReadableStream } from "web-streams-polyfill/ponyfill";
import "web-streams-polyfill/polyfill";
const readable = new ReadableStream();
```

### Compatibility
## Compatibility

The `polyfill` and `ponyfill` variants work in any ES5-compatible environment that has a global `Promise`.
The default and `polyfill` variants work in any ES2015-compatible environment.

The `es5` and `polyfill/es5` variants work in any ES5-compatible environment that has a global `Promise`.
If you need to support older browsers or Node versions that do not have a native `Promise` implementation
(check the [support table][promise-support]), you must first include a `Promise` polyfill
(e.g. [promise-polyfill][promise-polyfill]).

The `polyfill/es6` and `ponyfill/es6` variants work in any ES2015-compatible environment.

The `polyfill/es2018` and `ponyfill/es2018` variants work in any ES2018-compatible environment.

[Async iterable support for `ReadableStream`][rs-asynciterator] is available in all variants, but requires an ES2018-compatible environment or a polyfill for `Symbol.asyncIterator`.

[`WritableStreamDefaultController.signal`][ws-controller-signal] is available in all variants, but requires a global `AbortController` constructor. If necessary, consider using a polyfill such as [abortcontroller-polyfill].

### Compliance
## Compliance

The polyfill implements [version `cada812` (8 Jul 2021)][spec-snapshot] of the streams specification.

The polyfill is tested against the same [web platform tests][wpt] that are used by browsers to test their native implementations.
The polyfill aims to pass all tests, although it allows some exceptions for practical reasons:
* The `es2018` variant passes all of the tests, except for the ["bad buffers and views" tests for readable byte streams][wpt-bad-buffers].
These tests require the implementation to synchronously transfer the contents of an `ArrayBuffer`, which is not yet possible from JavaScript (although there is a [proposal][proposal-arraybuffer-transfer] to make it possible).
The reference implementation "cheats" on these tests [by making a copy instead][ref-impl-transferarraybuffer], but that is unacceptable for the polyfill's performance ([#3][issue-3]).
* The `es6` variant passes the same tests as the `es2018` variant, except for the [test for the prototype of `ReadableStream`'s async iterator][wpt-async-iterator-prototype].
Retrieving the correct `%AsyncIteratorPrototype%` requires using an async generator (`async function* () {}`), which is invalid syntax before ES2018.
Instead, the polyfill [creates its own version][stub-async-iterator-prototype] which is functionally equivalent to the real prototype.
* The `es5` variant passes the same tests as the `es6` variant, except for various tests about specific characteristics of the constructors, properties and methods.
It aims to pass all tests, although it allows some exceptions for practical reasons:
* The default (ES2015) variant passes all of the tests, except for:
* The ["bad buffers and views" tests for readable byte streams][wpt-bad-buffers].
These tests require the implementation to synchronously transfer the contents of an `ArrayBuffer`, which is not yet possible from JavaScript (although there is a [proposal][proposal-arraybuffer-transfer] to make it possible).
The reference implementation "cheats" on these tests [by making a copy instead][ref-impl-transferarraybuffer], but that is unacceptable for the polyfill's performance ([#3][issue-3]).
* The [test for the prototype of `ReadableStream`'s async iterator][wpt-async-iterator-prototype].
Retrieving the correct `%AsyncIteratorPrototype%` requires using an async generator (`async function* () {}`), which is invalid syntax before ES2018.
Instead, the polyfill [creates its own version][stub-async-iterator-prototype] which is functionally equivalent to the real prototype.
* The ES5 variant passes the same tests as the ES2015 variant, except for various tests about specific characteristics of the constructors, properties and methods.
These test failures do not affect the run-time behavior of the polyfill.
For example:
* The `name` property of down-leveled constructors is incorrect.
* The `length` property of down-leveled constructors and methods with optional arguments is incorrect.
* Not all properties and methods are correctly marked as non-enumerable.
* Down-leveled class methods are not correctly marked as non-constructable.

The type definitions are compatible with the built-in stream types of TypeScript 3.3.
The type definitions are compatible with the built-in stream types of TypeScript 3.3 and higher.

### Contributors
## Contributors

Thanks to these people for their work on [the original polyfill][creatorrr-polyfill]:

Expand All @@ -98,6 +107,8 @@ Thanks to these people for their work on [the original polyfill][creatorrr-polyf
[spec]: https://streams.spec.whatwg.org
[ref-impl]: https://github.com/whatwg/streams
[ponyfill]: https://github.com/sindresorhus/ponyfill
[amd]: https://requirejs.org/docs/whyamd.html
[migrating]: https://github.com/MattiasBuelens/web-streams-polyfill/blob/next/MIGRATING.md
[promise-support]: https://kangax.github.io/compat-table/es6/#test-Promise
[promise-polyfill]: https://www.npmjs.com/package/promise-polyfill
[rs-asynciterator]: https://streams.spec.whatwg.org/#rs-asynciterator
Expand All @@ -110,5 +121,5 @@ Thanks to these people for their work on [the original polyfill][creatorrr-polyf
[ref-impl-transferarraybuffer]: https://github.com/whatwg/streams/blob/cada8129edcc4803b2878a7a3f5e1d8325dc0c23/reference-implementation/lib/abstract-ops/ecmascript.js#L16
[issue-3]: https://github.com/MattiasBuelens/web-streams-polyfill/issues/3
[wpt-async-iterator-prototype]: https://github.com/web-platform-tests/wpt/blob/87a4c80598aee5178c385628174f1832f5a28ad6/streams/readable-streams/async-iterator.any.js#L24
[stub-async-iterator-prototype]: https://github.com/MattiasBuelens/web-streams-polyfill/blob/v2.0.0/src/target/es5/stub/async-iterator-prototype.ts
[stub-async-iterator-prototype]: https://github.com/MattiasBuelens/web-streams-polyfill/blob/next/src/lib/readable-stream/async-iterator.ts#L153-L161
[creatorrr-polyfill]: https://github.com/creatorrr/web-streams-polyfill
4 changes: 2 additions & 2 deletions api-extractor.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
*
* SUPPORTED TOKENS: <projectFolder>, <packageName>, <unscopedPackageName>
*/
"mainEntryPointFilePath": "<projectFolder>/lib/polyfill.d.ts",
"mainEntryPointFilePath": "<projectFolder>/lib/ponyfill.d.ts",

/**
* A list of NPM package names whose exports should be treated as part of this package.
Expand Down Expand Up @@ -195,7 +195,7 @@
* SUPPORTED TOKENS: <projectFolder>, <packageName>, <unscopedPackageName>
* DEFAULT VALUE: "<projectFolder>/dist/<unscopedPackageName>.d.ts"
*/
"untrimmedFilePath": "<projectFolder>/dist/types/polyfill.d.ts",
"untrimmedFilePath": "<projectFolder>/types/ponyfill.d.ts",

/**
* Specifies the output path for a .d.ts rollup file to be generated with trimming for a "beta" release.
Expand Down
71 changes: 0 additions & 71 deletions build/downlevel-dts.js

This file was deleted.

7 changes: 0 additions & 7 deletions es2018/package.json

This file was deleted.

7 changes: 0 additions & 7 deletions es6/package.json

This file was deleted.

10 changes: 6 additions & 4 deletions etc/web-streams-polyfill.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
```ts

/// <reference lib="es2018.asynciterable" />

// @public
export interface AbortSignal {
readonly aborted: boolean;
Expand All @@ -28,7 +30,6 @@ export class CountQueuingStrategy implements QueuingStrategy<any> {
// @public
export interface QueuingStrategy<T = any> {
highWaterMark?: number;
// Warning: (ae-forgotten-export) The symbol "QueuingStrategySizeCallback" needs to be exported by the entry point polyfill.d.ts
size?: QueuingStrategySizeCallback<T>;
}

Expand All @@ -37,6 +38,9 @@ export interface QueuingStrategyInit {
highWaterMark: number;
}

// @public
export type QueuingStrategySizeCallback<T = any> = (chunk: T) => number;

// @public
export class ReadableByteStreamController {
get byobRequest(): ReadableStreamBYOBRequest | null;
Expand Down Expand Up @@ -79,8 +83,7 @@ export interface ReadableStreamAsyncIterator<R> extends AsyncIterator<R> {

// @public
export class ReadableStreamBYOBReader {
// Warning: (ae-forgotten-export) The symbol "ReadableByteStream" needs to be exported by the entry point polyfill.d.ts
constructor(stream: ReadableByteStream);
constructor(stream: ReadableStream<Uint8Array>);
cancel(reason?: any): Promise<void>;
get closed(): Promise<undefined>;
read<T extends ArrayBufferView>(view: T): Promise<ReadableStreamBYOBReadResult<T>>;
Expand Down Expand Up @@ -269,7 +272,6 @@ export class WritableStreamDefaultWriter<W = any> {
write(chunk: W): Promise<void>;
}


// (No @packageDocumentation comment for this package)

```
Loading

0 comments on commit 2c9c5f3

Please sign in to comment.