Skip to content

Commit 946d2fc

Browse files
Add filters and functions when the environment is ready
1 parent 400e101 commit 946d2fc

5 files changed

Lines changed: 24 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Fixes
6+
7+
- [#2378: Add filters and functions when the environment is ready](https://github.com/alphagov/govuk-prototype-kit/pull/2378)
8+
59
## 13.15.1
610

711
### Fixes

lib/filters/api.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@ module.exports = {
5555
addFilter,
5656
getFilter
5757
},
58-
setEnvironment
58+
setEnvironment,
59+
runWhenEnvIsAvailable
5960
}

lib/filters/core-filters.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// local dependencies
2-
const { addFilter, getFilter } = require('../../').views
3-
4-
const nunjucksSafe = getFilter('safe')
2+
const { runWhenEnvIsAvailable, external } = require('./api')
3+
const { addFilter, getFilter } = external
54

65
/**
76
* Logs an object in the template to the console in the browser.
@@ -10,4 +9,7 @@ const nunjucksSafe = getFilter('safe')
109
* @example {{ "hello world" | log }}
1110
* @example {{ "hello world" | log | safe }} [for environments with autoescaping turned on]
1211
*/
13-
addFilter('log', a => nunjucksSafe('<script>console.log(' + JSON.stringify(a, null, '\t') + ');</script>'))
12+
runWhenEnvIsAvailable(() => {
13+
const nunjucksSafe = getFilter('safe')
14+
addFilter('log', a => nunjucksSafe('<script>console.log(' + JSON.stringify(a, null, '\t') + ');</script>'))
15+
})

lib/functions/api.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@ module.exports = {
6262
addFunction,
6363
getFunction
6464
},
65-
setEnvironment
65+
setEnvironment,
66+
runWhenEnvIsAvailable
6667
}

lib/utils/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const plugins = require('../plugins/plugins')
1818
const routes = require('../routes/api')
1919
const { appDir, projectDir } = require('./paths')
2020
const { asyncSeriesMap } = require('./asyncSeriesMap')
21+
const { runWhenEnvIsAvailable: runWhenFiltersEnvIsAvailable } = require('../filters/api')
22+
const { runWhenEnvIsAvailable: runWhenFunctionsEnvIsAvailable } = require('../functions/api')
2123

2224
// Tweak the Markdown renderer
2325
const defaultMarkedRenderer = marked.defaults.renderer || new marked.Renderer()
@@ -53,8 +55,10 @@ function addNunjucksFilters (env) {
5355
if (fs.existsSync(filtersPath)) {
5456
additionalFilters.push(filtersPath)
5557
}
56-
const filterFiles = plugins.getFileSystemPaths('nunjucksFilters').concat(additionalFilters)
57-
filterFiles.forEach(x => require(x))
58+
runWhenFiltersEnvIsAvailable(() => {
59+
const filterFiles = plugins.getFileSystemPaths('nunjucksFilters').concat(additionalFilters)
60+
filterFiles.forEach(x => require(x))
61+
})
5862
}
5963

6064
// Require core and custom functions, merges to one object
@@ -66,8 +70,10 @@ function addNunjucksFunctions (env) {
6670
if (fs.existsSync(functionsPath)) {
6771
additionalFunctions.push(functionsPath)
6872
}
69-
const globalFiles = plugins.getFileSystemPaths('nunjucksFunctions').concat(additionalFunctions)
70-
globalFiles.forEach(x => require(x))
73+
runWhenFunctionsEnvIsAvailable(() => {
74+
const globalFiles = plugins.getFileSystemPaths('nunjucksFunctions').concat(additionalFunctions)
75+
globalFiles.forEach(x => require(x))
76+
})
7177
}
7278

7379
function addRouters (app) {

0 commit comments

Comments
 (0)