Table of Contents
assets-esbuild
Note: The options API is still evolving; expect tweaks as the project grows.
What it does
- Adds
jsas a template format and processes anyindex.jsunder the resolved assetsjsdirectory (from Eleventydir.assets). - Bundles with esbuild (default target
es2020, minify on) and outputs JS. - Skips
11tydata.jsfiles and other JS outside the assets entry. - Overrides the default
allcollection to exclude11tydata.js. - Registers a filter
inlineESbuildto inline and bundle arbitrary JS files.
Defaults
- Entries: any
index.jsfile under<input>/<assets>/js/(including nested folders). - Bundle/minify with esbuild, target
es2020. - Options:
minify(defaulttrue) andtarget(default"es2020"); pass them via BaselineassetsESBuildoptions or directly to the plugin. - Peer deps:
esbuild(already included in this starter). - Output: under
dist/assets/js/(matching Eleventydir.assetsanddir.output).
Options
minify: forward to esbuild’sminifyflag.target: forward to esbuild’starget.inlineESbuildfilter also accepts an optional options object per call (merged with defaults).
Example:
import baseline, { config as baselineConfig } from '@apleasantview/eleventy-plugin-baseline';
/** @param {import("@11ty/eleventy").UserConfig} eleventyConfig */
export default async function (eleventyConfig) {
eleventyConfig.addPlugin(
baseline({
assetsESBuild: {
minify: process.env.ELEVENTY_ENV === 'production',
target: 'es2017'
}
})
);
}
export const config = baselineConfig;
How it works
- Resolves the assets directory from Eleventy
dirand buildsjsDirfrom it. - Registers
jsformat and an extension handler that runs forindex.jsunder the resolved assetsjsdirectory. - Runs esbuild with
{ bundle: true, write: false }plus the configuredminifyandtarget, returns compiled text. - Replaces the default
allcollection with one that filters out11tydata.js.
Inlining a bundle
- Any JS file can be inlined with
inlineESbuild; the filter bundles the exact file you pass (not limited to assets dir). - In a template or Markdown file, build the path (e.g., from
_baseline.assets.input) and pipe it throughinlineESbuild. - In Markdown, keep the call unindented and on its own lines so the
<script>is treated as raw HTML.
Example:
{% set jsPath = _baseline.assets.input ~ "js/inline-example.js" %}
{{ jsPath | inlineESbuild({ minify: false, target: "es2017" }) | safe }}
Tips
- Any nested
index.jsunder the assetsjsdirectory will be bundled; keep only the entries you intend to ship. - Pair with
inlineESbuildfilter if you want to inline the bundled output in templates. - See the “Assets Pipeline” tutorial for an end-to-end example.
- Tutorial: Assets Pipeline Quickstart
Previous: assets-core
Next: assets-postcss