Skip to content

Commit

Permalink
Add UMD bundle.min.js for backwards compatibility
Browse files Browse the repository at this point in the history
This precompiled file maintains compatibility for non-bundler users
  • Loading branch information
colinrotherham committed Jun 1, 2023
1 parent a6c821f commit a27237f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/contributing/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ npm scripts are defined in `package.json`. These trigger a number of Gulp tasks.
- copy GOV.UK Prototype Kit config files
- compile JavaScript to CommonJS modules
- compile JavaScript to ECMAScript (ES) modules
- compile JavaScript to Universal Module Definition (UMD) bundle
- runs `npm run postbuild:package` (which will test the output is correct)

**`npm run build:release` will do the following:**
Expand Down
4 changes: 2 additions & 2 deletions packages/govuk-frontend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ Some of the JavaScript included in GOV.UK Frontend improves the usability and
accessibility of the components. You should make sure that you are importing and
initialising Javascript in your application to ensure that all users can use it successfully.

You can include Javascript for all components either by copying the `all.js` from `node_modules/govuk-frontend/dist/govuk/` into your application or referencing the file directly:
You can include Javascript for all components either by copying the `bundle.min.js` from `node_modules/govuk-frontend/dist/govuk/` into your application or referencing the file directly:

```html
<script src="<path-to-govuk-frontend-all-file>/all.js"></script>
<script src="<path-to-govuk-frontend-bundle>/bundle.min.js"></script>
```

Next you need to initialise the script by adding:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ export default async () => {
return {
assets: [
'/dist/govuk/assets',
'/dist/govuk/all.js.map'
'/dist/govuk/bundle.min.js.map'
],
sass: [
'/dist/govuk-prototype-kit/init.scss'
],
scripts: [
'/dist/govuk/all.js',
'/dist/govuk/bundle.min.js',
'/dist/govuk-prototype-kit/init.js'
],
nunjucksMacros,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ describe('GOV.UK Prototype Kit config', () => {
it('includes paths for assets, scripts, sass', () => {
expect(config.assets).toEqual([
'/dist/govuk/assets',
'/dist/govuk/all.js.map'
'/dist/govuk/bundle.min.js.map'
])

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

expect(config.scripts).toEqual([
'/dist/govuk/all.js',
'/dist/govuk/bundle.min.js',
'/dist/govuk-prototype-kit/init.js'
])
})
Expand Down
18 changes: 18 additions & 0 deletions packages/govuk-frontend/tasks/build/package.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ describe('packages/govuk-frontend/dist/', () => {
join(requirePath, `${name}.mjs.map`) // with source map
]))

// Only source `./govuk/all.mjs` compiled to UMD bundle
.flatMap(mapPathTo(['**/govuk/all.mjs'], ({ dir: requirePath, name }) => [
join(requirePath, `${name}.mjs`),

// UMD bundle (minified) for compatibility
join(requirePath, 'bundle.min.js'),
join(requirePath, 'bundle.min.js.map') // with source map
]))

// Add Autoprefixer prefixes to all source '*.scss' files
.flatMap(mapPathTo(['**/*.scss'], ({ dir: requirePath, name }) => [
join(requirePath, `${name}.scss`),
Expand Down Expand Up @@ -135,6 +144,15 @@ describe('packages/govuk-frontend/dist/', () => {
})
})

describe('bundle.min.js', () => {
it("should export 'GOVUKFrontend' AMD module definition", async () => {
const contents = await readFile(join(paths.package, 'dist/govuk/bundle.min.js'), 'utf8')

// Look for AMD module definition for 'GOVUKFrontend' (minified)
expect(contents).toContain('typeof define&&define.amd?define("GOVUKFrontend",["exports"],e)')
})
})

describe('common/govuk-frontend-version.mjs', () => {
it('should have correct version number', async () => {
const contents = await readFile(join(paths.package, 'dist/govuk/common/govuk-frontend-version.mjs'), 'utf8')
Expand Down
18 changes: 18 additions & 0 deletions packages/govuk-frontend/tasks/scripts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ export const compile = (options) => gulp.series(
})
),

/**
* Compile GOV.UK Frontend JavaScript (UMD bundle, minified)
*/
task.name("compile:js 'bundle'", () =>
scripts.compile('all.mjs', {
...options,

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

// Rename to `bundle.*` based on module format
filePath ({ dir, ext }) {
return join(dir, `bundle.min${ext}`)
}
})
),

/**
* Compile GOV.UK Prototype Kit config
*/
Expand Down

0 comments on commit a27237f

Please sign in to comment.