Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Mar 17, 2023
1 parent 5f61c02 commit 8134d23
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
49 changes: 33 additions & 16 deletions src/Eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const ConsoleLogger = require("./Util/ConsoleLogger");
const PathPrefixer = require("./Util/PathPrefixer");
const TemplateConfig = require("./TemplateConfig");
const FileSystemSearch = require("./FileSystemSearch");

const PathNormalizer = require("./Util/PathNormalizer.js");
const simplePlural = require("./Util/Pluralize");
const checkPassthroughCopyBehavior = require("./Util/PassthroughCopyBehaviorCheck");
const debug = require("debug")("Eleventy");
Expand Down Expand Up @@ -715,6 +715,7 @@ Arguments:
this.env = this.getEnvironmentVariableValues();
this.initializeEnvironmentVariables(this.env);

// this.eleventyConfig = new TemplateConfig(null, this.options.configPath);
this.eleventyConfig.reset();

this.config = this.eleventyConfig.getConfig();
Expand All @@ -734,7 +735,7 @@ Arguments:
* @method
* @param {String} changedFilePath - File that triggered a re-run (added or modified)
*/
async _addFileToWatchQueue(changedFilePath) {
async _addFileToWatchQueue(changedFilePath, isResetConfig) {
// Currently this is only for 11ty.js deps but should be extended with usesGraph
let usedByDependants = [];
if (this.watchTargets) {
Expand All @@ -744,24 +745,27 @@ Arguments:
}

// Note: this is a sync event!
eventBus.emit("eleventy.resourceModified", changedFilePath, usedByDependants);
eventBus.emit("eleventy.resourceModified", changedFilePath, usedByDependants, {
viaConfigReset: isResetConfig,
});

this.watchManager.addToPendingQueue(changedFilePath);
}

_shouldResetConfig() {
let configFilePaths = this.eleventyConfig.getLocalProjectConfigFiles();
let configFilesChanged = this.watchManager.hasQueuedFiles(configFilePaths);

if (configFilesChanged) {
return true;
shouldTriggerConfigReset(changedFiles) {
let configFilePaths = new Set(this.eleventyConfig.getLocalProjectConfigFiles());
for (let filePath of changedFiles) {
if (configFilePaths.has(filePath)) {
return true;
}
}

for (const configFilePath of configFilePaths) {
// Any dependencies of the config file changed
let configFileDependencies = this.watchTargets.getDependenciesOf(configFilePath);
let configFileDependencies = new Set(this.watchTargets.getDependenciesOf(configFilePath));

for (let dep of configFileDependencies) {
if (this.watchManager.hasQueuedFile(dep)) {
for (let filePath of changedFiles) {
if (configFileDependencies.has(filePath)) {
return true;
}
}
Expand All @@ -770,13 +774,26 @@ Arguments:
return false;
}

// Checks the build queue to see if any configuration related files have changed
_shouldResetConfig(activeQueue = []) {
if (!activeQueue.length) {
return false;
}

return this.shouldTriggerConfigReset(
activeQueue.map((path) => {
return PathNormalizer.normalizeSeperator(TemplatePath.addLeadingDotSlash(path));
})
);
}

/**
* tbd.
*
* @private
* @method
*/
async _watch() {
async _watch(isResetConfig = false) {
if (this.watchManager.isBuildRunning()) {
return;
}
Expand All @@ -791,7 +808,6 @@ Arguments:
this.watchTargets.clearRequireCacheFor(queue);

// reset and reload global configuration
let isResetConfig = this._shouldResetConfig();
if (isResetConfig) {
this.resetConfig();
}
Expand Down Expand Up @@ -1025,12 +1041,13 @@ Arguments:
let watchRun = async (path) => {
path = TemplatePath.normalize(path);
try {
this._addFileToWatchQueue(path);
let isResetConfig = this._shouldResetConfig([path]);
this._addFileToWatchQueue(path, isResetConfig);
clearTimeout(watchDelay);

await new Promise((resolve, reject) => {
watchDelay = setTimeout(async () => {
this._watch().then(resolve, reject);
this._watch(isResetConfig).then(resolve, reject);
}, this.config.watchThrottleWaitTime);
});
} catch (e) {
Expand Down
26 changes: 18 additions & 8 deletions src/TemplateCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class TemplateCache {
this.cacheByInputPath = {};
}

// alias
removeAll() {
this.clear();
}

size() {
return Object.keys(this.cacheByInputPath).length;
}
Expand Down Expand Up @@ -60,29 +65,34 @@ class TemplateCache {
return this.cache[key];
}

remove(filePath) {
filePath = TemplatePath.stripLeadingDotSlash(filePath);

if (!this.cacheByInputPath[filePath]) {
remove(layoutFilePath) {
layoutFilePath = TemplatePath.stripLeadingDotSlash(layoutFilePath);
if (!this.cacheByInputPath[layoutFilePath]) {
// not a layout file
return;
}

let layoutTemplate = this.cacheByInputPath[filePath];
let layoutTemplate = this.cacheByInputPath[layoutFilePath];
layoutTemplate.resetCaches();

let keys = layoutTemplate.getCacheKeys();
for (let key of keys) {
delete this.cache[key];
}

delete this.cacheByInputPath[filePath];
delete this.cacheByInputPath[layoutFilePath];
}
}

let layoutCache = new TemplateCache();

eventBus.on("eleventy.resourceModified", (path) => {
layoutCache.remove(path);
eventBus.on("eleventy.resourceModified", (path, usedBy, metadata = {}) => {
// https://github.com/11ty/eleventy-plugin-bundle/issues/10
if (metadata.viaConfigReset) {
layoutCache.removeAll();
} else {
layoutCache.remove(path);
}
});

// singleton
Expand Down

0 comments on commit 8134d23

Please sign in to comment.