Skip to content

Commit 1d4eebb

Browse files
Migrate gulp tasks to ES modules
1 parent e78fade commit 1d4eebb

File tree

6 files changed

+60
-90
lines changed

6 files changed

+60
-90
lines changed

gulpfile.js renamed to gulpfile.mjs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
const browserSync = require('browser-sync')
2-
const gulp = require('gulp')
1+
import browserSync from 'browser-sync'
2+
import gulp from 'gulp'
33

4-
const {
4+
import {
55
buildHTML,
66
validateHTML,
77
copyCSS,
88
copyJS,
99
copyBinaryAssets,
1010
serve
11-
} = require('./shared/tasks/app')
12-
const { clean } = require('./shared/tasks/clean')
13-
const {
11+
} from './shared/tasks/app.mjs'
12+
import { clean } from './shared/tasks/clean.mjs'
13+
import {
1414
assets,
1515
cssFolder,
1616
jsFolder,
1717
createZip
18-
} = require('./shared/tasks/release')
19-
const { webpackJS, minifyJS } = require('./shared/tasks/scripts')
20-
const { compileCSS, minifyCSS } = require('./shared/tasks/styles')
18+
} from './shared/tasks/release.mjs'
19+
import { webpackJS, minifyJS } from './shared/tasks/scripts.mjs'
20+
import { compileCSS, minifyCSS } from './shared/tasks/styles.mjs'
2121

