-
Notifications
You must be signed in to change notification settings - Fork 247
/
Copy pathmetalsmith.js
235 lines (193 loc) · 6.22 KB
/
metalsmith.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
const { readFileSync } = require('fs')
const { dirname, join, resolve } = require('path')
// Third party metalsmith plugins and utilities
const inPlace = require('@metalsmith/in-place') // render templating syntax in source files
const layouts = require('@metalsmith/layouts') // apply layouts to source files
const permalinks = require('@metalsmith/permalinks') // apply a permalink pattern to files
const postcss = require('@metalsmith/postcss')
const sass = require('@metalsmith/sass') // convert Sass files to CSS using Dart Sass
const { glob } = require('glob') // Match files using glob patterns
const Metalsmith = require('metalsmith') // static site generator
const canonical = require('metalsmith-canonical') // add a canonical url property to pages
// Helpers and config
const { paths, navigation: menuItems } = require('../config')
const colours = require('../lib/colours.js') // get colours data
const nunjucksOptions = require('../lib/nunjucks/index.js') // nunjucks options
// Local metalsmith plugins
const { hashAssets } = require('./fingerprints') // rename files with hash fingerprints
const generateSitemap = require('./generate-sitemap.js') // generate sitemap
const lunr = require('./metalsmith-lunr-index') // generate search index
const renderMarkdown = require('./metalsmith-render-markdown')
const titleChecker = require('./metalsmith-title-checker.js')
const navigation = require('./navigation.js') // navigation plugin
const rollup = require('./rollup') // used to build GOV.UK Frontend JavaScript
// Static site generator
const metalsmith = Metalsmith(resolve(__dirname, '../'))
// Flag production mode (to skip plugins in development)
const isProduction = process.env.NODE_ENV !== 'development'
module.exports = metalsmith
// notify build starting
.use(
(files, metalsmith) =>
metalsmith.watch() &&
metalsmith.debug('build').info('Metalsmith build running')
)
// source directory
.source(paths.source)
// destination directory
.destination(paths.public)
// clean destination for production builds
.clean(isProduction)
// enable plugin optimisations in production etc
.env({
DEBUG: process.env.DEBUG,
// Node.js build environment
NODE_ENV: process.env.NODE_ENV ?? 'production',
// Netlify deploy context
// https://docs.netlify.com/site-deploys/overview/#deploy-contexts
CONTEXT: process.env.CONTEXT ?? 'production',
// Netlify variables for preview banner
BRANCH: process.env.BRANCH,
REVIEW_ID: process.env.REVIEW_ID,
PULL_REQUEST: process.env.PULL_REQUEST
})
// global variables used in layout files
.metadata({
title: '[TITLE NOT SET]',
colours,
// include access to metalsmith environment variables
// used to e.g. detect when we're building in a preview environment
env: (value) => metalsmith.env(value)
})
// ignore files from build
.ignore(['.DS_Store', '.eslintrc.js', 'tsconfig.json'])
// convert *.scss files to *.css
.use(
sass({
quietDeps: true,
sourceMapIncludeSources: true,
sourceMap: true,
// Resolve @imports via
loadPaths: [
join(paths.root, 'node_modules'),
join(paths.source, 'stylesheets'),
// Path to `govuk-frontend` export without `govuk/` suffix
join(dirname(require.resolve('govuk-frontend')), '../')
]
})
)
.use(
postcss({
plugins: {
// Add vendor prefixes
autoprefixer: {
env: 'stylesheets'
}
},
map: {
inline: false
}
})
)
.use(async (files, metalsmith, done) => {
async function copyAssets(pattern, options) {
const assets = await glob(pattern, options)
for (const asset of assets) {
const input = join(options.cwd, asset)
const output = join(options.dest, asset)
files[output] = {
contents: readFileSync(input)
}
}
}
await Promise.all([
copyAssets('{fonts/*,images/*,manifest.json}', {
cwd: join(dirname(require.resolve('govuk-frontend')), 'assets'),
dest: 'assets'
}),
copyAssets('govuk-frontend.min.css?(.map)', {
cwd: dirname(require.resolve('govuk-frontend')),
dest: 'stylesheets'
})
])
done()
})
// build the entrypoints for application specific JavaScript
.use(rollup('javascripts/application.mjs'))
.use(rollup('javascripts/application-example.mjs'))
// add hash to files in production
.use((files, metalsmith, done) => {
if (!isProduction) {
return done()
}
return hashAssets({
pattern: [
'**/*.css?(.map)',
'javascripts/*.{cjs,js,mjs}?(.map)',
'javascripts/vendor/*'
]
})(files, metalsmith, done)
})
// check titles are set
.use(titleChecker())
// render templating syntax in source files
.use(
inPlace({
pattern: '**/*.{md,njk}',
transform: 'jstransformer-nunjucks',
engineOptions: nunjucksOptions
})
)
// render markdown in source files and extract page headings
.use(renderMarkdown())
// apply a permalink pattern to files
.use(permalinks())
// add a canonical url property to pages
.use(
canonical({
hostname: 'https://design-system.service.gov.uk',
omitIndex: true,
omitTrailingSlashes: false
})
)
// apply navigation
.use(
navigation({
items: menuItems
})
)
// generate a search index
.use(lunr())
// add hash to search index in production
// we can't add it earlier with the rest
// as we can only generate it just above
.use((files, metalsmith, done) => {
if (!isProduction) {
return done()
}
return hashAssets({
pattern: ['search-index.json']
})(files, metalsmith, done)
})
// apply layouts to source files
.use(
layouts({
default: 'layout.njk',
directory: join(paths.views, 'layouts'),
pattern: '**/*.html',
engineOptions: nunjucksOptions
})
)
// generate a sitemap.xml in public/ folder
.use(
generateSitemap({
hostname: 'https://design-system.service.gov.uk',
pattern: ['**/*.html', '!**/default/*.html']
})
)
// notify build complete
.use(
(files, metalsmith) =>
metalsmith.watch() &&
metalsmith.debug('build').info('Metalsmith build complete')
)