@web-ts-toolkit/asset-inliner
Generic ESM-only asset inliner for CSS and HTML — Base64 data URL encoding, CSS url() / font format() formatting, deterministic catalog and file pipeline. Node >=22, named imports only.
This page mirrors the installed package
README.md(the authoritative consumer guide). The shipped declarations underdist/index.d.mtsplusREADME.mdare the primary installed-consumer docs; website docs are secondary.
Install
pnpm add @web-ts-toolkit/asset-inliner
ESM only with import-only export map (dist/index.mjs + dist/index.d.mts). require() is not supported. See package README.md for the full quickstart, detection modes, limits, supported built-ins, custom definitions, resolver hook, and error reference.
Shortest examples
import { encodeAsset, formatCssUrl } from '@web-ts-toolkit/asset-inliner';
const asset = await encodeAsset('./assets/logo.png');
formatCssUrl(asset); // url(data:image/png;base64,...)
import { createAssetCatalog, inlineCss } from '@web-ts-toolkit/asset-inliner';
const catalog = await createAssetCatalog(['./assets']);
const result = inlineCss('a { background: url("./assets/logo.png") }', { catalog, documentPath: '/project/src/a.css' });
import { inlineFiles } from '@web-ts-toolkit/asset-inliner';
await inlineFiles({ assets: ['./assets'], targets: ['./styles'] }); // dry-run; add write:true to persist
Notes
- Registry reuse: pass an already-validated
AssetDefinitionRegistryvia{ registry }tocreateAssetCatalog,discoverAssets, orencodeAssetto avoid re-normalizingdefinitions. - Literal unions:
AssetInlinerErrorCode('RESOURCE_LIMIT'etc.) andDiagnosticCode('UNRESOLVED_REFERENCE'etc.) narrow in consumers; subclasses likeResourceLimitErrorcarrycode: 'RESOURCE_LIMIT' as const. - sourcePath:
EncodedAsset.sourcePathis a normalized absolute path (path.resolve) when input was a file path. - Definition shape:
AssetTypeDefinitionis a discriminated union —fontFormatonly allowed whenkind === 'font'(checked at type and runtime). - Changed HTML:
inlineHtmlprefers source-location patches of the targeted attribute value ranges so unrelated markup stays byte-identical; if a patch is invalid/overlapping it falls back to full serialization (may normalize). - Embedded CSS:
inlineEmbeddedCss: true(opt-in, defaultfalse) inlines localurl(...)inside<style>elements andstyleattributes using the same CSS semantics asinlineCss, with shared limits, source-offset location mapping, and aPARSE_ERRORdiagnostic (no corruption) for malformed chunks. - Selective inlining:
InlineOptions/InlineFilesOptionsacceptmaxInlineBytes(byteLength threshold) and/orshouldInline(asset, url) => booleanto leave large or predicate-rejected assets as external references with anINLINE_SKIPPED(warn) diagnostic; hard limits (maxAssetBytes/maxTotalBytes) remain fail-closed (ResourceLimitError) and cannot be downgraded, with deterministic order and no implicit heuristics.
Migration note
Legacy node-font2base64 and base64-injector both exposed encodeToDataSrc with conflicting semantics and unsafe defaults. The new package splits them into encodeAsset (data URL only) + formatCssUrl (generic) / formatFontSource (font, requires fontFormat), makes file writes opt-in, skips remote/data: URLs before I/O, and reports ambiguity as AmbiguousAssetError instead of picking a winner. The package README.md contains the complete migration matrices for both legacies, the intentional breaking changes, CSP/caching and SVG non-sanitization caveats, and MIT provenance/license notices for dependencies (file-type, postcss, postcss-value-parser, parse5) and fixtures.