diff --git a/lib/filtersRegistry.js b/lib/filtersRegistry.js index 15b23f0be..fd6cb5f18 100644 --- a/lib/filtersRegistry.js +++ b/lib/filtersRegistry.js @@ -32,20 +32,28 @@ function registerLocalFilters(nunjucks, templateDir, filtersDir) { followLinks: false }); - walker.on('file', async (root, stats, next) => { - try { + walker.on('file', async (root, stats, next) => { + try { + // Construct the absolute path to the file const filePath = path.resolve(templateDir, path.resolve(root, stats.name)); - // If it's a module constructor, inject dependencies to ensure consistent usage in remote templates in other projects or plain directories. + + // Clear the module cache to ensure consistent usage of modules delete require.cache[require.resolve(filePath)]; + + // Require the module at the specified file path const mod = require(filePath); - + + // Add filters from the module to Nunjucks addFilters(nunjucks, mod); + // Move to the next file next(); - } catch (e) { + } catch (e) { + // If an error occurs, reject the promise reject(e); - } - }); + } +}).on('error', reject); + walker.on('errors', (root, nodeStatsArray) => { reject(nodeStatsArray);