diff --git a/CHANGELOG.md b/CHANGELOG.md
index 95db7b62e5..89ebdd48a2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,10 @@
## Unreleased
+### Fixes
+
+- [#2378: Add filters and functions when the environment is ready](https://github.com/alphagov/govuk-prototype-kit/pull/2378)
+
## 13.15.1
### Fixes
diff --git a/lib/filters/api.js b/lib/filters/api.js
index 3c4f93e073..1475d291d9 100644
--- a/lib/filters/api.js
+++ b/lib/filters/api.js
@@ -55,5 +55,6 @@ module.exports = {
addFilter,
getFilter
},
- setEnvironment
+ setEnvironment,
+ runWhenEnvIsAvailable
}
diff --git a/lib/filters/core-filters.js b/lib/filters/core-filters.js
index e7618b91b6..f96df7720c 100644
--- a/lib/filters/core-filters.js
+++ b/lib/filters/core-filters.js
@@ -1,7 +1,6 @@
// local dependencies
-const { addFilter, getFilter } = require('../../').views
-
-const nunjucksSafe = getFilter('safe')
+const { runWhenEnvIsAvailable, external } = require('./api')
+const { addFilter, getFilter } = external
/**
* Logs an object in the template to the console in the browser.
@@ -10,4 +9,7 @@ const nunjucksSafe = getFilter('safe')
* @example {{ "hello world" | log }}
* @example {{ "hello world" | log | safe }} [for environments with autoescaping turned on]
*/
-addFilter('log', a => nunjucksSafe(''))
+runWhenEnvIsAvailable(() => {
+ const nunjucksSafe = getFilter('safe')
+ addFilter('log', a => nunjucksSafe(''))
+})
diff --git a/lib/functions/api.js b/lib/functions/api.js
index e38e1f6a24..73059d5793 100644
--- a/lib/functions/api.js
+++ b/lib/functions/api.js
@@ -62,5 +62,6 @@ module.exports = {
addFunction,
getFunction
},
- setEnvironment
+ setEnvironment,
+ runWhenEnvIsAvailable
}
diff --git a/lib/utils/index.js b/lib/utils/index.js
index 9320e54d9f..353b3bea58 100644
--- a/lib/utils/index.js
+++ b/lib/utils/index.js
@@ -18,6 +18,8 @@ const plugins = require('../plugins/plugins')
const routes = require('../routes/api')
const { appDir, projectDir } = require('./paths')
const { asyncSeriesMap } = require('./asyncSeriesMap')
+const { runWhenEnvIsAvailable: runWhenFiltersEnvIsAvailable } = require('../filters/api')
+const { runWhenEnvIsAvailable: runWhenFunctionsEnvIsAvailable } = require('../functions/api')
// Tweak the Markdown renderer
const defaultMarkedRenderer = marked.defaults.renderer || new marked.Renderer()
@@ -53,8 +55,10 @@ function addNunjucksFilters (env) {
if (fs.existsSync(filtersPath)) {
additionalFilters.push(filtersPath)
}
- const filterFiles = plugins.getFileSystemPaths('nunjucksFilters').concat(additionalFilters)
- filterFiles.forEach(x => require(x))
+ runWhenFiltersEnvIsAvailable(() => {
+ const filterFiles = plugins.getFileSystemPaths('nunjucksFilters').concat(additionalFilters)
+ filterFiles.forEach(x => require(x))
+ })
}
// Require core and custom functions, merges to one object
@@ -66,8 +70,10 @@ function addNunjucksFunctions (env) {
if (fs.existsSync(functionsPath)) {
additionalFunctions.push(functionsPath)
}
- const globalFiles = plugins.getFileSystemPaths('nunjucksFunctions').concat(additionalFunctions)
- globalFiles.forEach(x => require(x))
+ runWhenFunctionsEnvIsAvailable(() => {
+ const globalFiles = plugins.getFileSystemPaths('nunjucksFunctions').concat(additionalFunctions)
+ globalFiles.forEach(x => require(x))
+ })
}
function addRouters (app) {