Make use of JS Modules less confusing

It's not obvious how the code that includes JS
Modules in the frontend build works.

This adds lots of comments to explain the various
bits and flattens `modules/all.mjs` to just be a
single function that starts off the window.GOVUK
namespace.

Also removes `module/all.js` from the repo'. It's
an artefact used by the frontend build so
shouldn't be included as source code.
This commit is contained in:
Tom Byers
2019-11-19 14:57:49 +00:00
parent 8890b7c4de
commit 21f9ecfcda
5 changed files with 26 additions and 750 deletions

View File

@@ -57,19 +57,24 @@ const bundleJavaScriptModules = async function () {
const bundle = await rollup.rollup({
input: paths.src + 'javascripts/modules/all.mjs',
plugins: [
// determine module entry points from either 'module' or 'main' fields in package.json
rollupPluginNodeResolve({
mainFields: ['module', 'main']
}),
// gulp rollup runs on nodeJS so reads modules in commonJS format
// this adds node_modules to the require path so it can find the GOVUK Frontend modules
rollupPluginCommonjs({
include: 'node_modules/**'
})
]
});
// write resulting module to Immediately Invoked Function Expression (IIFE) format
// map the exported code to the window.GOVUK namespace
await bundle.write({
file: paths.src + 'javascripts/modules/all.js',
format: 'iife',
name: 'GOVUKFrontend'
name: 'GOVUK'
});
};