Skip to content

Commit

Permalink
Add filters and functions when the environment is ready
Browse files Browse the repository at this point in the history
  • Loading branch information
BenSurgisonGDS committed Nov 22, 2023
1 parent 400e101 commit 946d2fc
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/filters/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ module.exports = {
addFilter,
getFilter
},
setEnvironment
setEnvironment,
runWhenEnvIsAvailable
}
10 changes: 6 additions & 4 deletions lib/filters/core-filters.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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('<script>console.log(' + JSON.stringify(a, null, '\t') + ');</script>'))
runWhenEnvIsAvailable(() => {
const nunjucksSafe = getFilter('safe')
addFilter('log', a => nunjucksSafe('<script>console.log(' + JSON.stringify(a, null, '\t') + ');</script>'))
})
3 changes: 2 additions & 1 deletion lib/functions/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ module.exports = {
addFunction,
getFunction
},
setEnvironment
setEnvironment,
runWhenEnvIsAvailable
}
14 changes: 10 additions & 4 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit 946d2fc

Please sign in to comment.