R
R
ronny2021-09-01 06:53:08
gulp.js
ronny, 2021-09-01 06:53:08

gulp loop only takes last value?

There is this gulp code with a loop

`exports.default = compileSaveTool`

function compileSaveTool (cb) {
  ...
  for (const tool in allCodes) {
    for (const code of allCodes[tool]) {
      const lang = code.length === 2 ? code : code.slice(0, -3)
      compile(allASaveTools, code, lang, allCodes, tool)
    }
  }
  cb()
}


And the `compile` function

function compile (allASaveTools, code, lang, allCodes, tool) {
  /** Сборка HTML на конкретном языке */
  const langFile = yaml.safeLoad(
    fs.readFileSync(`${srcDir}/lang/landing/${lang}.yaml`, 'utf8')
  )
  const JsChunks = utils.getAppJsChunks()
  const CssChunks = utils.getAppCssChunks()
  const langInfo = require(`${srcDir}/lang/langInfo.json`)
  const content = fs.readFileSync(
    `${srcDir}/lang/save_tools/${lang}/${tool}.md`,
    'utf8'
  )
  // FIXME тут ГЛЮК! почему-то используется послежний язык в списке
  const more = {
    code, // <=== !!!!!! передаем код в шаблонизатор
    langInfo,
    content: md.render(content),
    filename: tool,
    allASaveTools: allASaveTools,
    h1: allASaveTools[tool][lang]['title'],
    moment,
    allCodes,
    JsChunks,
    CssChunks
  }
  return src(`${srcDir}/templates/save_tools.html.ejs`)
    .pipe(ejs(Object.assign(allASaveTools[tool][lang], more, langFile))) // <=== !!!!!! шаблонизатор
    .pipe(replace('<img src', '<img loading="lazy" src'))
    .pipe(rename('index.html_' + code)) // <=== !!!!!! добавляем к имени файла
    .pipe(dest(`dist/${code}/${tool}/`)) // <=== !!!!!! код также в структуре папок
}


In `lang` the language code is of type `ru`, and in `code` it is of type `ru-ru` or `ru-by`. So, code is used in the path to the file and in the template engine ``. And also added it to the file name for clarity.

All files are created in the right folders with the right names according to the list of language codes (code). For example,

- `dist/ru-ru/tool1/index.html_ru-ru`
- `dist/ru-by/tool1/index.html_ru-by`
- `dist/ru-kz/tool1/index.html_ru-kz`

But for some reason, only the last language code appears inside the files. That is, each of these files will contain the `ru-kz` code, and not the one that was passed to the template engine. At the same time, all other template variables are written correctly. Except the code.

Why is this happening?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question