d08ddaf5b9
* chore: `yarn` => `pnpm` * docs: `yarn` => `pnpm` * chore(ci): yarn => pnpm * chore(ci): update pnpm cache path * fix: add missing deps * fix: add missing deps * test: add package.json to all test fixtures * chore: improve hoisting behavior * chore: move turbo into package.json * chore: update npmrc * fix: add missing `debug` dependency * chore: remove prepare script * test: fix new tests * fix: fully resolve renderer paths and `astro/internal` path * chore: update lockfile * chore: remove log * fix: resolve renderers in vite-plugin-jsx * fix: prefer public-hoist-pattern to shamefully-hoist * chore: ignore @babel/core peer warning * chore: update dependencies * test: add autoprefixer as explicit dep * chore: update `.npmrc` file in examples * chore: update dependencies * fix: resolve renderer dependencies in static build * fix: static build renderer resolution * chore: fix smoke tests * chore: hoist autoprefixer * chore: update lockfile * attempt: use full file:// path on Windows * attempt: use astro/internal * attempt: optimize astro/internal * attempt: expose ./internal.js * chore: add missing package.json files * attempt: resolve astro/internal path * chore: tidy package.json * chore: update lockfile * chore: update deps * chore: update deps * chore: yarn -> pnpm * attempt: explicit /@fs urls * attempt: explicit /@fs urls * chore: update all examples for pnpm * chore: fix hoisting for with-vite-plugin-pwa * chore(ci): fix sharp install * chore: update with-vite-plugin-pwa example * fix: pin vite-plugin-pwa to 0.11.11 * fix: add workbox-window to vite-plugin-pwa deps * refactor: use pnpm update --recursive Co-authored-by: JuanM04 <me@juanm04.com> * chore: yarn => pnpm * chore: yarn => pnpm * fix: update smoke test to skip examples which don't work in static build * update lockfile * chore: update .npmrc files * chore: update lockfile * fix: smoke script * chore: update .npmrc file * fix: return to shamefully-hoist (shamefully) * chore: update lockfile * fix(smoke): ignore scripts for smoke tests * fix: update example to disable renderers * chore: bump version * chore(ci): fix smoke tests * attempt: disable --frozen-lockfile for smoke tests * chore: update smoke test * chore: fix rebase issue * chore: update lockfile * fix: smoke tests * fix(ci): run external smoke tests first * fix(ci): run syntax * chore: update lockfile * fix(ci): ensure submodules are up-to-date * fix(ci): ensure submodules are up-to-date * chore: update lockfile * chore: update for webapi * chore: silence node:* warnings * chore: update deps * fix(ci): persist generated webapi assets * fix(ci): webapi build script * chore(ci): remove custom node caching * chore: keep turbo.json * chore: update turbo, ignore create-astro * chore: update deps * fix(ci): test command * chore(ci): update test script Co-authored-by: JuanM04 <me@juanm04.com> |
||
---|---|---|
.. | ||
run | ||
src | ||
test | ||
.gitignore | ||
apply.js | ||
LICENSE | ||
mod.d.ts | ||
package.json | ||
README.md | ||
tsconfig.json |
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
- AbortController
- AbortSignal
- Blob
- ByteLengthQueuingStrategy
- CanvasRenderingContext2D
- CSSStyleSheet
- CountQueuingStrategy
- CustomElementRegistry
- CustomEvent
- DOMException
- Document
- DocumentFragment
- Element
- Event
- EventTarget
- File
- FormData
- HTMLDocument
- HTMLElement
- HTMLBodyElement
- HTMLCanvasElement
- HTMLDivElement
- HTMLHeadElement
- HTMLHtmlElement
- HTMLImageElement
- HTMLSpanElement
- HTMLStyleElement
- HTMLTemplateElement
- HTMLUnknownElement
- Headers
- IntersectionObserver
- Image
- ImageData
- MediaQueryList
- MutationObserver
- Node
- NodeIterator
- OffscreenCanvas
- ReadableByteStreamController
- ReadableStream
- ReadableStreamBYOBReader
- ReadableStreamBYOBRequest
- ReadableStreamDefaultController
- ReadableStreamDefaultReader
- Request
- Response
- ShadowRoot
- Storage
- StyleSheet
- TransformStream
- TreeWalker
- WritableStream
- WritableStreamDefaultController
- WritableStreamDefaultWriter
- Window
- atob
- btoa
- cancelAnimationFrame
- cancelIdleCallback
- clearTimeout
- fetch
- localStorage
- requestAnimationFrame
- requestIdleCallback
- setTimeout
- structuredClone
- Object.hasOwn
- Promise.any
- Array.prototype.at
- String.prototype.at
- String.prototype.replaceAll
- TypedArray.prototype.at
- URLPattern
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
Code original to this project is 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.