0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00
astro/packages/webapi
2023-04-19 10:59:09 +02:00
..
run Remove more unnecessary polyfills from webapi for Node 16 (#5814) 2023-01-10 15:29:06 +01:00
src chore: use directive @ts-expect-error instead of @ts-ignore (#6429) 2023-03-07 13:41:24 +00:00
test Cleanup CI logs and run windows on node16 only (#6838) 2023-04-19 10:59:09 +02: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 (#6432) 2023-03-07 14:49:19 -05:00
LICENSE Use undici's FormData for polyfilling (#6413) 2023-03-03 14:47:24 +01:00
mod.d.ts docs(images): Add docs for image related errors (#6463) 2023-03-08 17:48:33 +01:00
package.json feat(all): Migrate to TypeScript 5.0 (#6579) 2023-03-20 17:02:07 +01:00
README.md Replace tabs with double spaces (#6562) 2023-03-16 15:46:31 +02:00
tsconfig.json Update compilation target for Node 16 (#6213) 2023-03-06 13:57:16 -05: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.