Skip to content

Commit

Permalink
Update review app to use pre-built govuk-frontend modules
Browse files Browse the repository at this point in the history
Rollup build is not necessary when `govuk-frontend-{{ version }}.min.js` is already built

I’ve had to use `require()` inside ES modules to avoid experimental warnings using:

```
import pkg from 'govuk-frontend/package.json' assert { type: 'json' }
```
  • Loading branch information
colinrotherham committed Jul 5, 2023
1 parent 16796c5 commit f8bf269
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 89 deletions.
2 changes: 0 additions & 2 deletions docs/contributing/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ This task will:

This task will:

- check JavaScript code quality via ESLint (`npm run lint:js`) (using JavaScript Standard Style)
- compile JavaScript to Immediately Invoked Function Expression (IIFE) into `./packages/govuk-frontend-review/dist/javascripts`
- compile JavaScript documentation into `./packages/govuk-frontend-review/dist/docs/jsdoc`

## Review app only
Expand Down
13 changes: 11 additions & 2 deletions packages/govuk-frontend-review/browsersync.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { join } = require('path')

const pkg = require('govuk-frontend/package.json')
const { paths, ports } = require('govuk-frontend-config')
const { packageTypeToPath } = require('govuk-frontend-lib/names')

Expand All @@ -22,9 +23,14 @@ module.exports = {

// Files to watch for auto reload
files: [
join(paths.app, 'dist/javascripts/**/*.mjs'),
join(paths.app, 'dist/stylesheets/**/*.css'),
join(paths.app, 'src/views/**/*.njk'),

packageTypeToPath('govuk-frontend', {
modulePath: `govuk-frontend-${pkg.version}.min.js`,
moduleRoot: paths.app
}),

packageTypeToPath('govuk-frontend', {
modulePath: '**/*.njk',
moduleRoot: paths.app
Expand All @@ -36,7 +42,10 @@ module.exports = {
serveStatic: [
{
route: '/javascripts',
dir: join(paths.app, 'dist/javascripts')
dir: packageTypeToPath('govuk-frontend', {
modulePath: '/',
moduleRoot: paths.app
})
},
{
route: '/stylesheets',
Expand Down
2 changes: 1 addition & 1 deletion packages/govuk-frontend-review/nodemon.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"../../shared/lib",
"../../shared/stats"
],
"ignore": ["./src/javascripts/**", "**/*.test.*"],
"ignore": ["**/*.test.*"],
"events": {
"restart": "browser-sync reload --config browsersync.config.js"
},
Expand Down
45 changes: 0 additions & 45 deletions packages/govuk-frontend-review/rollup.config.mjs

This file was deleted.

6 changes: 6 additions & 0 deletions packages/govuk-frontend-review/src/app.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createRequire } from 'module'

import express from 'express'
import { getComponentsData, getComponentNames, filterPath } from 'govuk-frontend-lib/files'
import { componentNameToMacroName } from 'govuk-frontend-lib/names'
Expand All @@ -9,6 +11,9 @@ import * as middleware from './common/middleware/index.mjs'
import * as nunjucks from './common/nunjucks/index.mjs'
import * as routes from './routes/index.mjs'

const require = createRequire(import.meta.url)
const { version } = require('govuk-frontend/package.json')

export default async () => {
const app = express()

Expand Down Expand Up @@ -44,6 +49,7 @@ export default async () => {
app.use(middleware.robots)

// Add build stats
app.locals.version = version
app.locals.stats = Object.fromEntries(await Promise.all(modulePaths
.map(async (modulePath) => [modulePath, await getStats(modulePath)])))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const router = express.Router()
*/

router.use('/assets', express.static(packageTypeToPath('govuk-frontend', { modulePath: 'assets', moduleRoot: paths.app })))
router.use('/javascripts', express.static(join(paths.app, 'dist/javascripts')))
router.use('/javascripts', express.static(packageTypeToPath('govuk-frontend', { modulePath: '/', moduleRoot: paths.app })))
router.use('/stylesheets', express.static(join(paths.app, 'dist/stylesheets')))

export default router
1 change: 0 additions & 1 deletion packages/govuk-frontend-review/src/javascripts/all.mjs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
{% endblock %}

{% block bodyEnd %}
<script type="module" src="/javascripts/all.bundle.min.mjs"></script>
<script type="module" src="/javascripts/govuk-frontend-{{ version }}.min.js"></script>
<script type="module">
import { initAll } from '/javascripts/all.bundle.min.mjs'
import { initAll } from '/javascripts/govuk-frontend-{{ version }}.min.js'
const $scope = document.getElementById('scoped')
initAll({ scope: $scope })
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@

{% block bodyEnd %}
<!-- block:bodyEnd -->
<script type="module" src="/javascripts/all.bundle.min.mjs"></script>
<script type="module" src="/javascripts/govuk-frontend-{{ version }}.min.js"></script>
<script type="module">
import { initAll } from '/javascripts/all.bundle.min.mjs'
import { initAll } from '/javascripts/govuk-frontend-{{ version }}.min.js'
initAll()
</script>
<!-- endblock:bodyEnd -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

{% block bodyEnd %}
<!-- block:bodyEnd -->
<script type="module" src="/javascripts/all.bundle.min.mjs"></script>
<script type="module" src="/javascripts/govuk-frontend-{{ version }}.min.js"></script>
<script type="module">
import { initAll } from '/javascripts/all.bundle.min.mjs'
import { initAll } from '/javascripts/govuk-frontend-{{ version }}.min.js'
initAll()
</script>
<!-- endblock:bodyEnd -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -937,9 +937,9 @@
{% endblock %}

{% block bodyEnd %}
<script type="module" src="/javascripts/all.bundle.min.mjs"></script>
<script type="module" src="/javascripts/govuk-frontend-{{ version }}.min.js"></script>
<script type="module">
import { initAll } from '/javascripts/all.bundle.min.mjs'
import { initAll } from '/javascripts/govuk-frontend-{{ version }}.min.js'
initAll({
accordion: {
i18n: {
Expand Down
4 changes: 2 additions & 2 deletions packages/govuk-frontend-review/src/views/layouts/_generic.njk
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
{% set mainClasses = 'govuk-main-wrapper--auto-spacing' %}

{% block bodyEnd %}
<script type="module" src="/javascripts/all.bundle.min.mjs"></script>
<script type="module" src="/javascripts/govuk-frontend-{{ version }}.min.js"></script>
<script type="module">
import { initAll } from '/javascripts/all.bundle.min.mjs'
import { initAll } from '/javascripts/govuk-frontend-{{ version }}.min.js'
initAll()
</script>
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{% block head %}
{{ super() }}
<script type="importmap">
{ "imports": { "govuk-frontend": "/javascripts/all.bundle.min.mjs" } }
{ "imports": { "govuk-frontend": "/javascripts/govuk-frontend-{{ version }}.min.js" } }
</script>
{% endblock %}

Expand All @@ -21,5 +21,5 @@
{% endblock %}

{% block bodyEnd %}
<script type="module" src="/javascripts/all.bundle.min.mjs"></script>
<script type="module" src="/javascripts/govuk-frontend-{{ version }}.min.js"></script>
{% endblock %}
17 changes: 2 additions & 15 deletions packages/govuk-frontend-review/tasks/scripts.mjs
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
import { join } from 'path'

import { npm, scripts, task } from 'govuk-frontend-tasks'
import { npm } from 'govuk-frontend-tasks'
import gulp from 'gulp'

/**
* JavaScripts task (for watch)
* Compilation, documentation
* Documentation
*
* @type {import('govuk-frontend-tasks').TaskFunction}
*/
export const compile = (options) => gulp.series(
task.name('compile:js', () =>
scripts.compile('all.mjs', {
...options,

srcPath: join(options.srcPath, 'javascripts'),
destPath: join(options.destPath, 'javascripts'),
configPath: join(options.basePath, 'rollup.config.mjs')
})
),

// Build JSDoc for /docs/javascript
npm.script('build:jsdoc', [], options)
)
10 changes: 0 additions & 10 deletions packages/govuk-frontend-review/tasks/watch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,12 @@ export const watch = (options) => gulp.parallel(
], styles(options))
),

/**
* JavaScripts lint watcher
*/
task.name('lint:js watch', () =>
gulp.watch([
`${slash(paths.app)}/src/javascripts/**/*.mjs`
], npm.script('lint:js:cli', [slash(join(options.workspace, '**/*.{cjs,js,md,mjs}'))]))
),

/**
* JavaScripts build watcher
*/
task.name('compile:js watch', () =>
gulp.watch([
`${slash(paths.root)}/typedoc.config.js`,
`${slash(paths.app)}/src/javascripts/**/*.mjs`,
`${slash(paths.package)}/dist/govuk/**/*.mjs`
], scripts(options))
)
Expand Down

0 comments on commit f8bf269

Please sign in to comment.