astro/packages/webapi
Houston (Bot) 73ca0ef383
[ci] release (#5948)
* [ci] release

* Update changelogs (#5955)

* [ci] release

* Wrap astro 2.0 beta logs in `<details>`

* Add link to docs upgrade guide

* First pass cleaning up 2.0 release notes

* mdx changes from Sarah

* combine 5584 and 5842 in deno, image, netlify

* markdown/remark incl (5684 & 5769) to match mdx

* Tweak markdown/remark formatting

* Format astro-prism

* Format astro-rss

* Format create-astro

* Format cloudflare

* Format lit

* Format partytown

* Format node

* Format preact

* Format react

* Format solid

* Format svelte

* Format tailwind

* Format vercel

* Format vue

* Format telemetry

* Format webapi

* Format scripts

* Reinstate h3s for headings

Co-authored-by: Ben Holmes <hey@bholmes.dev>

* Reformat mdx

* astro & markdown/remark: Combine #5679 & #5684 changelogs

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Co-authored-by: Ben Holmes <hey@bholmes.dev>

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Matthew Phillips <matthew@skypack.dev>
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Co-authored-by: Ben Holmes <hey@bholmes.dev>
2023-01-24 11:38:06 -05:00
..
run Remove more unnecessary polyfills from webapi for Node 16 (#5814) 2023-01-10 15:29:06 +01:00
src chore(webapi): Remove unused file (#5916) 2023-01-20 15:52:48 +01:00
test Remove more unnecessary polyfills from webapi for Node 16 (#5814) 2023-01-10 15:29:06 +01: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 (#5948) 2023-01-24 11:38:06 -05:00
LICENSE Remove more unnecessary polyfills from webapi for Node 16 (#5814) 2023-01-10 15:29:06 +01:00
mod.d.ts Remove more unnecessary polyfills from webapi for Node 16 (#5814) 2023-01-10 15:29:06 +01:00
package.json [ci] release (#5948) 2023-01-24 11:38:06 -05:00
README.md Remove more unnecessary polyfills from webapi for Node 16 (#5814) 2023-01-10 15:29:06 +01:00
tsconfig.json [ci] yarn format 2022-03-07 21:37:50 +00:00

WebAPI

WebAPI lets you use Web APIs not present in Node v16 and later.

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 { 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
Document+ Document, HTMLDocument
Element+ Element, and exclusions from HTMLElement+
Event+ Event, CustomEvent, EventTarget, MediaQueryList, Window, and exclusions from Node+
EventTarget+ Event, CustomEvent, EventTarget, 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 event-target-shim is licensed under the MIT License (MIT), Copyright Toru Nagashima.

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