Skip to content

Commit

Permalink
Compile ES modules using Rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Mar 30, 2023
1 parent 69b3f08 commit f4326b9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
6 changes: 3 additions & 3 deletions tasks/build/package.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ export default gulp.series(
})
),

// Copy GOV.UK Frontend JavaScript (ES modules)
task.name('copy:mjs', () =>
files.copy('**/!(*.test).mjs', {
// Compile GOV.UK Frontend JavaScript (ES modules)
task.name('compile:mjs', () =>
scripts.compile('!(*.test).mjs', {
srcPath: join(paths.src, 'govuk'),
destPath: join(paths.package, 'govuk-esm')
})
Expand Down
9 changes: 7 additions & 2 deletions tasks/build/package.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,14 @@ describe('package/', () => {
join(requirePath, `${name}.js.map`) // with source map
]

// Only source `./govuk/**/*.mjs` files copied to `./govuk-esm/**/*.mjs`
// Only source `./govuk/**/*.mjs` files compiled to `./govuk-esm/**/*.mjs`
if (importFilter.test(requirePath)) {
output.push(join(requirePath.replace(importFilter, 'govuk-esm'), `${name}.mjs`))
const importPath = requirePath.replace(importFilter, 'govuk-esm')

output.push(...[
join(importPath, `${name}.mjs`),
join(importPath, `${name}.mjs.map`) // with source map
])
}

return output
Expand Down
32 changes: 21 additions & 11 deletions tasks/scripts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,38 @@ export async function compileJavaScript ([modulePath, { srcPath, destPath, fileP
const moduleSrcPath = join(srcPath, modulePath)
const moduleDestPath = join(destPath, filePath ? filePath(parse(modulePath)) : modulePath)

// Rollup output format
const format = moduleDestPath.endsWith('.mjs')
? 'es'
: 'umd'

// Create Rollup bundle
const bundle = await rollup({
input: moduleSrcPath
input: moduleSrcPath,

// Preserve JavaScript ESM import statements
experimentalPreserveModules: format === 'es'
})

// Compile JavaScript ESM to CommonJS
// Compile JavaScript to output format
const bundled = await bundle[moduleDestPath.endsWith('.min.js') ? 'generate' : 'write']({
file: moduleDestPath,
sourcemapFile: moduleDestPath,
dir: destPath,
format,
sourcemap: true,

// Universal Module Definition (UMD)
// for browser + Node.js compatibility
format: 'umd',
...format === 'umd' && {
file: moduleDestPath,

// Legacy mode is required for IE8 support
legacy: true,
// Legacy mode is required for IE8 support
legacy: true,

// Used to set the `window` global for 'iife' and 'umd' bundles
// Components are given unique names (e.g GOVUKFrontend.Accordion)
amd: { id: componentPathToModuleName(modulePath) },
name: componentPathToModuleName(modulePath)
// Used to set the `window` global for 'iife' and 'umd' bundles
// Components are given unique names (e.g GOVUKFrontend.Accordion)
amd: { id: componentPathToModuleName(modulePath) },
name: componentPathToModuleName(modulePath)
}
})

// Minify bundle
Expand Down

0 comments on commit f4326b9

Please sign in to comment.