Skip to content

Commit b17295d

Browse files
committed
Fixes #91
1 parent c21fe7b commit b17295d

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

src/Eleventy.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,27 @@ Eleventy.prototype.getHelp = function() {
177177
return out.join("\n");
178178
};
179179

180-
Eleventy.prototype._watch = async function() {
180+
Eleventy.prototype._watch = async function(path) {
181181
if (this.active) {
182-
this.queuedToRun = true;
182+
this.queuedToRun = path;
183183
return;
184184
}
185185

186186
this.active = true;
187+
188+
// reset and reload global configuration :O
189+
if (path === config.getLocalProjectConfigFile()) {
190+
config.reset();
191+
}
192+
187193
this.restart();
188194
await this.write();
189195
this.active = false;
190196

191197
if (this.queuedToRun) {
192198
console.log("You saved while Eleventy was running, let’s run again.");
193199
this.queuedToRun = false;
194-
await this._watch();
200+
await this._watch(this.queuedToRun);
195201
} else {
196202
console.log("Watching…");
197203
}
@@ -205,24 +211,29 @@ Eleventy.prototype.watch = async function() {
205211

206212
const watch = require("glob-watcher");
207213

214+
let rawFiles = this.writer.getRawFiles();
215+
// Watch the local project config file
216+
rawFiles.push(config.getLocalProjectConfigFile());
217+
debug("Eleventy.watch rawFiles: %o", rawFiles);
218+
208219
console.log("Watching…");
209-
let watcher = watch(this.writer.getRawFiles(), {
220+
let watcher = watch(rawFiles, {
210221
ignored: this.writer.getWatchedIgnores()
211222
});
212223

213224
watcher.on(
214225
"change",
215226
async function(path, stat) {
216227
console.log("File changed:", path);
217-
this._watch();
228+
this._watch(path);
218229
}.bind(this)
219230
);
220231

221232
watcher.on(
222233
"add",
223234
async function(path, stat) {
224235
console.log("File added:", path);
225-
this._watch();
236+
this._watch(path);
226237
}.bind(this)
227238
);
228239
};

src/EleventyConfig.js

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ const pkg = require("../package.json");
88
// API to expose configuration options in config file
99
class EleventyConfig {
1010
constructor() {
11+
this.reset();
12+
}
13+
14+
reset() {
15+
debug("Resetting EleventyConfig to initial values.");
1116
this.events = new EventEmitter();
1217
this.collections = {};
1318

src/TemplateConfig.js

+35-14
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,40 @@ const chalk = require("chalk");
22
const fs = require("fs-extra");
33
const lodashMerge = require("lodash.merge");
44
const TemplatePath = require("./TemplatePath");
5-
const mainRootConfig = require("../config.js");
65
const eleventyConfig = require("./EleventyConfig");
76
const debug = require("debug")("Eleventy:TemplateConfig");
87

98
class TemplateConfig {
10-
constructor(rootConfig, projectConfigPath) {
9+
constructor(customRootConfig, localProjectConfigPath) {
1110
this.overrides = {};
12-
this.projectConfigPath = projectConfigPath || ".eleventy.js";
13-
this.rootConfig = rootConfig || mainRootConfig;
14-
if (rootConfig) {
11+
this.localProjectConfigPath = localProjectConfigPath || ".eleventy.js";
12+
13+
if (customRootConfig) {
14+
this.customRootConfig = customRootConfig;
1515
debug("Warning: Using custom root config!");
16+
} else {
17+
this.customRootConfig = null;
1618
}
19+
this.initializeRootConfig();
20+
this.config = this.mergeConfig(this.localProjectConfigPath);
21+
}
1722

18-
if (typeof this.rootConfig === "function") {
19-
this.rootConfig = this.rootConfig(eleventyConfig);
20-
// debug( "rootConfig is a function, after calling, eleventyConfig is %o", eleventyConfig );
21-
}
22-
debug("rootConfig %o", this.rootConfig);
23+
getLocalProjectConfigFile() {
24+
return this.localProjectConfigPath;
25+
}
2326

24-
this.config = this.mergeConfig(this.projectConfigPath);
27+
reset() {
28+
eleventyConfig.reset();
29+
this.initializeRootConfig();
30+
this.config = this.mergeConfig(this.localProjectConfigPath);
2531
}
2632

2733
getConfig() {
2834
return this.config;
2935
}
3036

3137
setProjectConfigPath(path) {
32-
this.projectConfigPath = path;
38+
this.localProjectConfigPath = path;
3339

3440
this.config = this.mergeConfig(path);
3541
}
@@ -40,16 +46,31 @@ class TemplateConfig {
4046
this.config.pathPrefix = pathPrefix;
4147
}
4248

43-
mergeConfig(projectConfigPath) {
49+
initializeRootConfig() {
50+
this.rootConfig = this.customRootConfig || require("../config.js");
51+
52+
if (typeof this.rootConfig === "function") {
53+
this.rootConfig = this.rootConfig(eleventyConfig);
54+
// debug( "rootConfig is a function, after calling, eleventyConfig is %o", eleventyConfig );
55+
}
56+
debug("rootConfig %o", this.rootConfig);
57+
}
58+
59+
mergeConfig(localProjectConfigPath) {
4460
let overrides = ["templateFormats"];
4561
let localConfig = {};
4662
let path = TemplatePath.normalize(
47-
TemplatePath.getWorkingDir() + "/" + projectConfigPath
63+
TemplatePath.getWorkingDir() + "/" + localProjectConfigPath
4864
);
4965
debug(`Merging config with ${path}`);
5066

5167
if (fs.existsSync(path)) {
5268
try {
69+
// remove from require cache so it will grab a fresh copy
70+
if (path in require.cache) {
71+
delete require.cache[path];
72+
}
73+
5374
localConfig = require(path);
5475
// debug( "localConfig require return value: %o", localConfig );
5576
} catch (err) {

0 commit comments

Comments
 (0)