@11ty/eleventy-plugin-bundle

中文文档
4.0.3发布于 2026/09/18MITES Module构建来源已验证近 30 天下载 70.1万

Little bundles of code, little bundles of joy.

原始使用文档
文档目录12 节
  1. Why?
  2. Installation
  3. Usage
  4. Full options list
  5. Universal Shortcodes
  6. Example: Add bundle code in a Markdown file in Eleventy
  7. Render bundle code
  8. Write a bundle to a file
  9. Asset bucketing
  10. Examples
  11. Advanced
  12. Limitations

eleventy-plugin-bundle

Little bundles of code, little bundles of joy.

Create minimal per-page or app-level bundles of CSS, JavaScript, or HTML to be included in your Eleventy project.

Makes it easy to implement Critical CSS, in-use-only CSS/JS bundles, SVG icon libraries, or secondary HTML content to load via XHR.

Why?

This project is a minimum-viable-bundler and asset pipeline in Eleventy. It does not perform any transpilation or code manipulation (by default). The code you put in is the code you get out (with configurable transforms if you’d like to modify the code).

For more larger, more complex use cases you may want to use a more full featured bundler like Vite, Parcel, Webpack, rollup, esbuild, or others.

But do note that a full-featured bundler has a significant build performance cost, so take care to weigh the cost of using that style of bundler against whether or not this plugin has sufficient functionality for your use case—especially as the platform matures and we see diminishing returns on code transpilation (ES modules everywhere).

Installation

No installation necessary. Starting with Eleventy core v3.0.0 and newer, this plugin is now bundled with Eleventy.

Usage

By default, Bundle Plugin v2.0 does not include any default bundles. You must add these yourself via eleventyConfig.addBundle.

[!NOTE] One notable exception happens when using the WebC Eleventy Plugin, which adds css, js, and html bundles for you.

To create a bundle type, use eleventyConfig.addBundle in your Eleventy configuration file (default eleventy.config.js):

// eleventy.config.js
export default function(eleventyConfig) {
    eleventyConfig.addBundle("css");
};

This does two things:

  1. Creates a new css shortcode for adding arbitrary code to this bundle
  2. Adds "css" as an eligible type argument to the getBundle and getBundleFileUrl shortcodes.

Full options list

export default function(eleventyConfig) {
    eleventyConfig.addBundle("css", {
        // (Optional) Folder (relative to output directory) files will write to
        toFileDirectory: "bundle",

        // (Optional) File extension used for bundle file output, defaults to bundle name
        outputFileExtension: "css",

        // (Optional) Name of shortcode for use in templates, defaults to bundle name
        shortcodeName: "css",
        // shortcodeName: false, // disable this feature.

        // (Optional) Modify bundle content
        transforms: [],

        // (Optional) If two identical code blocks exist in non-default buckets, they’ll be hoisted to the first bucket in common.
        hoist: true,

        // (Optional) In 11ty.js templates, having a named export of `bundle` will populate your bundles.
        bundleExportKey: "bundle",
        // bundleExportKey: false, // disable this feature.
    });
};

Read more about hoist and duplicate bundle hoisting.

Universal Shortcodes

The following Universal Shortcodes (available in njk, liquid, hbs, 11ty.js, and webc) are provided by this plugin:

  • getBundle to retrieve bundled code as a string.
  • getBundleFileUrl to create a bundle file on disk and retrieve the URL to that file.

Here’s a real-world commit showing this in use on the eleventy-base-blog project.

Example: Add bundle code in a Markdown file in Eleventy

# My Blog Post

This is some content, I am writing markup.

{% css %}
em { font-style: italic; }
{% endcss %}

## More Markdown

{% css %}
strong { font-weight: bold; }
{% endcss %}

Renders to:

<h1>My Blog Post</h1>

<p>This is some content, I am writing markup.</p>

<h2>More Markdown</h2>

Note that the bundled code is excluded!

There are a few more examples below!

Render bundle code

<!-- Use this *anywhere*: a layout file, content template, etc -->
<style>{% getBundle "css" %}</style>

<!--
You can add more code to the bundle after calling
getBundle and it will be included.
-->
{% css %}* { color: orange; }{% endcss %}

Write a bundle to a file

Writes the bundle content to a content-hashed file location in your output directory and returns the URL to the file for use like this:

<link rel="stylesheet" href="{% getBundleFileUrl "css" %}">

NOTE: Writing bundles to files will likely be slower for empty-cache first time visitors but better cached in the browser for repeat-views (and across multiple pages, too).

Asset bucketing

<!-- This goes into a `defer` bucket (the bucket can be any string value) -->
{% css "defer" %}em { font-style: italic; }{% endcss %}
<!-- Pass the arbitrary `defer` bucket name as an additional argument -->
<style>{% getBundle "css", "defer" %}</style>
<link rel="stylesheet" href="{% getBundleFileUrl 'css', 'defer' %}">

A default bucket is implied:

<!-- These two statements are the same -->
{% css %}em { font-style: italic; }{% endcss %}
{% css "default" %}em { font-style: italic; }{% endcss %}

<!-- These two are the same too -->
<style>{% getBundle "css" %}</style>
<style>{% getBundle "css", "default" %}</style>

Examples

Critical CSS

// eleventy.config.js
export default function(eleventyConfig) {
    eleventyConfig.addBundle("css");
};

Use asset bucketing to divide CSS between the default bucket and a defer bucket, loaded asynchronously.

(Note that some HTML boilerplate has been omitted from the sample below)

