Eleventy: addPassthroughCopy can't copy same source folder into several target directories

Created on 11 Feb 2020  Β·  5Comments  Β·  Source: 11ty/eleventy

I'm trying to create multi-language webpage.
I need to copy /assets into every language folder:

eleventy.js

module.exports = function(eleventyConfig) {
  eleventyConfig.addPassthroughCopy({ assets: "en" });
  eleventyConfig.addPassthroughCopy({ assets: "fr" });
};

Project structure:

  _includes
  assets
  en
  fr

But /assets copied only into /fr language.

Any ideas?

enhancement needs-votes

Most helpful comment

I’d imagine that syntax would probably be something like:

eleventyConfig.addPassthroughCopy({ "src/assets": ["en", "de", "fr"] });

All 5 comments

Interesting. It looks like addPassthroughCopy() is proxying an internal passthroughCopies property which is a simple object, so I'm guessing that using the same key ("assets") is just overwriting the existing value in the passthroughCopies object.

I wonder if you'd have to copy the assets yourself using a 3rd party lib like copy, or cp, or fs-extra.

https://github.com/11ty/eleventy/blob/0d18f4d1ca4c0587c1ebba298a9603ca11429243/src/UserConfig.js#L299-L316

https://github.com/11ty/eleventy/blob/0d18f4d1ca4c0587c1ebba298a9603ca11429243/src/UserConfig.js#L44

Not the prettiest solution, but I liked fs-extra's API the best:

const path = require("path");

const fs = require("fs-extra");

module.exports = eleventyConfig => {
  /*
  // The src/assets files are only copied to the "de/" (last) folder.
  eleventyConfig.addPassthroughCopy({ "src/assets": "en" });
  eleventyConfig.addPassthroughCopy({ "src/assets": "fr" });
  eleventyConfig.addPassthroughCopy({ "src/assets": "de" });
  */

  const dir = {
    input: "src",
    output: "www"
  };

  const locales = ["en", "fr", "de"];
  for (const locale of locales) {
    try {
      fs.copySync(
        // src: "./src/assets/"
        path.join(dir.input, "assets"),
        // dest: "./www/{{ locale }}/"
        path.join(dir.output, locale)
      );
    } catch (err) {
      console.error(err.message);
      process.exitCode = 1;
    }
  }

  return {
    dir
  };
};

INPUT

$ tree src

src/
β”œβ”€β”€ _data/
β”œβ”€β”€ _includes/
β”œβ”€β”€ assets/
β”‚Β Β  β”œβ”€β”€ site.css
β”‚Β Β  └── site.js
└── index.njk

3 directories, 3 files

OUTPUT

$ tree www
www/
β”œβ”€β”€ de/
β”‚Β Β  β”œβ”€β”€ site.css
β”‚Β Β  └── site.js
β”œβ”€β”€ en/
β”‚Β Β  β”œβ”€β”€ site.css
β”‚Β Β  └── site.js
β”œβ”€β”€ fr/
β”‚Β Β  β”œβ”€β”€ site.css
β”‚Β Β  └── site.js
└── index.html

3 directories, 7 files

I’d imagine that syntax would probably be something like:

eleventyConfig.addPassthroughCopy({ "src/assets": ["en", "de", "fr"] });

This repository is now using lodash style issue management for enhancements. This means enhancement issues will now be closed instead of leaving them open.

View the enhancement backlog here. Don’t forget to upvote the top comment with πŸ‘!

eleventyConfig.addPassthroughCopy({ "src/assets": ["en", "de", "fr"] });

This seems to fail with the following message:

> Having trouble copying

`TemplateWriterWriteError` was thrown
> Having trouble copying './src/assets'

`TemplatePassthroughManagerCopyError` was thrown
> The "path" argument must be of type string. Received type object

`TypeError` was thrown:
    TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object
Was this page helpful?
0 / 5 - 0 ratings