-
Notifications
You must be signed in to change notification settings - Fork 14
/
nunjucks.js
54 lines (48 loc) · 1.71 KB
/
nunjucks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const fs = require('node:fs')
const path = require('node:path')
const Nunjucks = require('nunjucks')
/**
* If there is a version conflict between a govuk-eleventy-plugin dependency
* and the host project's dependencies, npm will include the expected version
* in a nested node_modules folder.
* @param {string} module - Module name
* @returns {string} Path to module
*/
const resolveNpmModule = (module) => {
const localPath = `./node_modules/@x-govuk/govuk-eleventy-plugin/node_modules/${module}`
return fs.existsSync(localPath) ? localPath : `./node_modules/${module}`
}
/**
* Configure Nunjucks environment
* @see {@link https://mozilla.github.io/nunjucks/api.html#environment}
* @param {object} eleventyConfig - Eleventy config
* @returns {Function} Nunjucks environment
*/
module.exports = (eleventyConfig) => {
const { includes, input, layouts } = eleventyConfig.dir
const searchPaths = [
'./node_modules/@x-govuk/govuk-eleventy-plugin',
'./node_modules/govuk-frontend/dist',
resolveNpmModule('@x-govuk/govuk-prototype-components'),
...(includes ? [path.join(input, includes)] : []),
...(layouts ? [path.join(input, layouts)] : []),
...input
]
/**
* Set default options, but respect `nunjucksEnvironmentOptions`
* @see {@link https://www.11ty.dev/docs/languages/nunjucks/#optional-use-your-nunjucks-environment-options}
*/
const options = {
autoescape: false,
lstripBlocks: true,
trimBlocks: true,
noCache: process.env === 'development',
watch: process.env === 'development',
...eleventyConfig.nunjucksEnvironmentOptions
}
const nunjucks = new Nunjucks.Environment(
new Nunjucks.FileSystemLoader(searchPaths),
options
)
return nunjucks
}