Skip to content

Commit 457b7d4

Browse files
committed
Move devtool view context into separate function
1 parent f712fbd commit 457b7d4

4 files changed

Lines changed: 34 additions & 26 deletions

File tree

src/server/plugins/engine/configureEnginePlugin.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
type PluginOptions
1010
} from '~/src/server/plugins/engine/plugin.js'
1111
import { findPackageRoot } from '~/src/server/plugins/engine/plugin.js'
12+
import { devtoolContext } from '~/src/server/plugins/nunjucks/context.js'
1213
import { type RouteConfig } from '~/src/server/types.js'
1314

1415
export const configureEnginePlugin = async ({
@@ -36,9 +37,7 @@ export const configureEnginePlugin = async ({
3637
nunjucks: {
3738
paths: [join(findPackageRoot(), 'src/server/devserver')] // custom layout to make it really clear this is not the same as the runner
3839
},
39-
viewContext: () => ({
40-
baseLayoutPath: 'dxt-devtool-baselayout.html' // from plugin.options.nunjucks.paths
41-
})
40+
viewContext: devtoolContext
4241
}
4342
}
4443
}

src/server/plugins/nunjucks/context.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,6 @@ let webpackManifest
2222
* @param {FormRequest | FormRequestPayload | null} request
2323
*/
2424
export function context(request) {
25-
const manifestPath = join(config.get('publicDir'), 'assets-manifest.json')
26-
27-
if (!webpackManifest) {
28-
try {
29-
// eslint-disable-next-line -- Allow JSON type 'any'
30-
webpackManifest = JSON.parse(readFileSync(manifestPath, 'utf-8'))
31-
} catch {
32-
logger.error(`Webpack ${basename(manifestPath)} not found`)
33-
}
34-
}
35-
3625
const { params, path, response } = request ?? {}
3726

3827
const isPreviewMode = path?.startsWith(PREVIEW_PATH_PREFIX)
@@ -54,7 +43,6 @@ export function context(request) {
5443
// take consumers props first so we can override it
5544
...consumerViewContext,
5645
appVersion: pkg.version,
57-
assetPath: '/assets',
5846
config: {
5947
cdpEnvironment: config.get('cdpEnvironment'),
6048
designerUrl: config.get('designerUrl'),
@@ -66,14 +54,34 @@ export function context(request) {
6654
crumb: safeGenerateCrumb(request),
6755
currentPath: request ? `${request.path}${request.url.search}` : undefined,
6856
previewMode: isPreviewMode ? params?.state : undefined,
69-
slug: isResponseOK ? params?.slug : undefined,
57+
slug: isResponseOK ? params?.slug : undefined
58+
}
59+
60+
return ctx
61+
}
7062

63+
/**
64+
* Returns the context for the devtool. Consumers won't have access to this.
65+
*/
66+
export function devtoolContext() {
67+
const manifestPath = join(config.get('publicDir'), 'assets-manifest.json')
68+
69+
if (!webpackManifest) {
70+
try {
71+
// eslint-disable-next-line -- Allow JSON type 'any'
72+
webpackManifest = JSON.parse(readFileSync(manifestPath, 'utf-8'))
73+
} catch {
74+
logger.error(`Webpack ${basename(manifestPath)} not found`)
75+
}
76+
}
77+
78+
return {
79+
baseLayoutPath: 'dxt-devtool-baselayout.html', // from plugin.options.nunjucks.paths
80+
assetPath: '/assets',
7181
getDxtAssetPath: (asset = '') => {
7282
return `/${webpackManifest?.[asset] ?? asset}`
7383
}
7484
}
75-
76-
return ctx
7785
}
7886

7987
/**

src/server/plugins/nunjucks/context.test.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@ import { tmpdir } from 'node:os'
22

33
import { config } from '~/src/config/index.js'
44
import { encodeUrl } from '~/src/server/plugins/engine/helpers.js'
5-
import { context } from '~/src/server/plugins/nunjucks/context.js'
5+
import {
6+
context,
7+
devtoolContext
8+
} from '~/src/server/plugins/nunjucks/context.js'
69

710
describe('Nunjucks context', () => {
811
beforeEach(() => jest.resetModules())
912

1013
describe('Asset path', () => {
1114
it("should include 'assetPath' for GOV.UK Frontend icons", () => {
12-
const { assetPath } = context(null)
15+
const { assetPath } = devtoolContext()
1316
expect(assetPath).toBe('/assets')
1417
})
1518
})
1619

1720
describe('Asset helper', () => {
1821
it("should locate 'assets-manifest.json' assets", () => {
19-
const { getDxtAssetPath } = context(null)
22+
const { getDxtAssetPath } = devtoolContext()
2023

2124
expect(getDxtAssetPath('example.scss')).toBe(
2225
'/stylesheets/example.xxxxxxx.min.css'
@@ -32,13 +35,13 @@ describe('Nunjucks context', () => {
3235
const { config } = await import('~/src/config/index.js')
3336

3437
// Import when isolated to avoid cache
35-
const { context } = await import(
38+
const { devtoolContext } = await import(
3639
'~/src/server/plugins/nunjucks/context.js'
3740
)
3841

3942
// Update config for missing manifest
4043
config.set('publicDir', tmpdir())
41-
const { getDxtAssetPath } = context(null)
44+
const { getDxtAssetPath } = devtoolContext()
4245

4346
// Uses original paths when missing
4447
expect(getDxtAssetPath('example.scss')).toBe('/example.scss')
@@ -47,7 +50,7 @@ describe('Nunjucks context', () => {
4750
})
4851

4952
it('should return path to unknown assets', () => {
50-
const { getDxtAssetPath } = context(null)
53+
const { getDxtAssetPath } = devtoolContext()
5154

5255
expect(getDxtAssetPath()).toBe('/')
5356
expect(getDxtAssetPath('example.jpg')).toBe('/example.jpg')

src/server/plugins/nunjucks/types.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@
1212
/**
1313
* @typedef {object} ViewContext - Nunjucks view context
1414
* @property {string} appVersion - Application version
15-
* @property {string} assetPath - Asset path
1615
* @property {Partial<Config>} config - Application config properties
1716
* @property {string} [crumb] - Cross-Site Request Forgery (CSRF) token
1817
* @property {string} [cspNonce] - Content Security Policy (CSP) nonce
1918
* @property {string} [currentPath] - Current path
2019
* @property {string} [previewMode] - Preview mode
2120
* @property {string} [slug] - Form slug
22-
* @property {(asset?: string) => string} getDxtAssetPath - Asset path resolver
2321
* @property {FormContext} [context] - the current form context
2422
* @property {PluginOptions['viewContext']} [injectedViewContext] - the current form context
2523
*/

0 commit comments

Comments
 (0)