Switch to using gulp-better-rollup

Means our rollup bundling doesn't leave any
artefact files lying around that we'd then have to
deal with.

Also includes:
- removal of some JSHint config' marking the
  artefacts as scripts to ignore
- use of streamqueue package to allow the same
  ordering of scripts as before
This commit is contained in:
Tom Byers
2019-11-20 13:43:07 +00:00
parent e034f5b77a
commit 0e10879ef2
3 changed files with 71 additions and 71 deletions

View File

@@ -2,10 +2,10 @@
// a bit like `app/__init__` in the Flask app. // a bit like `app/__init__` in the Flask app.
// //
// When processed by a bundler, this is turned into a Immediately Invoked Function Expression (IIFE) // When processed by a bundler, this is turned into a Immediately Invoked Function Expression (IIFE)
// and saved as `all.js` in the same folder as this file. The IIFE format allows it to run in // The IIFE format allows it to run in browsers that don't support JS Modules.
// browsers that don't support JS Modules.
// //
// Exported items will be added to the window.GOVUK namespace. // Exported items will be added to the window.GOVUK namespace.
// For example, `export { Frontend }` will assign `Frontend` to `window.Frontend`
import Header from 'govuk-frontend/components/header/header'; import Header from 'govuk-frontend/components/header/header';
// Copy of the initAll function from https://github.com/alphagov/govuk-frontend/blob/v2.13.0/src/all.js // Copy of the initAll function from https://github.com/alphagov/govuk-frontend/blob/v2.13.0/src/all.js

View File

