Skip to content

Commit 7a537de

Browse files
XhmikosRTrott
authored andcommitted
server.js: only build the CSS when CSS changes, not the layouts (#2621)
1 parent 1011be5 commit 7a537de

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

build.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ function buildLocale (source, locale, opts) {
7070
const labelForBuild = `[metalsmith] build/${locale} finished`
7171
console.time(labelForBuild)
7272
const metalsmith = Metalsmith(__dirname)
73+
7374
metalsmith
7475
// Sets global metadata imported from the locale's respective site.json.
7576
.metadata({
@@ -295,10 +296,6 @@ function getSource (callback) {
295296
// name. It brings together all build steps and dependencies and executes them.
296297
function fullBuild (opts) {
297298
const { selectedLocales, preserveLocale } = opts
298-
// Build static files.
299-
copyStatic()
300-
// Build CSS
301-
buildCSS()
302299
getSource((err, source) => {
303300
if (err) { throw err }
304301

@@ -320,11 +317,16 @@ function fullBuild (opts) {
320317
if (require.main === module) {
321318
const preserveLocale = process.argv.includes('--preserveLocale')
322319
const selectedLocales = process.env.DEFAULT_LOCALE ? process.env.DEFAULT_LOCALE.toLowerCase().split(',') : process.env.DEFAULT_LOCALE
320+
// Copy static files
321+
copyStatic()
322+
// Build CSS
323+
buildCSS()
323324
fullBuild({ selectedLocales, preserveLocale })
324325
}
325326

326327
exports.getSource = getSource
327328
exports.fullBuild = fullBuild
329+
exports.buildCSS = buildCSS
328330
exports.buildLocale = buildLocale
329331
exports.copyStatic = copyStatic
330332
exports.generateLocalesData = generateLocalesData

server.js

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
// The server where the site is exposed through a static file server
44
// while developing locally.
55

6-
const path = require('path')
7-
const st = require('st')
6+
const fs = require('fs')
87
const http = require('http')
8+
const path = require('path')
99
const chokidar = require('chokidar')
10+
const junk = require('junk')
11+
const st = require('st')
12+
const build = require('./build')
13+
1014
const mount = st({
1115
path: path.join(__dirname, 'build'),
1216
cache: false,
1317
index: 'index.html',
1418
passthrough: true
1519
})
1620

17-
const build = require('./build')
18-
const fs = require('fs')
1921
const port = process.env.PORT || 8080
20-
const junk = require('junk')
21-
2222
const selectedLocales = process.env.DEFAULT_LOCALE ? process.env.DEFAULT_LOCALE.toLowerCase().split(',') : process.env.DEFAULT_LOCALE
2323
const preserveLocale = process.argv.includes('--preserveLocale')
2424
const serveOnly = process.argv.includes('--serve-only')
@@ -30,8 +30,9 @@ const opts = {
3030
usePolling: true
3131
}
3232
const locales = chokidar.watch(path.join(__dirname, 'locale'), opts)
33-
const layouts = chokidar.watch(path.join(__dirname, 'layouts'), opts)
34-
const statics = chokidar.watch(path.join(__dirname, 'static'), opts)
33+
const css = chokidar.watch(path.join(__dirname, 'layouts/css/**/*.styl'), opts)
34+
const layouts = chokidar.watch(path.join(__dirname, 'layouts/**/*.hbs'), opts)
35+
const staticFiles = chokidar.watch(path.join(__dirname, 'static'), opts)
3536

3637
// Gets the locale name by path.
3738
function getLocale (filePath) {
@@ -44,10 +45,11 @@ function getLocale (filePath) {
4445
// 2. Choose what languages for the menu.
4546
function dynamicallyBuildOnLanguages (source, locale) {
4647
if (!selectedLocales || selectedLocales.length === 0) {
47-
fs.readdir(path.join(__dirname, 'locale'), (e, locales) => {
48-
if (e) {
49-
throw e
48+
fs.readdir(path.join(__dirname, 'locale'), (err, locales) => {
49+
if (err) {
50+
throw err
5051
}
52+
5153
const filteredLocales = locales.filter(file => junk.not(file))
5254
const localesData = build.generateLocalesData(filteredLocales)
5355
build.buildLocale(source, locale, { preserveLocale, localesData })
@@ -59,7 +61,9 @@ function dynamicallyBuildOnLanguages (source, locale) {
5961
}
6062

6163
build.getSource((err, source) => {
62-
if (err) { throw err }
64+
if (err) {
65+
throw err
66+
}
6367

6468
locales.on('change', (filePath) => {
6569
const locale = getLocale(filePath)
@@ -81,15 +85,21 @@ build.getSource((err, source) => {
8185
})
8286
})
8387

88+
css.on('change', () => build.buildCSS())
89+
css.on('add', (filePath) => {
90+
css.add(filePath)
91+
build.buildCSS()
92+
})
93+
8494
layouts.on('change', () => build.fullBuild({ selectedLocales, preserveLocale }))
8595
layouts.on('add', (filePath) => {
8696
layouts.add(filePath)
8797
build.fullBuild({ selectedLocales, preserveLocale })
8898
})
8999

90-
statics.on('change', build.copyStatic)
91-
statics.on('add', (filePath) => {
92-
statics.add(filePath)
100+
staticFiles.on('change', build.copyStatic)
101+
staticFiles.on('add', (filePath) => {
102+
staticFiles.add(filePath)
93103
build.copyStatic()
94104
})
95105

@@ -104,9 +114,13 @@ http.createServer((req, res) => {
104114
req.url = `/${mainLocale}`
105115
}
106116
mount(req, res)
107-
}).listen(port, () => console.log(`\x1B[32mServer running at http://localhost:${port}/${mainLocale}/\x1B[39m`))
117+
}).listen(port, () => {
118+
console.log(`\x1B[32mServer running at http://localhost:${port}/${mainLocale}/\x1B[39m`)
119+
})
108120

109121
if (!serveOnly) {
110122
// Start the initial build of static HTML pages
123+
build.copyStatic()
124+
build.buildCSS()
111125
build.fullBuild({ selectedLocales, preserveLocale })
112126
}

0 commit comments

Comments
 (0)