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 lib functions renderTemplate() and renderPreview() etc #4329

Merged
merged 4 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions packages/govuk-frontend-review/src/app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
getComponentsFixtures,
getComponentNames,
getComponentNamesFiltered,
renderComponent
renderComponent,
renderPreview
} from '@govuk-frontend/lib/components'
import { filterPath, hasPath } from '@govuk-frontend/lib/files'
import { getStats, modulePaths } from '@govuk-frontend/stats'
Expand Down Expand Up @@ -200,9 +201,11 @@ export default async () => {

// Test view for injecting rendered components
// and testing specific JavaScript configurations
// Example view
app.get('/tests/boilerplate', function (req, res) {
res.render('tests/boilerplate')
const componentName = undefined

// Render blank component preview
res.send(renderPreview(componentName, { env }))
})

// Full page example views
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const frontendPath = packageTypeToPath('govuk-frontend', {

router.use('/assets', express.static(join(frontendPath, 'assets')))
router.use('/javascripts', express.static(frontendPath))
router.use('/stylesheets', express.static(join(paths.app, 'dist/stylesheets')))
router.use('/stylesheets', [
express.static(frontendPath),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question Just to confirm that I understand this change properly: this leads Express to also look in govuk-frontend (however resolved earlier in the file) alongside looking at the built stylesheets from the review app. In turn, this enables it to find the /stylesheets/govuk-frontend.min.css from the boilerplate markup, is that it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah Express.js accepts an array of routes to try in order

Makes sure we can resolve CSS files from both locations:

/stylesheets/govuk-frontend.min.css
/stylesheets/app.min.css

express.static(join(paths.app, 'dist/stylesheets'))
])

export default router
25 changes: 0 additions & 25 deletions packages/govuk-frontend-review/src/views/tests/boilerplate.njk

This file was deleted.

51 changes: 51 additions & 0 deletions shared/lib/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,56 @@ function renderMacro(macroName, macroPath, options) {
return renderString(macroString, options)
}

/**
* Render component preview on boilerplate page
*
* @param {string} [componentName] - Component name
* @param {MacroRenderOptions} [options] - Nunjucks macro render options
* @returns {string} HTML rendered from the Nunjucks template
*/
function renderPreview(componentName, options) {
const stylesPath = '/stylesheets/govuk-frontend.min.css'
const scriptsPath = '/javascripts/govuk-frontend.min.js'

// Render page template
return renderTemplate('govuk/template.njk', {
blocks: {
pageTitle: 'Test boilerplate - GOV.UK',
head: outdent`
<link rel="stylesheet" href="${stylesPath}">

<script type="importmap">
{ "imports": { "govuk-frontend": "${scriptsPath}" } }
</script>
`,

// Remove default template blocks
skipLink: '',
bodyStart: '',
header: '',
footer: '',

main: outdent`
<div class="govuk-width-container">
<h1 class="govuk-heading-l">Test boilerplate</h1>
<p class="govuk-body">Used during testing to inject rendered components and test specific configurations</p>

<div id="slot" class="govuk-!-margin-top-6">
${componentName ? renderComponent(componentName, options) : ''}
</div>
</div>
`,

bodyEnd: outdent`
<script type="module" src="${scriptsPath}"></script>
`
},
context: {
mainClasses: 'govuk-main-wrapper--auto-spacing'
}
})
}

/**
* Render string
*
Expand Down Expand Up @@ -214,6 +264,7 @@ module.exports = {
nunjucksEnv,
renderComponent,
renderMacro,
renderPreview,
renderString,
renderTemplate
}
Expand Down