2222
/**
2323
* Development tasks

shared/tasks/app.js renamed to shared/tasks/app.mjs

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
const { mkdir, writeFile } = require('fs/promises')
2-
const { join, parse } = require('path')
1+
import { mkdir, writeFile } from 'fs/promises'
2+
import { join, parse } from 'path'
33

4-
const browserSync = require('browser-sync')
5-
const { glob } = require('glob')
6-
const gulp = require('gulp')
7-
const { HtmlValidate, formatterFactory } = require('html-validate')
8-
const nunjucks = require('nunjucks')
9-
const PluginError = require('plugin-error')
4+
import browserSync from 'browser-sync'
5+
import { glob } from 'glob'
6+
import gulp from 'gulp'
7+
import { HtmlValidate, formatterFactory } from 'html-validate'
8+
import nunjucks from 'nunjucks'
9+
import PluginError from 'plugin-error'
1010

11-
const validatorConfig = require('../../.htmlvalidate')
12-
const { version } = require('../../package.json')
11+
import validatorConfig from '../../.htmlvalidate.js'
12+
import pkg from '../../package.json' with { type: 'json' }
1313

1414
const { PORT = 3000 } = process.env
1515

1616
/**
1717
* Compile Nunjucks into HTML
1818
*/
19-
async function buildHTML() {
19+
export async function buildHTML() {
2020
const paths = await glob('**/*.njk', {
2121
cwd: 'app',
2222
nodir: true
@@ -34,7 +34,7 @@ async function buildHTML() {
3434
const html = env.render(path, {
3535
assetPath: `/nhsuk-frontend/assets`,
3636
baseUrl: '/nhsuk-frontend/',
37-
version
37+
version: pkg.version
3838
})
3939

4040
const destPath = join('dist/app', dir)
@@ -49,7 +49,7 @@ async function buildHTML() {
4949
/**
5050
* Validate Nunjucks HTML output
5151
*/
52-
async function validateHTML() {
52+
export async function validateHTML() {
5353
const paths = await glob('dist/app/**/*.html', {
5454
nodir: true
5555
})
@@ -74,7 +74,7 @@ async function validateHTML() {
7474
/**
7575
* Copy CSS from dist into the documentation directory
7676
*/
77-
function copyCSS() {
77+
export async function copyCSS() {
7878
return gulp
7979
.src('dist/*.min.{css,css.map}')
8080
.pipe(gulp.dest('dist/app/stylesheets'))
@@ -84,7 +84,7 @@ function copyCSS() {
8484
/**
8585
* Copy JS from dist into the documentation directory
8686
*/
87-
function copyJS() {
87+
export async function copyJS() {
8888
return gulp
8989
.src('dist/*.min.{js,js.map}')
9090
.pipe(gulp.dest('dist/app/javascripts'))
@@ -94,7 +94,7 @@ function copyJS() {
9494
/**
9595
* Copy logos, icons and other binary assets
9696
*/
97-
function copyBinaryAssets() {
97+
export async function copyBinaryAssets() {
9898
return gulp
9999
.src('packages/assets/**', { encoding: false })
100100
.pipe(gulp.dest('dist/app/assets'))
@@ -104,7 +104,7 @@ function copyBinaryAssets() {
104104
/**
105105
* Serve the static docs directory over localhost
106106
*/
107-
function serve() {
107+
export function serve() {
108108
return browserSync({
109109
ghostMode: false,
110110
host: '0.0.0.0',
@@ -143,15 +143,6 @@ function serve() {
143143
})
144144
}
145145

146-
module.exports = {
147-
buildHTML,
148-
validateHTML,
149-
copyCSS,
150-
copyJS,
151-
copyBinaryAssets,
152-
serve
153-
}
154-
155146
/**
156147
* @import { Result } from 'html-validate'
157148
*/

shared/tasks/clean.js renamed to shared/tasks/clean.mjs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
* @param {string[]} pattern - glob patterns or paths to clean
44
* @param {string[]} [ignore] - glob patterns or paths to ignore
55
*/
6-
async function clean(pattern, ignore) {
6+
export async function clean(pattern, ignore) {
77
const { deleteAsync } = await import('del')
88
await deleteAsync(pattern, { ignore })
99
}
10-
11-
module.exports = {
12-
clean
13-
}
Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const gulp = require('gulp')
1+
import gulp from 'gulp'
22

3-
const { version } = require('../../package.json')
3+
import pkg from '../../package.json' with { type: 'json' }
44

55
/**
66
* Assets tasks
@@ -9,7 +9,7 @@ const { version } = require('../../package.json')
99
/**
1010
* Copy assets such as icons and images into the distribution
1111
*/
12-
function assets() {
12+
export function assets() {
1313
return gulp
1414
.src('packages/assets/**', { encoding: false })
1515
.pipe(gulp.dest('dist/assets/'))
@@ -20,16 +20,16 @@ function assets() {
2020
*/
2121

2222
/* Copy JS files into their relevant folders */
23-
function jsFolder() {
23+
export function jsFolder() {
2424
return gulp.src('dist/*.min.{js,js.map}').pipe(gulp.dest('dist/js/'))
2525
}
2626

2727
/* Copy CSS files into their relevant folders */
28-
function cssFolder() {
28+
export function cssFolder() {
2929
return gulp.src('dist/*.min.{css,css.map}').pipe(gulp.dest('dist/css/'))
3030
}
3131

32-
async function createZip() {
32+
export async function createZip() {
3333
const { default: zip } = await import('gulp-zip')
3434

3535
return gulp
@@ -44,13 +44,6 @@ async function createZip() {
4444
encoding: false
4545
}
4646
)
47-
.pipe(zip(`nhsuk-frontend-${version}.zip`))
47+
.pipe(zip(`nhsuk-frontend-${pkg.version}.zip`))
4848
.pipe(gulp.dest('dist'))
4949
}
50-
51-
module.exports = {
52-
assets,
53-
jsFolder,
54-
cssFolder,
55-
createZip
56-
}

shared/tasks/scripts.js renamed to shared/tasks/scripts.mjs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
const { join, relative } = require('path')
2-
const { cwd } = require('process')
1+
import { join, relative } from 'path'
2+
import { cwd } from 'process'
33

4-
const gulp = require('gulp')
5-
const rename = require('gulp-rename')
6-
const terser = require('gulp-terser')
7-
const PluginError = require('plugin-error')
8-
const webpack = require('webpack-stream')
4+
import gulp from 'gulp'
5+
import rename from 'gulp-rename'
6+
import terser from 'gulp-terser'
7+
import PluginError from 'plugin-error'
8+
import webpack from 'webpack-stream'
99

10-
const { version } = require('../../package.json')
10+
import pkg from '../../package.json' with { type: 'json' }
1111

1212
/**
1313
* Compile JavaScript task
1414
*/
15-
function webpackJS(done) {
15+
export function webpackJS(done) {
1616
return gulp
1717
.src('./packages/nhsuk.js', {
1818
sourcemaps: true
@@ -67,7 +67,7 @@ function webpackJS(done) {
6767
/**
6868
* Minify JavaScript task
6969
*/
70-
function minifyJS() {
70+
export function minifyJS() {
7171
return gulp
7272
.src(
7373
[
@@ -90,7 +90,7 @@ function minifyJS() {
9090
)
9191
.pipe(
9292
rename({
93-
suffix: `-${version}.min`
93+
suffix: `-${pkg.version}.min`
9494
})
9595
)
9696
.pipe(
@@ -99,8 +99,3 @@ function minifyJS() {
9999
})
100100
)
101101
}
102-
103-
module.exports = {
104-
webpackJS,
105-
minifyJS
106-
}

shared/tasks/styles.js renamed to shared/tasks/styles.mjs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
const { join, relative } = require('path')
2-
const { cwd } = require('process')
3-
const { Transform } = require('stream')
1+
import { join, relative } from 'path'
2+
import { cwd } from 'process'
3+
import { Transform } from 'stream'
44

5-
const autoprefixer = require('autoprefixer')
6-
const cssnano = require('cssnano')
7-
const gulp = require('gulp')
8-
const postcss = require('gulp-postcss')
9-
const rename = require('gulp-rename')
10-
const gulpSass = require('gulp-sass')
11-
const PluginError = require('plugin-error')
12-
const dartSass = require('sass-embedded')
5+
import autoprefixer from 'autoprefixer'
6+
import cssnano from 'cssnano'
7+
import gulp from 'gulp'
8+
import postcss from 'gulp-postcss'
9+
import rename from 'gulp-rename'
10+
import gulpSass from 'gulp-sass'
11+
import PluginError from 'plugin-error'
12+
import dartSass from 'sass-embedded'
1313

14-
const { version } = require('../../package.json')
14+
import pkg from '../../package.json' with { type: 'json' }
1515

1616
const sass = gulpSass(dartSass)
1717

1818
/**
1919
* Compile Sass task
2020
*/
21-
function compileCSS(done) {
21+
export function compileCSS(done) {
2222
return gulp
2323
.src(['packages/nhsuk.scss'], {
2424
sourcemaps: true
@@ -62,7 +62,7 @@ function compileCSS(done) {
6262
/**
6363
* Minify CSS task
6464
*/
65-
function minifyCSS() {
65+
export function minifyCSS() {
6666
return gulp
6767
.src(
6868
[
@@ -74,7 +74,7 @@ function minifyCSS() {
7474
.pipe(postcss([cssnano()]))
7575
.pipe(
7676
rename({
77-
suffix: `-${version}.min`
77+
suffix: `-${pkg.version}.min`
7878
})
7979
)
8080
.pipe(
@@ -83,8 +83,3 @@ function minifyCSS() {
8383
})
8484
)
8585
}
86-
87-
module.exports = {
88-
compileCSS,
89-
minifyCSS
90-
}

0 commit comments

Comments
 (0)