Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
How to use a method multiple times in one?

Objective:

So I'm using Eleventy as an SSG and want to create multiple folders and copy some files into these folders, before Eleventy is doing the building or site generation.

A JSON file is for the array of site titles (folder names).

And I chose to make use the fs-extra module instead of the Node.js fs module.

Inside .eleventy.js (config file):

    const fse = require('fs-extra');
    const arrayFromJSON = require('./_data/sites.json');
    const arrayTitles = arrayFromJSON.titles;
    
      eleventyConfig.on("beforeBuild", () => {
  // Create site folders and copy layout and frontmatter data to the folders
    arrayTitles.forEach(element => 
      fse.copySync(`sitesource/index.njk`, `src/_sites/${element}/${element}.njk`, { recursive: true }));
    arrayTitles.forEach(element => 
      fse.copySync(`sitesource/fm.11tydata.js`, `src/_sites/${element}/${element}.11tydata.js`, { recursive: true }));
  });

I've been looking around for ideas and examples to combine these two actions into one, where it's only needed once to call arrayTitles.forEach. But I haven't found it.

Is this possible, and anyone with suggestions to improve this code?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I may be confused, but why not simply use one forEach?

eleventyConfig.on("beforeBuild", () => {
    // Create site folders and copy layout and frontmatter data to the folders
    arrayTitles.forEach(element => 
      fse.copySync(`sitesource/index.njk`, `src/_sites/${element}/${element}.njk`, { recursive: true });
      fse.copySync(`sitesource/fm.11tydata.js`, `src/_sites/${element}/${element}.11tydata.js`, { recursive: true });
    );
  });
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda