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

Compile GOV.UK Prototype Kit config to package top level #3623

Merged
merged 2 commits into from
May 18, 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
1 change: 1 addition & 0 deletions bin/pre-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ npm run build:package
if [[ -n $(git status --porcelain) ]]; then
echo "✍️ Commiting changed package"
git add packages/govuk-frontend/dist/
git add --force packages/govuk-frontend/govuk-prototype-kit.config.json

git commit -m "Release GOV.UK Frontend to '$BRANCH_NAME' for testing"

Expand Down
2 changes: 2 additions & 0 deletions packages/govuk-frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
govuk-prototype-kit.config.json

# Build output (committed)
!/dist
1 change: 1 addition & 0 deletions packages/govuk-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"files": [
"src",
"dist",
"govuk-prototype-kit.config.json",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This ensures GOV.UK Prototype Kit can find the config

But paths at their end are still hard coded: alphagov/govuk-prototype-kit@953830a

"package.json",
"README.md"
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { join } from 'path'

import { filterPath, getDirectories, getListing } from 'govuk-frontend-lib/files'
import { componentNameToMacroName, packageNameToPath } from 'govuk-frontend-lib/names'
import slash from 'slash'
Expand All @@ -10,35 +8,35 @@ import slash from 'slash'
* @returns {Promise<PrototypeKitConfig>} GOV.UK Prototype Kit config
*/
export default async () => {
const componentsFiles = await getListing(packageNameToPath('govuk-frontend', 'src/govuk/components'))
const componentMacros = await getListing(packageNameToPath('govuk-frontend', 'src'), '**/components/**/macro.njk')
const componentNames = await getDirectories(packageNameToPath('govuk-frontend', 'src/govuk/components'))

// Build array of macros
const nunjucksMacros = componentNames
.map((componentName) => {
const [macroPath] = componentsFiles
const [macroPath = ''] = componentMacros
.filter(filterPath([`**/${componentName}/macro.njk`]))

return {
importFrom: slash(join('govuk/components', macroPath)),
importFrom: slash(macroPath),
macroName: componentNameToMacroName(componentName)
}
})

return {
assets: [
'/govuk/assets',
'/govuk/all.js.map'
'/dist/govuk/assets',
'/dist/govuk/all.js.map'
],
sass: [
'/govuk-prototype-kit/init.scss'
'/dist/govuk-prototype-kit/init.scss'
],
scripts: [
'/govuk/all.js',
'/govuk-prototype-kit/init.js'
'/dist/govuk/all.js',
'/dist/govuk-prototype-kit/init.js'
],
nunjucksMacros,
nunjucksPaths: ['/']
nunjucksPaths: ['/dist']
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ describe('GOV.UK Prototype Kit config', () => {

it('includes paths for assets, scripts, sass', () => {
expect(config.assets).toEqual([
'/govuk/assets',
'/govuk/all.js.map'
'/dist/govuk/assets',
'/dist/govuk/all.js.map'
])

expect(config.sass).toEqual([
'/govuk-prototype-kit/init.scss'
'/dist/govuk-prototype-kit/init.scss'
])

expect(config.scripts).toEqual([
'/govuk/all.js',
'/govuk-prototype-kit/init.js'
'/dist/govuk/all.js',
'/dist/govuk-prototype-kit/init.js'
])
})

Expand Down Expand Up @@ -159,7 +159,7 @@ describe('GOV.UK Prototype Kit config', () => {
})

it('includes paths', () => {
expect(config.nunjucksPaths).toEqual(['/'])
expect(config.nunjucksPaths).toEqual(['/dist'])
})
})
})
20 changes: 16 additions & 4 deletions packages/govuk-frontend/tasks/build/package.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { filterPath, getDirectories, getListing, mapPathTo } from 'govuk-fronten
import { componentNameToClassName, componentPathToModuleName } from 'govuk-frontend-lib/names'

describe('packages/govuk-frontend/dist/', () => {
let listingPackage
let listingSource
let listingDist

Expand All @@ -17,6 +18,7 @@ describe('packages/govuk-frontend/dist/', () => {
let componentNames

beforeAll(async () => {
listingPackage = await getListing(paths.package, '*')
listingSource = await getListing(join(paths.package, 'src'))
listingDist = await getListing(join(paths.package, 'dist'))

Expand All @@ -41,10 +43,8 @@ describe('packages/govuk-frontend/dist/', () => {
const listingExpected = listingSource
.filter(filterPath(filterPatterns))

// Replaces GOV.UK Prototype kit config with JSON
.flatMap(mapPathTo(['**/govuk-prototype-kit.config.mjs'], ({ dir: requirePath, name }) => [
join(requirePath, '../', `${name}.json`)
]))
// Removes GOV.UK Prototype kit config (moved to package top level)
.flatMap(mapPathTo(['**/govuk-prototype-kit.config.mjs'], () => []))

// Replaces all source '*.mjs' files
.flatMap(mapPathTo(['**/*.mjs'], ({ dir: requirePath, name }) => {
Expand Down Expand Up @@ -89,6 +89,18 @@ describe('packages/govuk-frontend/dist/', () => {

// Compare array of actual output files
expect(listingDist).toEqual(listingExpected)

// Check top level package contents
expect(listingPackage).toEqual([
'README.md',
'govuk-prototype-kit.config.json',
'gulpfile.mjs',
'package.json',
'postcss.config.mjs',
'postcss.config.unit.test.mjs',
'tsconfig.build.json',
'tsconfig.json'
])
})

describe('README.md', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/govuk-frontend/tasks/scripts.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join } from 'path'
import { join, resolve } from 'path'

import { configs, scripts, task } from 'govuk-frontend-tasks'
import gulp from 'gulp'
Expand Down Expand Up @@ -39,7 +39,7 @@ export const compile = (options) => gulp.series(
task.name("compile:js 'govuk-prototype-kit'", () =>
configs.compile('govuk-prototype-kit.config.mjs', {
srcPath: join(options.srcPath, 'govuk-prototype-kit'),
destPath: options.destPath,
destPath: resolve(options.destPath, '../'), // Top level (not dist) for compatibility

filePath (file) {
return join(file.dir, `${file.name}.json`)
Expand Down