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 <[email protected]>
Co-authored-by: Brett Kyle <[email protected]>
  • Loading branch information
3 people committed Nov 22, 2024
1 parent 0d3bfbb commit bcce2be
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 12 deletions.
25 changes: 25 additions & 0 deletions docs/contributing/managing-change.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,31 @@ If possible, update the mixin or function to maintain the existing functionality
}
```

### Deprecating a JavaScript file being removed

Removing a JavaScript file may happen because the module is no longer needed, or has been moved to another place in the project.

Awaiting for the next breaking, the file needs to remain included in the built package, marked as deprecated.

1. To ensure the file remains in the package, add its path within `src/govuk` to the list of paths in `packages/tasks/config/deprecated-scripts.mjs`. For example:

```mjs
export const deprecatedFilesPaths = [
'govuk-frontend-component.mjs'
]
```

This will build the file individually when creating the package, as it is no longer being discovered automatically by Rollup when building `all.mjs`.

2. To mark the file as deprecated, add the following JSDoc comment at the top of the file:

```js
/**
* @deprecated - Optionally describe where the file has been moved to or why it's been removed
* @module
*/
```

## Renaming things

When renaming things, keep the old name available as an alias and mark it as deprecated, following the steps above to [make sure we remember to remove the deprecated feature](#make-sure-we-remember-to-remove-the-deprecated-feature).
Expand Down
30 changes: 18 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 Down
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
/**
* @deprecated - The file has been renamed `component.mjs`
* @module
*/

export { Component as GOVUKFrontendComponent } from './component.mjs'
45 changes: 45 additions & 0 deletions packages/govuk-frontend/tasks/config/deprecated-scripts.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 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']

/**
* 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 bcce2be

Please sign in to comment.