Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filters and functions when the environment is ready #2378

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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