0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/webapi
2022-12-19 11:26:48 +08:00
..
run webapi: Rename misspelled inheritence to inheritance (#5634) 2022-12-19 11:26:48 +08:00
src webapi: Rename misspelled inheritence to inheritance (#5634) 2022-12-19 11:26:48 +08:00
test chore: webapi test now use chai (#3048) 2022-04-10 18:29:46 -07:00
.gitignore Move from yarn to pnpm (#2455) 2022-03-08 15:46:11 -06:00
apply.js Add @astrojs/webapi package (#2729) 2022-03-07 15:36:22 -06:00
CHANGELOG.md [ci] release (#5239) 2022-10-31 10:21:09 -04:00
LICENSE Add @astrojs/webapi package (#2729) 2022-03-07 15:36:22 -06:00
mod.d.ts Skip mod.d.ts in format:ci (#4235) 2022-08-10 22:05:07 +08:00
package.json [ci] release (#5239) 2022-10-31 10:21:09 -04:00
README.md Add a thank you to the original author of the original webapi package (#5246) 2022-10-31 13:24:12 -04:00
tsconfig.json [ci] yarn format 2022-03-07 21:37:50 +00:00

WebAPI

WebAPI lets you use Web APIs in Node v12, v14, and v16.

npm install @astrojs/webapi
import { polyfill } from '@astrojs/webapi'

polyfill(globalThis)

const t = new EventTarget()
const e = new CustomEvent('hello')

t.addEventListener('hello', console.log)

t.dispatchEvent(e) // logs `e` event from `t`

These APIs are combined from popular open source projects and configured to share implementation details. This allows their behavior to match browser expectations as well as reduce their combined memory footprint.

Features

Usage

You can use WebAPIs as individual exports.

import { AbortController, Blob, Event, EventTarget, File, fetch, Response } from '@astrojs/webapi'

You can apply WebAPIs to an object, like globalThis.

import { polyfill } from '@astrojs/webapi'

polyfill(globalThis)

Polyfill Options

The exclude option receives a list of WebAPIs to exclude from polyfilling.

polyfill(globalThis, {
	// disables polyfills for setTimeout clearTimeout
	exclude: 'setTimeout clearTimeout',
})

The exclude option accepts shorthands to exclude multiple polyfills. These shorthands end with the plus sign (+).

polyfill(globalThis, {
	// disables polyfills for setTimeout clearTimeout
	exclude: 'Timeout+',
})
polyfill(globalThis, {
	// disables polyfills for Node, Window, Document, HTMLElement, etc.
	exclude: 'Node+',
})
polyfill(globalThis, {
	// disables polyfills for Event, EventTarget, Node, Window, Document, HTMLElement, etc.
	exclude: 'Event+',
})
Shorthand Excludes
Blob+ Blob, File
Document+ Document, HTMLDocument
Element+ Element, and exclusions from HTMLElement+
Event+ Event, CustomEvent, EventTarget, AbortSignal, MediaQueryList, Window, and exclusions from Node+
EventTarget+ Event, CustomEvent, EventTarget, AbortSignal, MediaQueryList, Window, and exclusions from Node+
HTMLElement+ CustomElementsRegistry, HTMLElement, HTMLBodyElement, HTMLCanvasElement, HTMLDivElement, HTMLHeadElement, HTMLHtmlElement, HTMLImageElement, HTMLStyleElement, HTMLTemplateElement, HTMLUnknownElement, Image
Node+ Node, DocumentFragment, ShadowRoot, Document, HTMLDocument, and exclusions from Element+
StyleSheet+ StyleSheet, CSSStyleSheet

License & Attribution

Thank you to Jon Neal for his work on the original webapi project that this package is forked from. Licensed under the CC0-1.0 License.

Code from abort-controller is licensed under the MIT License (MIT), Copyright Toru Nagashima.

Code from event-target-shim is licensed under the MIT License (MIT), Copyright Toru Nagashima.

Code from fetch-blob is licensed under the MIT License (MIT), Copyright Jimmy Wärting.

Code from formdata-polyfill is licensed under the MIT License (MIT), Copyright Jimmy Wärting.

Code from node-fetch is licensed under the MIT License (MIT), Copyright Node Fetch Team.

Code from web-streams-polyfill is licensed under the MIT License (MIT), Copyright Mattias Buelens and Diwank Singh Tomer.