Skip to content

Commit 85fe9e6

Browse files
author
jona
committed
Merge pull request #396 from nodejs/sup-buildjs-stuff
document build.js
2 parents 7bb4cb3 + c560eb0 commit 85fe9e6

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

build.js

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
#!/usr/bin/env node
1+
#! /usr/bin/env node
22

33
'use strict'
44

5+
// BUILD.JS: This file is responsible for building static HTML pages and a
6+
// server for local development.
7+
58
const Metalsmith = require('metalsmith')
69
const autoprefixer = require('autoprefixer-stylus')
710
const collections = require('metalsmith-collections')
@@ -23,20 +26,23 @@ const loadVersions = require('./scripts/load-versions')
2326
const latestVersion = require('./scripts/helpers/latestversion')
2427
const eventGeo = require('./scripts/event-geo.js')
2528

26-
/** Build **/
27-
28-
// load template.json for given language, but use default language as fallback
29-
// for properties which are not present in the given language
29+
// Set the default language, also functions as a fallback for properties which
30+
// are not defined in the given language.
3031
const DEFAULT_LANG = 'en'
3132

33+
// Set up the Markdown renderer that we'll use for our Metalsmith build process,
34+
// with the necessary adjustments that we need to make in order to have Prism
35+
// work.
3236
const renderer = new marked.Renderer()
3337
renderer.heading = anchorMarkdownHeadings
34-
3538
const markedOptions = {
3639
langPrefix: 'language-',
3740
renderer: renderer
3841
}
3942

43+
// This function imports a given language file and uses the default language set
44+
// in DEFAULT_LANG as a fallback to prevent any strings that aren't filled out
45+
// from appearing as blank.
4046
function i18nJSON (lang) {
4147
var defaultJSON = require(`./locale/${DEFAULT_LANG}/site.json`)
4248
var templateJSON = require(`./locale/${lang}/site.json`)
@@ -55,17 +61,24 @@ function i18nJSON (lang) {
5561
return finalJSON
5662
}
5763

64+
// This is the function where the actual magic happens. This contains the main
65+
// Metalsmith build cycle used for building a locale subsite, such as the
66+
// english one.
5867
function buildlocale (source, locale) {
5968
console.time('[metalsmith] build/' + locale + ' finished')
6069
const siteJSON = path.join(__dirname, 'locale', locale, 'site.json')
6170
const metalsmith = Metalsmith(__dirname)
6271
metalsmith
72+
// Sets global metadata imported from the locale's respective site.json.
6373
.metadata({
6474
site: require(siteJSON),
6575
project: source.project,
6676
i18n: i18nJSON(locale)
6777
})
78+
// Sets the build source as the locale folder.
6879
.source(path.join(__dirname, 'locale', locale))
80+
// Defines the blog post/guide collections used to internally group them for
81+
// easier future handling and feed generation.
6982
.use(collections({
7083
blog: {
7184
pattern: 'blog/**/*.md',
@@ -115,15 +128,20 @@ function buildlocale (source, locale) {
115128
}))
116129
.use(markdown(markedOptions))
117130
.use(prism())
131+
// Deletes Stylus partials since they'll be included in the main CSS file
132+
// anyways.
118133
.use(filterStylusPartials())
119134
.use(stylus({
120135
compress: true,
121136
paths: [path.join(__dirname, 'layouts', 'css')],
122137
use: [autoprefixer()]
123138
}))
139+
// Set pretty permalinks, we don't want .html suffixes everywhere.
124140
.use(permalinks({
125141
relative: false
126142
}))
143+
// Generates the feed XML files from their respective collections which were
144+
// defined earlier on.
127145
.use(feed({
128146
collection: 'blog',
129147
destination: 'feed/blog.xml',
@@ -149,6 +167,9 @@ function buildlocale (source, locale) {
149167
destination: 'feed/tsc-minutes.xml',
150168
title: 'Node.js Technical Steering Committee meetings'
151169
}))
170+
// Finally, this compiles the rest of the layouts present in ./layouts.
171+
// They're language-agnostic, but have to be regenerated for every locale
172+
// anyways.
152173
.use(layouts({
153174
engine: 'handlebars',
154175
pattern: '**/*.html',
@@ -162,14 +183,20 @@ function buildlocale (source, locale) {
162183
apidocslink: require('./scripts/helpers/apidocslink.js')
163184
}
164185
}))
186+
// Pipes the generated files into their respective subdirectory in the build
187+
// directory.
165188
.destination(path.join(__dirname, 'build', locale))
166189

190+
// This actually executes the build and stops the internal timer after
191+
// completion.
167192
metalsmith.build(function (err) {
168193
if (err) { throw err }
169194
console.timeEnd('[metalsmith] build/' + locale + ' finished')
170195
})
171196
}
172197

198+
// This function copies the rest of the static assets to their subfolder in the
199+
// build directory.
173200
function copystatic () {
174201
console.time('[metalsmith] build/static finished')
175202
fs.mkdir(path.join(__dirname, 'build'), function () {
@@ -183,8 +210,12 @@ function copystatic () {
183210
})
184211
}
185212

213+
// This is where the build is orchestrated from, as indicated by the function
214+
// name. It brings together all build steps and dependencies and executes them.
186215
function fullbuild () {
216+
// Copies static files.
187217
copystatic()
218+
// Loads all node/io.js versions.
188219
loadVersions(function (err, versions) {
189220
if (err) { throw err }
190221
const source = {
@@ -202,6 +233,7 @@ function fullbuild () {
202233
}
203234
}
204235

236+
// Executes the build cycle for every locale.
205237
fs.readdir(path.join(__dirname, 'locale'), function (e, locales) {
206238
locales.filter(junk.not).forEach(function (locale) {
207239
buildlocale(source, locale)
@@ -210,8 +242,10 @@ function fullbuild () {
210242
})
211243
}
212244

245+
// The server function, where the site is exposed through a static file server
246+
// locally.
213247
function server () {
214-
/** Static file server **/
248+
// Initializes the server and mounts it in the generated build directory.
215249
const st = require('st')
216250
const http = require('http')
217251
const mount = st({
@@ -225,7 +259,8 @@ function server () {
225259
function () { console.log('http://localhost:8080/en/') }
226260
)
227261

228-
/** File Watches for Re-Builds **/
262+
// Watches for file changes in the locale, layout and static directories, and
263+
// rebuilds the modified one.
229264
const chokidar = require('chokidar')
230265
const opts = {
231266
persistent: true,
@@ -242,6 +277,7 @@ function server () {
242277
const layouts = chokidar.watch(path.join(__dirname, 'layouts'), opts)
243278
const staticf = chokidar.watch(path.join(__dirname, 'static'), opts)
244279

280+
// Gets the locale name by path.
245281
function getlocale (p) {
246282
const pre = path.join(__dirname, 'locale')
247283
return p.slice(pre.length + 1, p.indexOf('/', pre.length + 1))
@@ -261,8 +297,10 @@ function server () {
261297
staticf.on('add', function (p) { staticf.add(p); copystatic() })
262298
}
263299

300+
// Starts the build.
264301
fullbuild()
265302

303+
// If the command-line option was provided, starts the static server.
266304
if (process.argv[2] === 'serve') {
267305
server()
268306
}

0 commit comments

Comments
 (0)