@css-inline/css-inline-wasm

中文文档
0.23.2发布于 2026/10/27MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 11.4万

High-performance library for inlining CSS into HTML 'style' attributes

原始使用文档
文档目录8 节
  1. @css-inline/css-inline-wasm
  2. Installation
  3. Usage
  4. API
  5. Playground
  6. Remote stylesheets
  7. Restrictions
  8. License

css-inline

@css-inline/css-inline-wasm

build status npm codecov.io gitter

css-inline inlines CSS into HTML style attributes. This package is its WebAssembly build for browsers and bundlers.

Installation

npm i @css-inline/css-inline-wasm

The package ships TypeScript types (index.d.ts).

Usage

With a bundler or ESM:

import { initWasm, inline, inlineFragment } from "@css-inline/css-inline-wasm";

// `initWasm` is async and runs once: wait for it before calling `inline`
await initWasm(fetch("https://unpkg.com/@css-inline/[email protected]/index_bg.wasm"));

const html = inline("<html><head><style>h1 { color:blue; }</style></head><body><h1>Big Text</h1></body></html>");
// <html><head></head><body><h1 style="color: blue;">Big Text</h1></body></html>

const fragment = inlineFragment("<h1>Big Text</h1>", "h1 { color: red; }");
// <h1 style="color: red;">Big Text</h1>

In Node.js, pass the file contents:

import { readFile } from "node:fs/promises";
import { initWasm, inline } from "@css-inline/css-inline-wasm";

await initWasm(readFile(new URL(import.meta.resolve("@css-inline/css-inline-wasm/index_bg.wasm"))));

From a CDN, the script defines a global cssInline. Pin the version in both URLs to the release you want; unpinned URLs load the latest:

<iframe id="output"></iframe>
<script src="https://unpkg.com/@css-inline/[email protected]"></script>
<script>
  cssInline.initWasm(fetch("https://unpkg.com/@css-inline/[email protected]/index_bg.wasm")).then(() => {
    const inlinedHtml = cssInline.inline("<html><head><style>h1 { color:blue; }</style></head><body><h1>Big Text</h1></body></html>");
    document.getElementById("output").srcdoc = inlinedHtml;
  });
</script>

API

  • initWasm(moduleOrPath): load the .wasm module. Accepts a URL, Response, BufferSource, WebAssembly.Module or a promise of one.
  • inline(html, options?)
  • inlineFragment(html, css, options?): css is a CSS string.
  • version()

Options match the Node.js package except cache, which WASM lacks: inlineStyleTags, keepStyleTags, keepLinkTags, keepAtRules, minifyCss, baseUrl, loadRemoteStylesheets, extraCss, preallocateNodeCapacity, removeInlinedSelectors, applyWidthAttributes, applyHeightAttributes, resolveCssVariables.

Playground

Try css-inline in the WebAssembly-powered playground.

Remote stylesheets

inlineAsync and inlineFragmentAsync return a Promise and load remote link stylesheets with fetch. In browsers, the server hosting a stylesheet must allow cross-origin requests (CORS).

const inlined = await inlineAsync(html, { baseUrl: "https://example.com/" });

Restrictions

The WASM module cannot read the filesystem, and the synchronous inline and inlineFragment cannot load any link stylesheet. They throw a string (not an Error):

  • Remote URL: Loading remote stylesheets is not supported on WASM: <url>. Use inlineAsync instead.
  • file:// or a relative href without an http(s) baseUrl: Loading local files is not supported on WASM: <url>

inlineAsync and inlineFragmentAsync throw the same local file error, and <error>: <url> when a fetch fails.

To skip link tags, either:

  • pass loadRemoteStylesheets: false to skip every link tag, remote and local, or
  • add data-css-inline="ignore" to the link tag.

Add the skipped CSS through extraCss if you need it.

Other errors are strings too, e.g. an invalid baseUrl throws relative URL without a base: <value>.

License

Licensed under the MIT license.

版本与发布记录

@css-inline/css-inline-wasm 共发布 27 个版本,首次发布于 2023/12/29;过去 12 个月发布了 14 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.23.2latest2026/10/2714 天前
0.23.12026/10/1724 天前
0.23.02026/10/1725 天前
0.22.12026/10/1427 天前
0.22.02026/10/021 个月前
0.21.22026/08/242 个月前

关于 @css-inline/css-inline-wasm 的常见问题

@css-inline/css-inline-wasm 是什么?

High-performance library for inlining CSS into HTML 'style' attributes

如何安装 @css-inline/css-inline-wasm?

在项目目录运行 npm install @css-inline/css-inline-wasm,或使用 pnpm add / yarn add / bun add。

@css-inline/css-inline-wasm 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@css-inline/css-inline-wasm 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@css-inline/css-inline-wasm 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 10。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@css-inline/css-inline-wasm 可以商用吗?

@css-inline/css-inline-wasm 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@css-inline/css-inline-wasm 的下载量有多少?

根据 npm 官方下载统计,@css-inline/css-inline-wasm 在 2026/10/11 – 2026/11/09 这 30 天内被下载 113,712 次,日均约 3,790 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@css-inline/css-inline-wasm 还在维护吗?

最新版本 0.23.2 发布于 2026/10/27(14 天前);至今共发布 27 个版本,其中过去 12 个月发布了 14 个;首次发布于 2023/12/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。