Skip to content

Commit d6162d0

Browse files
Add build script to kit (using esbuild) (#96)
This adds the compiling of SASS and javascript to the kit, using `esbuild`. It's an optional extra, enabled by adding an `buildOptions` config to the `init` function with references to the files you’d like to build: For example, to compile SASS only: ```js NHSPrototypeKit.init({ buildOptions: { entryPoints: ['assets/css/*.scss'] }, ... }) ``` or for both SASS and javascript: ```js NHSPrototypeKit.init({ buildOptions: { entryPoints: ['assets/css/*.scss', 'assets/javascript/*.js'] }, ... }) ``` It could also be run standalone by using this async function: ```js await NHSPrototypeKit.build(true, {entryPoints: ['assets/css/*.scss']}) ``` This is a replacement for `gulpfile.js` in the existing NHS prototype kit.
1 parent c517d38 commit d6162d0

File tree

12 files changed

+4508
-2921
lines changed

12 files changed

+4508
-2921
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ Thumbs.db
1717

1818
# ESlint cache
1919
.cache
20+
21+
# Compiled assets for the test app
22+
testapp/public

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ const prototype = NHSPrototypeKit.init({
3939
express: app,
4040
nunjucks: nunjucks,
4141
routes: routes,
42-
sessionDataDefaults: sessionDataDefaults
42+
sessionDataDefaults: sessionDataDefaults,
43+
buildOptions: {
44+
entryPoints: ['assets/sass/*.scss', 'assets/javascript/*.js']
45+
}
4346
})
4447
```
4548

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ export default defineConfig([
169169
globalIgnores([
170170
'**/app/**',
171171
'**/public/**',
172+
'testapp/assets/javascript/application.js',
172173

173174
// Enable dotfile linting
174175
'!.*',

lib/build/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const esbuild = require('esbuild')
2+
const { sassPlugin } = require('esbuild-sass-plugin')
3+
4+
const build = async function (watch, options = {}) {
5+
const sassLoadPaths = options.sassLoadPaths || ['node_modules']
6+
7+
const esbuildOptions = {
8+
entryPoints: options.entryPoints,
9+
entryNames: '[name]',
10+
bundle: true,
11+
legalComments: 'none',
12+
minify: true,
13+
outdir: 'public',
14+
plugins: [
15+
sassPlugin({
16+
loadPaths: sassLoadPaths,
17+
silenceDeprecations: ['import'],
18+
quietDeps: true
19+
})
20+
],
21+
sourcemap: true
22+
}
23+
24+
try {
25+
if (watch) {
26+
const ctx = await esbuild.context(esbuildOptions)
27+
await ctx.watch()
28+
} else {
29+
await esbuild.build(esbuildOptions)
30+
}
31+
} catch (error) {
32+
console.error(error)
33+
}
34+
}
35+
36+
module.exports = build

lib/index.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
const { join } = require('node:path')
22

3+
const build = require('./build')
34
const expressMiddleware = require('./express-middleware')
45
const expressSettings = require('./express-settings')
56
const nunjucksFilters = require('./nunjucks-filters')
67
const utils = require('./utils')
78

8-
function NHSPrototypeKit({ express, nunjucks, config }) {
9+
function NHSPrototypeKit({ express, nunjucks, config, buildOptions }) {
910
this.express = express
1011
this.nunjucks = nunjucks
1112
this.config = config
13+
this.buildOptions = buildOptions
1214
}
1315

1416
NHSPrototypeKit.nunjucksFilters = nunjucksFilters
@@ -41,7 +43,8 @@ NHSPrototypeKit.init = function (options) {
4143
return new NHSPrototypeKit({
4244
express: options.express,
4345
nunjucks: options.nunjucks,
44-
config: options.config
46+
config: options.config,
47+
buildOptions: options.buildOptions
4548
})
4649
}
4750

@@ -69,6 +72,9 @@ NHSPrototypeKit.prototype.start = async function (port) {
6972
return
7073
}
7174

75+
// Build the frontend assets (if configured)
76+
await this.build()
77+
7278
this.express.listen(availablePort, () => {
7379
if (nodeEnv === 'production') {
7480
console.info(`Running on port ${availablePort}`)
@@ -78,4 +84,18 @@ NHSPrototypeKit.prototype.start = async function (port) {
7884
})
7985
}
8086

87+
// This function can be used to compile the frontend assets
88+
NHSPrototypeKit.prototype.build = async function () {
89+
const nodeEnv = process.env.NODE_ENV || 'development'
90+
91+
if (this.buildOptions) {
92+
const watch = nodeEnv !== 'production'
93+
try {
94+
await build(watch, this.buildOptions)
95+
} catch (error) {
96+
console.log(error)
97+
}
98+
}
99+
}
100+
81101
module.exports = NHSPrototypeKit

0 commit comments

Comments
 (0)