From 48658265d1918e815fee71e11d1824d0de695ff2 Mon Sep 17 00:00:00 2001 From: Durgesh kumar prajapati <98798977+Durgesh4993@users.noreply.github.com> Date: Thu, 29 Feb 2024 05:37:14 +0530 Subject: [PATCH] Update filtersRegistry.js --- lib/filtersRegistry.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) 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);