Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server.js: only build the CSS when CSS changes, not the layouts #2621

Merged
merged 6 commits into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function buildLocale (source, locale, opts) {
const labelForBuild = `[metalsmith] build/${locale} finished`
console.time(labelForBuild)
const metalsmith = Metalsmith(__dirname)

metalsmith
// Sets global metadata imported from the locale's respective site.json.
.metadata({
Expand Down Expand Up @@ -295,10 +296,6 @@ function getSource (callback) {
// name. It brings together all build steps and dependencies and executes them.
function fullBuild (opts) {
const { selectedLocales, preserveLocale } = opts
// Build static files.
copyStatic()
// Build CSS
buildCSS()
getSource((err, source) => {
if (err) { throw err }

Expand All @@ -320,11 +317,16 @@ function fullBuild (opts) {
if (require.main === module) {
const preserveLocale = process.argv.includes('--preserveLocale')
const selectedLocales = process.env.DEFAULT_LOCALE ? process.env.DEFAULT_LOCALE.toLowerCase().split(',') : process.env.DEFAULT_LOCALE
// Copy static files
copyStatic()
// Build CSS
buildCSS()
fullBuild({ selectedLocales, preserveLocale })
}

exports.getSource = getSource
exports.fullBuild = fullBuild
exports.buildCSS = buildCSS
exports.buildLocale = buildLocale
exports.copyStatic = copyStatic
exports.generateLocalesData = generateLocalesData
46 changes: 30 additions & 16 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
// The server where the site is exposed through a static file server
// while developing locally.

const path = require('path')
const st = require('st')
const fs = require('fs')
const http = require('http')
const path = require('path')
const chokidar = require('chokidar')
const junk = require('junk')
const st = require('st')
const build = require('./build')

const mount = st({
path: path.join(__dirname, 'build'),
cache: false,
index: 'index.html',
passthrough: true
})

const build = require('./build')
const fs = require('fs')
const port = process.env.PORT || 8080
const junk = require('junk')

const selectedLocales = process.env.DEFAULT_LOCALE ? process.env.DEFAULT_LOCALE.toLowerCase().split(',') : process.env.DEFAULT_LOCALE
const preserveLocale = process.argv.includes('--preserveLocale')
const serveOnly = process.argv.includes('--serve-only')
Expand All @@ -30,8 +30,9 @@ const opts = {
usePolling: true
}
const locales = chokidar.watch(path.join(__dirname, 'locale'), opts)
const layouts = chokidar.watch(path.join(__dirname, 'layouts'), opts)
const statics = chokidar.watch(path.join(__dirname, 'static'), opts)
const css = chokidar.watch(path.join(__dirname, 'layouts/css/**/*.styl'), opts)
const layouts = chokidar.watch(path.join(__dirname, 'layouts/**/*.hbs'), opts)
const staticFiles = chokidar.watch(path.join(__dirname, 'static'), opts)

// Gets the locale name by path.
function getLocale (filePath) {
Expand All @@ -44,10 +45,11 @@ function getLocale (filePath) {
// 2. Choose what languages for the menu.
function dynamicallyBuildOnLanguages (source, locale) {
if (!selectedLocales || selectedLocales.length === 0) {
fs.readdir(path.join(__dirname, 'locale'), (e, locales) => {
if (e) {
throw e
fs.readdir(path.join(__dirname, 'locale'), (err, locales) => {
if (err) {
throw err
}

const filteredLocales = locales.filter(file => junk.not(file))
const localesData = build.generateLocalesData(filteredLocales)
build.buildLocale(source, locale, { preserveLocale, localesData })
Expand All @@ -59,7 +61,9 @@ function dynamicallyBuildOnLanguages (source, locale) {
}

build.getSource((err, source) => {
if (err) { throw err }
if (err) {
throw err
}

locales.on('change', (filePath) => {
const locale = getLocale(filePath)
Expand All @@ -81,15 +85,21 @@ build.getSource((err, source) => {
})
})

css.on('change', () => build.buildCSS())
css.on('add', (filePath) => {
css.add(filePath)
build.buildCSS()
})

layouts.on('change', () => build.fullBuild({ selectedLocales, preserveLocale }))
layouts.on('add', (filePath) => {
layouts.add(filePath)
build.fullBuild({ selectedLocales, preserveLocale })
})

statics.on('change', build.copyStatic)
statics.on('add', (filePath) => {
statics.add(filePath)
staticFiles.on('change', build.copyStatic)
staticFiles.on('add', (filePath) => {
staticFiles.add(filePath)
build.copyStatic()
})

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

if (!serveOnly) {
// Start the initial build of static HTML pages
build.copyStatic()
build.buildCSS()
build.fullBuild({ selectedLocales, preserveLocale })
}