@@ -6,16 +6,16 @@
// 1. LIBRARIES // 1. LIBRARIES
// - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - -
const { src, pipe, dest, series, parallel, watch } = require('gulp'); const { src, pipe, dest, series, parallel, watch } = require('gulp');
const rollup = require('rollup');
const rollupPluginCommonjs = require('rollup-plugin-commonjs'); const rollupPluginCommonjs = require('rollup-plugin-commonjs');
const rollupPluginNodeResolve = require('rollup-plugin-node-resolve'); const rollupPluginNodeResolve = require('rollup-plugin-node-resolve');
const streamqueue = require('streamqueue');
const stylish = require('jshint-stylish'); const stylish = require('jshint-stylish');
const del = require('del');
const plugins = {}; const plugins = {};
plugins.addSrc = require('gulp-add-src'); plugins.addSrc = require('gulp-add-src');
plugins.babel = require('gulp-babel'); plugins.babel = require('gulp-babel');
plugins.base64 = require('gulp-base64-inline'); plugins.base64 = require('gulp-base64-inline');
plugins.rollup = require('gulp-better-rollup')
plugins.concat = require('gulp-concat'); plugins.concat = require('gulp-concat');
plugins.cssUrlAdjuster = require('gulp-css-url-adjuster'); plugins.cssUrlAdjuster = require('gulp-css-url-adjuster');
plugins.jshint = require('gulp-jshint'); plugins.jshint = require('gulp-jshint');
@@ -54,9 +54,18 @@ const copy = {
}; };
const bundleJavaScriptModules = async function () {
const bundle = await rollup.rollup({
input: paths.src + 'javascripts/modules/all.mjs', const javascripts = () => {
// JS from third-party sources
// We assume none of it will need to pass through Babel
const vendored = src(paths.src + 'javascripts/modules/all.mjs')
// Use Rollup to combine all JS in JS module format into a Immediately Invoked Function
// Expression (IIFE) to:
// - deliver it in one bundle
// - allow it to run in browsers without support for JS Modules
.pipe(plugins.rollup(
{
plugins: [ plugins: [
// determine module entry points from either 'module' or 'main' fields in package.json // determine module entry points from either 'module' or 'main' fields in package.json
rollupPluginNodeResolve({ rollupPluginNodeResolve({
@@ -68,20 +77,24 @@ const bundleJavaScriptModules = async function () {
include: 'node_modules/**' 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', format: 'iife',
name: 'GOVUK' name: 'GOVUK'
}); }
}; ))
// return a stream which pipes these files before the JS modules bundle
.pipe(plugins.addSrc.prepend([
paths.npm + 'hogan.js/dist/hogan-3.0.2.js',
paths.npm + 'jquery/dist/jquery.min.js',
paths.npm + 'query-command-supported/dist/queryCommandSupported.min.js',
paths.npm + 'diff-dom/diffDOM.js',
paths.npm + 'timeago/jquery.timeago.js',
paths.npm + 'textarea-caret/index.js'
]));
// JS local to this application
const javascripts = () => { const local = src([
return src([
paths.toolkit + 'javascripts/govuk/modules.js', paths.toolkit + 'javascripts/govuk/modules.js',
paths.toolkit + 'javascripts/govuk/show-hide-content.js', paths.toolkit + 'javascripts/govuk/show-hide-content.js',
paths.src + 'javascripts/govuk/cookie-functions.js', paths.src + 'javascripts/govuk/cookie-functions.js',
@@ -108,16 +121,11 @@ const javascripts = () => {
.pipe(plugins.prettyerror()) .pipe(plugins.prettyerror())
.pipe(plugins.babel({ .pipe(plugins.babel({
presets: ['@babel/preset-env'] presets: ['@babel/preset-env']
})) }));
.pipe(plugins.addSrc.prepend([
paths.npm + 'hogan.js/dist/hogan-3.0.2.js', // return single stream of all vinyl objects piped from the end of the vendored stream, then
paths.npm + 'jquery/dist/jquery.min.js', // those from the end of the local stream
paths.npm + 'query-command-supported/dist/queryCommandSupported.min.js', return streamqueue({ objectMode: true }, vendored, local)
paths.npm + 'diff-dom/diffDOM.js',
paths.npm + 'timeago/jquery.timeago.js',
paths.npm + 'textarea-caret/index.js',
paths.src + 'javascripts/modules/all.js'
]))
.pipe(plugins.uglify()) .pipe(plugins.uglify())
.pipe(plugins.concat('all.js')) .pipe(plugins.concat('all.js'))
.pipe(dest(paths.dist + 'javascripts/')) .pipe(dest(paths.dist + 'javascripts/'))
@@ -188,8 +196,7 @@ const lint = {
}, },
'js': (cb) => { 'js': (cb) => {
return src( return src(
paths.src + 'javascripts/**/*.js', paths.src + 'javascripts/**/*.js'
{ ignore: paths.src + 'javascripts/modules/*.js' } // ignore bundler boilerplate JS
) )
.pipe(plugins.jshint()) .pipe(plugins.jshint())
.pipe(plugins.jshint.reporter(stylish)) .pipe(plugins.jshint.reporter(stylish))
@@ -198,13 +205,6 @@ const lint = {
}; };
const clean = {
javascripts: (cb) => {
return del([paths.src + 'javascripts/modules/all.js'])
}
}
// Default: compile everything // Default: compile everything
const defaultTask = parallel( const defaultTask = parallel(
series( series(
@@ -214,9 +214,7 @@ const defaultTask = parallel(
series( series(
copy.error_pages, copy.error_pages,
series( series(
bundleJavaScriptModules, javascripts
javascripts,
clean.javascripts
), ),
sass sass
) )

View File

@@ -30,6 +30,7 @@
"gulp-add-src": "1.0.0", "gulp-add-src": "1.0.0",
"gulp-babel": "8.0.0", "gulp-babel": "8.0.0",
"gulp-base64-inline": "1.0.4", "gulp-base64-inline": "1.0.4",
"gulp-better-rollup": "4.0.1",
"gulp-concat": "2.6.1", "gulp-concat": "2.6.1",
"gulp-include": "2.3.1", "gulp-include": "2.3.1",
"gulp-sass": "4.0.2", "gulp-sass": "4.0.2",
@@ -38,6 +39,7 @@
"jquery": "3.4.1", "jquery": "3.4.1",
"query-command-supported": "1.0.0", "query-command-supported": "1.0.0",
"rollup": "1.23.1", "rollup": "1.23.1",
"streamqueue": "1.1.2",
"textarea-caret": "3.1.0", "textarea-caret": "3.1.0",
"timeago": "1.6.5" "timeago": "1.6.5"
}, },