Skip to content

Commit

Permalink
Add deprecated JavaScript files to package build
Browse files Browse the repository at this point in the history
Because they're not imported by `all.mjs` deprecated JavaScript files were not included in the package (in which they need to remain until the next breaking release).

This adds a module to store the list of deprecated JavaScript files and helper functions that are then used:
- in the Gulp configuration, to add a new task building each deprecated file individually
- in the Rollup configuration, to prevent the bundled output of the deprecated files as all we want is for them to be in the package as a module

Co-authored-by: Patrick Cartlidge <patrick.cartlidge@digital.cabinet-office.gov.uk>
Co-authored-by: Brett Kyle <brett.kyle@digital.cabinet-office.gov.uk>
  • Loading branch information
3 people committed Nov 21, 2024
1 parent 1e21410 commit 59adefb
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 12 deletions.
40 changes: 28 additions & 12 deletions packages/govuk-frontend/rollup.publish.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { babel } from '@rollup/plugin-babel'
import replace from '@rollup/plugin-replace'
import { defineConfig } from 'rollup'

import { isDeprecated } from './tasks/config/deprecated-scripts.mjs'

/**
* Rollup config for npm publish
*/
Expand All @@ -28,26 +30,30 @@ export default defineConfig(({ i: input }) => ({
* ECMAScript (ES) module bundles for browser <script type="module">
* or using `import` for modern browsers and Node.js scripts
*/
{
format: 'es',
isDeprecated(input)
? null
: {
format: 'es',

// Bundled modules
preserveModules: false
},
// Bundled modules
preserveModules: false
},

/**
* Universal Module Definition (UMD) bundle for browser <script>
* `window` globals and compatibility with CommonJS and AMD `require()`
*/
{
format: 'umd',
isDeprecated(input)
? null
: {
format: 'umd',

// Bundled modules
preserveModules: false,
// Bundled modules
preserveModules: false,

// Export via `window.GOVUKFrontend.${exportName}`
name: 'GOVUKFrontend'
}
// Export via `window.GOVUKFrontend.${exportName}`
name: 'GOVUKFrontend'
}
],

/**
Expand All @@ -66,3 +72,13 @@ export default defineConfig(({ i: input }) => ({
})
]
}))

export const renamedFilesThatShouldBeKept = [
'govuk/govuk-frontend-component.mjs'
]

export function shouldBuildBundle(entryPath) {
return !renamedFilesThatShouldBeKept.some((renamedFile) =>
entryPath.endsWith(renamedFile)
)
}
48 changes: 48 additions & 0 deletions packages/govuk-frontend/tasks/config/deprecated-scripts.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Logistics for handling files that get renamed and are no longer part of modules imported by `all.mjs`
* but need to remain in the published package to support deprecations before they get removed
*/
import { join } from 'node:path'

/**
* Paths to the deprecated files within `src/govuk`
* (excluding the `src/govuk` part)
*/
export const deprecatedFilesPaths = [
'govuk-frontend-component.mjs',
'govuk-frontend-component-copy.mjs'
]

/**
* Checks if given Rollup input is a deprecated file
*
* This helps us decide whether to create bundled version of that input,
* which we don't want for deprecated files (we just want to include them in the package)
*
* @param {string} rollupInput - The path to the input Rollup is compiling
* @returns {boolean} - Whether the path corresponds to a deprecated file
*/
export function isDeprecated(rollupInput) {
return deprecatedFilesPaths.some((deprecatedFilePath) =>
rollupInput.endsWith(join('govuk', deprecatedFilePath))
)
}

/**
* Creates a glob matching the list of paths
*
* @param {string[]} paths - The list of paths to create a glob for
* @returns {string} - A glob matching the deprecated files
*/
export function createGlobFromPaths(paths) {
// Curly brace syntax in glob only works
// when there's more than one pattern to match
// so we need to distinguish between the two.
if (paths.length > 1) {
const joinedGlobs = paths.join(',')

return `{${joinedGlobs}}`
}

return paths[0]
}
19 changes: 19 additions & 0 deletions packages/govuk-frontend/tasks/scripts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { pkg } from '@govuk-frontend/config'
import { configs, scripts, task } from '@govuk-frontend/tasks'
import gulp from 'gulp'

import {
createGlobFromPaths,
deprecatedFilesPaths
} from './config/deprecated-scripts.mjs'

/**
* JavaScripts task (for watch)
*
Expand All @@ -24,6 +29,20 @@ export const compile = (options) =>
})
),

/**
* Compile deprecated files no longer imported by `all.mjs`
* but that need to be kept in the package
*/
task.name("compile:js 'deprecations'", () =>
scripts.compile(createGlobFromPaths(deprecatedFilesPaths), {
...options,

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

/**
* Compile GOV.UK Frontend JavaScript for main entry point only
*/
Expand Down

0 comments on commit 59adefb

Please sign in to comment.