<!-- … -->
<head>
    <!-- Inlined critical styles -->
    <style>{% getBundle "css" %}</style>

    <!-- Deferred non-critical styles -->
    <link rel="stylesheet" href="{% getBundleFileUrl 'css', 'defer' %}" media="print" onload="this.media='all'">
    <noscript>
        <link rel="stylesheet" href="{% getBundleFileUrl 'css', 'defer' %}">
    </noscript>
</head>
<body>
    <!-- This goes into a `default` bucket -->
    {% css %}/* Inline in the head, great with @font-face! */{% endcss %}
    <!-- This goes into a `defer` bucket (the bucket can be any string value) -->
    {% css "defer" %}/* Load me later */{% endcss %}
</body>
<!-- … -->

Related:

SVG Icon Library

Here an svg is bundle is created.

// eleventy.config.js
export default function(eleventyConfig) {
    eleventyConfig.addBundle("svg");
};
<svg width="0" height="0" aria-hidden="true" style="position: absolute;">
    <defs>{% getBundle "svg" %}</defs>
</svg>

<!-- And anywhere on your page you can add icons to the set -->
{% svg %}
<g id="icon-close"><path d="…" /></g>
{% endsvg %}

And now you can use `icon-close` in as many SVG instances as you’d like (without repeating the heftier SVG content).

<svg><use xlink:href="#icon-close"></use></svg>
<svg><use xlink:href="#icon-close"></use></svg>
<svg><use xlink:href="#icon-close"></use></svg>
<svg><use xlink:href="#icon-close"></use></svg>

React Helmet-style <head> additions

// eleventy.config.js
export default function(eleventyConfig) {
    eleventyConfig.addBundle("html");
};

This might exist in an Eleventy layout file:

<head>
    {% getBundle "html", "head" %}
</head>

And then in your content you might want to page-specific preconnect:

{% html "head" %}
<link href="https://v1.opengraph.11ty.dev" rel="preconnect" crossorigin>
{% endhtml %}

Bundle Sass with the Render Plugin

You can render template syntax inside of the {% css %} shortcode too, if you’d like to do more advanced things using Eleventy template types.

This example assumes you have added the Render plugin and the scss custom template type to your Eleventy configuration file.

{% css %}
  {% renderTemplate "scss" %}
  h1 { .test { color: red; } }
  {% endrenderTemplate %}
{% endcss %}

Now the compiled Sass is available in your default bundle and will show up in getBundle and getBundleFileUrl.

Use with WebC

Starting with @11ty/[email protected] (track at issue #48) this plugin is used by default in the Eleventy WebC plugin. Specifically, WebC Bundler Mode now uses the bundle plugin under the hood.

To add CSS to a bundle in WebC, you would use a <style> element in a WebC page or component:

<style>/* This is bundled. */</style>
<style webc:keep>/* Do not bundle me—leave as is */</style>

To add JS to a page bundle in WebC, you would use a <script> element in a WebC page or component:

<script>/* This is bundled. */</script>
<script webc:keep>/* Do not bundle me—leave as is */</script>
  • Existing calls via WebC helpers getCss or getJs (e.g. <style @raw="getCss(page.url)">) have been wired up to getBundle (for "css" and "js" respectively) automatically.
    • For consistency, you may prefer using the bundle plugin method names everywhere: <style @raw="getBundle('css')"> and <script @raw="getBundle('js')"> both work fine.
  • Outside of WebC, the Universal Filters webcGetCss and webcGetJs were removed in Eleventy v3.0.0-alpha.10 in favor of the getBundle Universal Shortcode ({% getBundle "css" %} and {% getBundle "js" %} respectively).

Modify the bundle output

You can wire up your own async-friendly callbacks to transform the bundle output too. Here’s a quick example of postcss integration.

const postcss = require("postcss");
const postcssNested = require("postcss-nested");

export default function(eleventyConfig) {
    eleventyConfig.addBundle("css", {
        transforms: [
            async function(content) {
                // this.type returns the bundle name.
                // Same as Eleventy transforms, this.page is available here.
                let result = await postcss([postcssNested]).process(content, { from: this.page.inputPath, to: null });
                return result.css;
            }
        ]
    });
};

Advanced

Limitations

Bundles do not support nesting or recursion (yet?). If this will be useful to you, please file an issue!

版本与发布记录

@11ty/eleventy-plugin-bundle 共发布 23 个版本,首次发布于 2023/02/03;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.0.3latest2026/09/181 个月前
4.0.22026/07/024 个月前
4.0.12026/07/014 个月前
4.0.02026/07/014 个月前
3.0.72025/08/231.2 年前
3.0.62025/04/251.5 年前

其他发布通道:beta → 3.0.1-beta.2。

关于 @11ty/eleventy-plugin-bundle 的常见问题

@11ty/eleventy-plugin-bundle 是什么?

Little bundles of code, little bundles of joy.

如何安装 @11ty/eleventy-plugin-bundle?

在项目目录运行 npm install @11ty/eleventy-plugin-bundle,或使用 pnpm add / yarn add / bun add。

@11ty/eleventy-plugin-bundle 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/11ty__eleventy-plugin-bundle 是否存在。

@11ty/eleventy-plugin-bundle 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@11ty/eleventy-plugin-bundle 需要什么版本的 Node.js?

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

@11ty/eleventy-plugin-bundle 可以商用吗?

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

@11ty/eleventy-plugin-bundle 的下载量有多少?

根据 npm 官方下载统计,@11ty/eleventy-plugin-bundle 在 2026/10/04 – 2026/11/02 这 30 天内被下载 701,290 次,日均约 23,376 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@11ty/eleventy-plugin-bundle 还在维护吗?

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