Skip to content

Commit

Permalink
Provide bundled JavaScript UMD export
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Aug 14, 2022
1 parent ee8fb10 commit a7f590e
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 42 deletions.
15 changes: 9 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
"x-govuk"
],
"main": "x-govuk/all.js",
"sass": "x-govuk/all.scss",
"module": "x-govuk/all.mjs",
"repository": {
"type": "git",
"url": "https://github.com/x-govuk/govuk-prototype-components.git"
},
"scripts": {
"prepublishOnly": "rollup --config",
"prebuild": "rimraf public",
"build": "eleventy --quiet",
"predev": "rimraf public",
Expand All @@ -45,9 +46,12 @@
"lodash": "^4.17.21"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^22.0.2",
"@rollup/plugin-node-resolve": "^13.3.0",
"ava": "^4.0.1",
"c8": "^7.11.0",
"nunjucks": "^3.2.3",
"rollup": "^2.78.0",
"standard": "^17.0.0",
"stylelint": "^14.6.1",
"stylelint-config-gds": "^0.2.0"
Expand All @@ -57,7 +61,8 @@
},
"exports": {
".": {
"import": "./x-govuk/all.js",
"sass": "./x-govuk/all.scss",
"import": "./x-govuk/all.mjs",
"require": "./x-govuk/all.js"
},
"./decorate": {
Expand Down
17 changes: 17 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import commonJs from '@rollup/plugin-commonjs'
import { nodeResolve } from '@rollup/plugin-node-resolve'

export default [{
input: [
'x-govuk/all.mjs'
],
output: {
dir: 'x-govuk',
format: 'umd',
name: 'GOVUKPrototypeComponents'
},
plugins: [
nodeResolve(),
commonJs()
]
}]
166 changes: 132 additions & 34 deletions x-govuk/all.js

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions x-govuk/all.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Autocomplete from './components/autocomplete/autocomplete.js'
import Edge from './components/edge/edge.js'
import WarnOnUnsavedChanges from './components/warn-on-unsaved-changes/warn-on-unsaved-changes.js'

function initAll (options) {
// Set the options to an empty object by default if no options are passed.
options = typeof options !== 'undefined' ? options : {}

// Allow user to initialise components in only certain sections of the page
// Defaults to the entire document if nothing is set.
const scope = typeof options.scope !== 'undefined' ? options.scope : document

const $autocompletes = scope.querySelectorAll('[data-module="autocomplete"]')
$autocompletes.forEach(function ($autocomplete) {
new Autocomplete($autocomplete).init()
})

const $edges = scope.querySelectorAll('[data-module="edge"]')
$edges.forEach(function ($edge) {
new Edge($edge).init()
})

const $forms = scope.querySelectorAll('[data-module="warn-on-unsaved-changes"]')
$forms.forEach(function ($form) {
new WarnOnUnsavedChanges($form).init()
})
}

export {
initAll,
Autocomplete,
Edge,
WarnOnUnsavedChanges
}

0 comments on commit a7f590e

Please sign in to comment.