Eleventy — Use Passthrough File Copy to Add Assets

Published:

In Eleventy, static assets like CSS and JavaScript aren't automatically copied to the output directory as 11ty only processes template files (e.g., Nunjucks, Liquid, or Markdown) out-of-the-box.

Depending on your project setup, you may not need a bundler or build pipeline to process the website assets (e.g., CSS, JS, images, fonts, and videos) and want them copied in the Eleventy output folder.

For example, if you write native CSS rather than SCSS. Or, if you manually optimize images and fonts. In such cases, a simple copy and paste of assets from the input to the output directory is what's required.

Luckily, Eleventy comes with a feature that allows copying additional files and folders that are not Eleventy templates to the output directory — called Passthrough File Copy.

Let's take a look at it and how to use it in an 11ty project.

Eleventy — Use Passthrough File Copy to Add Assets

Eleventy Passthrough File Copy

Using Passthrough File Copy, you can specify additional files and folders that Eleventy should copy to the output folder each time the site is built.

To do so, specify the name of files or folders in the Eleventy config file (e.g., .eleventy.js or eleventy.config.js) using the addPassthroughCopy() method as mentioned below:

module.exports = function (eleventyConfig) {
  eleventyConfig.addPassthroughCopy('./assets');
};

Here, the "assets" folder (relative to the root project folder) is copied as-is to the 11ty output folder (_site by default) each time Eleventy is run.

Note that Passthrough File Copy looks for files and folders within the project root directory (not the Eleventy input directory).

Copying Other Content Type

In addition to static assets, you can also add another type of content, such as robots.txt or sitemap.xml files, document files like PDF, and more.

module.exports = function (eleventyConfig) {
  eleventyConfig.addPassthroughCopy('./docs/overview.pdf');
  eleventyConfig.addPassthroughCopy('./robots.txt');
  eleventyConfig.addPassthroughCopy('./sitemap.xml');
};

Copying Individual Assets — CSS, JS, Images, and More

For some reason, if you want to passthrough copy certain asset types, simply specify the file or folder path relative to the project root.

For instance, if you use webpack to process JavaScript files and want Eleventy to add CSS and images to the output directory, this is how you'd do it:

module.exports = function (eleventyConfig) {
  eleventyConfig.addPassthroughCopy('./assets/css');
  eleventyConfig.addPassthroughCopy('./assets/images');
};

The Configuration API options above only move the ~/assets/css and ~/assets/images folders to the output folder.

Wrapping Up

That's it. We looked at how to use the addPassthroughCopy() method to copy static assets in the output folder in an Eleventy project. ✨

In addition, we also saw how to passthrough copy other content files like PDFs. 📄

Let me know if you have any questions or feedback via email at [email protected]. 🙂

Contents

    Danial Zahid's headshot.

    About Danial Zahid

    I'm a self-taught web developer passionate about everything related to websites and web development. Over the past years, I've explored and worked with different web technologies and tools, notably WordPress, Eleventy, npm, and Gulp.js, to name a few.

    In addition, I've created and currently maintain several open-source projects, including web apps, widgets, websites, and more.

    When I'm not writing content for W3Things or doing web development work, I enjoy my free time by playing games or listening to podcasts.

    Get Essential Gulp Kit

    The ultimate Gulp.js solution to improve your developer experience.

    • Includes complete Gulp.js toolkit setup
    • Optimize website content and static assets
    • Continuous asset optimization during development phase
    • Optimize unlimited number of files in bulk
    • Plus many more awesome features

    Subscribe to the Newsletter

    This website uses cookies to optimize your site experience. By continuing to use the site, you are agreeing to its privacy policy and the use of cookies.