Skip to content

Commit 865a89d

Browse files
author
Maledong
authored
fixture: watch cannot build files changed or added successfully (#2457)
1. For function 'fullBuild', the first statement 'opts' can be divided into two parameters called "selectedLocales" and "reserveLocale", the order isn't right, according to the caller of this function itself (See its calling at server.js by 'build.fullBuild' and other related functions). So switch them together. 2. Fix #2443 (comment) for watch, because we didn't pass the parameter into the buildLocale, there'll be Unhandled Exception.
1 parent c54ff3c commit 865a89d

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

build.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ function getSource (callback) {
310310
// This is where the build is orchestrated from, as indicated by the function
311311
// name. It brings together all build steps and dependencies and executes them.
312312
function fullBuild (opts) {
313-
const { preserveLocale, selectedLocales } = opts
313+
const { selectedLocales, preserveLocale } = opts
314314
// Build static files.
315315
copyStatic()
316316
// Build layouts
@@ -340,3 +340,4 @@ exports.getSource = getSource
340340
exports.fullBuild = fullBuild
341341
exports.buildLocale = buildLocale
342342
exports.copyStatic = copyStatic
343+
exports.generateLocalesData = generateLocalesData

server.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ const mount = st({
1515
})
1616

1717
const build = require('./build')
18-
18+
const fs = require('fs')
1919
const port = process.env.PORT || 8080
20+
const junk = require('junk')
2021

2122
const selectedLocales = process.env.DEFAULT_LOCALE ? process.env.DEFAULT_LOCALE.toLowerCase().split(',') : process.env.DEFAULT_LOCALE
2223
const preserveLocale = process.argv.includes('--preserveLocale')
@@ -44,19 +45,40 @@ function getLocale (filePath) {
4445
return filePath.slice(pre.length + 1, filePath.indexOf(path.sep, pre.length + 1))
4546
}
4647

48+
// This function has two meanings:
49+
// 1. Build for the specific language.
50+
// 2. Choose what languages for the menu.
51+
function dynamicallyBuildOnLanguages (source, locale) {
52+
if (!selectedLocales || selectedLocales.length === 0) {
53+
fs.readdir(path.join(__dirname, 'locale'), (e, locales) => {
54+
const filteredLocales = locales.filter(file => junk.not(file))
55+
const localesData = build.generateLocalesData(filteredLocales)
56+
build.buildLocale(source, locale, { preserveLocale, localesData })
57+
})
58+
} else {
59+
const localesData = build.generateLocalesData(selectedLocales)
60+
build.buildLocale(source, locale, { preserveLocale, localesData })
61+
}
62+
}
63+
4764
build.getSource((err, source) => {
4865
if (err) { throw err }
4966

5067
locales.on('change', (filePath) => {
5168
const locale = getLocale(filePath)
69+
5270
if (!selectedLocales || selectedLocales.includes(locale)) {
53-
build.buildLocale(source, locale)
71+
console.log(`The language ${locale} is changed, file is ${filePath}.`)
72+
dynamicallyBuildOnLanguages(source, locale)
5473
}
5574
})
75+
5676
locales.on('add', (filePath) => {
5777
const locale = getLocale(filePath)
78+
5879
if (!selectedLocales || selectedLocales.includes(locale)) {
59-
build.buildLocale(source, locale)
80+
console.log(`The language ${locale} is changed, file is ${filePath}.`)
81+
dynamicallyBuildOnLanguages(source, locale)
6082
locales.add(filePath)
6183
}
6284
})

0 commit comments

Comments
 (0)