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

Small simplifications (redo) #3250

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 17 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
9 changes: 4 additions & 5 deletions src/Benchmark/BenchmarkGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const debugBenchmark = debugUtil("Eleventy:Benchmark");
class BenchmarkGroup {
constructor() {
this.benchmarks = {};
// Warning: aggregate benchmarks automatically default to false via BenchmarkManager->getBenchmarkGroup
// Warning: aggregate benchmarks automatically default to false via `BenchmarkManager.getBenchmarkGroup()`
this.isVerbose = true;
this.logger = new ConsoleLogger(this.isVerbose);
this.minimumThresholdMs = 0;
Expand All @@ -21,8 +21,8 @@ class BenchmarkGroup {
}

reset() {
for (var type in this.benchmarks) {
this.benchmarks[type].reset();
for (let bench of Object.values(this.benchmarks)) {
bench.reset();
}
}

Expand Down Expand Up @@ -88,8 +88,7 @@ class BenchmarkGroup {
}

finish(label, totalTimeSpent) {
for (var type in this.benchmarks) {
let bench = this.benchmarks[type];
for (let [type, bench] of Object.entries(this.benchmarks)) {
let isAbsoluteMinimumComparison = this.minimumThresholdMs > 0;
let totalForBenchmark = bench.getTotal();
let percent = Math.round((totalForBenchmark * 100) / totalTimeSpent);
Expand Down
8 changes: 4 additions & 4 deletions src/Benchmark/BenchmarkManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class BenchmarkManager {
reset() {
this.start = this.getNewTimestamp();

for (var j in this.benchmarkGroups) {
this.benchmarkGroups[j].reset();
for (let bench of Object.values(this.benchmarkGroups)) {
bench.reset();
}
}

Expand Down Expand Up @@ -64,8 +64,8 @@ class BenchmarkManager {

finish() {
let totalTimeSpentBenchmarking = this.getNewTimestamp() - this.start;
for (var j in this.benchmarkGroups) {
this.benchmarkGroups[j].finish(j, totalTimeSpentBenchmarking);
for (let [groupName, group] of Object.entries(this.benchmarkGroups)) {
group.finish(groupName, totalTimeSpentBenchmarking);
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Data/TemplateData.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { TemplatePath, isPlainObject } from "@11ty/eleventy-utils";
import debugUtil from "debug";

import merge from "../Util/Merge.js";
import unique from "../Util/Unique.js";
import TemplateGlob from "../TemplateGlob.js";
import EleventyExtensionMap from "../EleventyExtensionMap.js";
import EleventyBaseError from "../Errors/EleventyBaseError.js";
Expand Down Expand Up @@ -599,7 +598,7 @@ class TemplateData {
}

debug("getLocalDataPaths(%o): %o", templatePath, paths);
return unique(paths).reverse();
return [...new Set(paths)].reverse();
Zearin marked this conversation as resolved.
Show resolved Hide resolved
}

static mergeDeep(config, target, ...source) {
Expand Down
14 changes: 7 additions & 7 deletions src/Eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,14 +390,14 @@ class Eleventy {
// Restore from cache
if (this._privateCaches.has(key)) {
let c = this._privateCaches.get(key);
for (let cacheKey in c) {
inst[cacheKey] = c[cacheKey];
for (let [cacheKey, cacheValue] of Object.entries(c)) {
inst[cacheKey] = cacheValue;
}
} else {
// Set cache
let c = {};
for (let cacheKey of inst.caches || []) {
c[cacheKey] = inst[cacheKey];
for (let [cacheKey, cacheValue] of Object.entries(inst.caches || [])) {
c[cacheKey] = cacheValue;
}
this._privateCaches.set(key, c);
}
Expand Down Expand Up @@ -880,7 +880,7 @@ Arguments:
build: {
templates: templateResults
.flat()
.filter((entry) => !!entry)
.filter(Boolean)
.map((entry) => {
entry.url = PathPrefixer.joinUrlParts(normalizedPathPrefix, entry.url);
return entry;
Expand Down Expand Up @@ -1250,10 +1250,10 @@ Arguments:

if (to === "fs") {
// New in 3.0: flatten return object for return.
ret[1] = templateResults.flat().filter((entry) => !!entry);
ret[1] = templateResults.flat().filter(Boolean);
eventsArg.results = ret[1];
} else {
eventsArg.results = templateResults.filter((entry) => !!entry);
eventsArg.results = templateResults.filter(Boolean);
}

if (to === "ndjson") {
Expand Down
68 changes: 23 additions & 45 deletions src/EleventyExtensionMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ class EleventyExtensionMap {
}

setFormats(formatKeys = []) {
this.unfilteredFormatKeys = formatKeys.map(function (key) {
return key.trim().toLowerCase();
});
this.unfilteredFormatKeys = formatKeys.map((key) => key.trim().toLowerCase());

this.validTemplateLanguageKeys = this.unfilteredFormatKeys.filter((key) =>
this.hasExtension(key),
Expand Down Expand Up @@ -60,13 +58,11 @@ class EleventyExtensionMap {
}

let files = [];
this.validTemplateLanguageKeys.forEach(
function (key) {
this.getExtensionsFromKey(key).forEach(function (extension) {
files.push((dir ? dir + "/" : "") + path + "." + extension);
});
}.bind(this),
);
this.validTemplateLanguageKeys.forEach((key) => {
this.getExtensionsFromKey(key).forEach((extension) => {
files.push((dir ? dir + "/" : "") + path + "." + extension);
});
});

return files;
}
Expand All @@ -75,12 +71,7 @@ class EleventyExtensionMap {
// on paths found from the file system glob search.
// TODO: Method name might just need to be renamed to something more accurate.
isFullTemplateFilePath(path) {
for (let extension of this.validTemplateLanguageKeys) {
if (path.endsWith(`.${extension}`)) {
return true;
}
}
return false;
return this.validTemplateLanguageKeys.some((extension) => path.endsWith(`.${extension}`));
}

getCustomExtensionEntry(extension) {
Expand All @@ -96,12 +87,9 @@ class EleventyExtensionMap {
}

getValidExtensionsForPath(path) {
let extensions = new Set();
for (let extension in this.extensionToKeyMap) {
if (path.endsWith(`.${extension}`)) {
extensions.add(extension);
}
}
let extensions = new Set(
Object.keys(this.extensionToKeyMap).filter((extension) => path.endsWith(`.${extension}`)),
);

// if multiple extensions are valid, sort from longest to shortest
// e.g. .11ty.js and .js
Expand Down Expand Up @@ -167,18 +155,13 @@ class EleventyExtensionMap {
}

hasExtension(key) {
for (var extension in this.extensionToKeyMap) {
if (this.extensionToKeyMap[extension] === key) {
return true;
}
}
return false;
return Object.values(this.extensionToKeyMap).includes(key);
}

getExtensionsFromKey(key) {
let extensions = [];
for (var extension in this.extensionToKeyMap) {
if (this.extensionToKeyMap[extension] === key) {
for (let [extension, entry] of Object.entries(this.extensionToKeyMap)) {
if (entry === key) {
extensions.push(extension);
}
}
Expand All @@ -187,15 +170,16 @@ class EleventyExtensionMap {

// Only `addExtension` configuration API extensions
getExtensionEntriesFromKey(key) {
let entries = [];
if ("extensionMap" in this.config) {
if (this.config?.extensionMap) {
let entries = [];
for (let entry of this.config.extensionMap) {
if (entry.key === key) {
entries.push(entry);
return entries;
}
}
}
return entries;
return [];
}

hasEngine(pathOrKey) {
Expand All @@ -205,23 +189,17 @@ class EleventyExtensionMap {
getKey(pathOrKey) {
pathOrKey = (pathOrKey || "").toLowerCase();

for (var extension in this.extensionToKeyMap) {
let key = this.extensionToKeyMap[extension];
if (pathOrKey === extension) {
return key;
} else if (pathOrKey.endsWith("." + extension)) {
for (let [extension, key] of Object.entries(this.extensionToKeyMap)) {
if (pathOrKey === extension || pathOrKey.endsWith(`.${extension}`)) {
return key;
}
}
}

removeTemplateExtension(path) {
for (var extension in this.extensionToKeyMap) {
if (path === extension || path.endsWith("." + extension)) {
return path.slice(
0,
path.length - 1 - extension.length < 0 ? 0 : path.length - 1 - extension.length,
);
for (let extension in this.extensionToKeyMap) {
if (path === extension || path.endsWith(`.${extension}`)) {
return path.slice(0, Math.max(0, path.length - 1 - extension.length));
}
}
return path;
Expand All @@ -241,7 +219,7 @@ class EleventyExtensionMap {
"11ty.mjs": "11ty.js",
};

if ("extensionMap" in this.config) {
if (this.config?.extensionMap) {
for (let entry of this.config.extensionMap) {
this._extensionToKeyMap[entry.extension] = entry.key;
}
Expand Down
65 changes: 21 additions & 44 deletions src/EleventyFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,13 @@ class EleventyFiles {
}

get passthroughGlobs() {
let paths = new Set();
// stuff added in addPassthroughCopy()
for (let path of this.passthroughManager.getConfigPathGlobs()) {
paths.add(path);
}
// non-template language extensions
for (let path of this.extensionMap.getPassthroughCopyGlobs(this.inputDir)) {
paths.add(path);
}
let paths = new Set([
// stuff added in addPassthroughCopy()
...this.passthroughManager.getConfigPathGlobs(),

// non-template language extensions
...this.extensionMap.getPassthroughCopyGlobs(this.inputDir),
]);
return Array.from(paths);
}

Expand Down Expand Up @@ -183,13 +181,7 @@ class EleventyFiles {
}

getIgnoreGlobs() {
let uniqueIgnores = new Set();
for (let ignore of this.fileIgnores) {
uniqueIgnores.add(ignore);
}
for (let ignore of this.extraIgnores) {
uniqueIgnores.add(ignore);
}
let uniqueIgnores = new Set([...this.fileIgnores, ...this.extraIgnores]);
// Placing the config ignores last here is important to the tests
for (let ignore of this.config.ignores) {
uniqueIgnores.add(TemplateGlob.normalizePath(this.localPathRoot || ".", ignore));
Expand All @@ -203,12 +195,9 @@ class EleventyFiles {
}

let ignores = [];
for (let ignorePath of ignoreFiles) {
ignorePath = TemplatePath.normalize(ignorePath);

let dir = TemplatePath.getDirFromFilePath(ignorePath);

for (let ignorePath of ignoreFiles.map(TemplatePath.normalize)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brittle map usage.

if (fs.existsSync(ignorePath) && fs.statSync(ignorePath).size > 0) {
let dir = TemplatePath.getDirFromFilePath(ignorePath);
let ignoreContent = fs.readFileSync(ignorePath, "utf8");

ignores = ignores.concat(EleventyFiles.normalizeIgnoreContent(dir, ignoreContent));
Expand All @@ -226,9 +215,7 @@ class EleventyFiles {
if (ignoreContent) {
ignores = ignoreContent
.split("\n")
.map((line) => {
return line.trim();
})
.map((line) => line.trim())
.filter((line) => {
if (line.charAt(0) === "!") {
debug(
Expand Down Expand Up @@ -360,7 +347,7 @@ class EleventyFiles {

paths = paths.concat(virtualTemplates);

// Virtual templates can not live at the same place as files on the file system!
// Virtual templates cannot live at the same place as files on the file system!
if (paths.length !== new Set(paths).size) {
let conflicts = {};
for (let path of paths) {
Expand Down Expand Up @@ -398,14 +385,7 @@ class EleventyFiles {
if (!filePath) {
return false;
}

for (let path of paths) {
if (path === filePath) {
return true;
}
}

return false;
return paths.includes(filePath);
}

/* For `eleventy --watch` */
Expand All @@ -418,7 +398,7 @@ class EleventyFiles {
}

// Revert to old passthroughcopy copy files behavior
return this.validTemplateGlobs.concat(this.passthroughGlobs).concat(directoryGlobs);
return this.validTemplateGlobs.concat(this.passthroughGlobs, directoryGlobs);
}

/* For `eleventy --watch` */
Expand Down Expand Up @@ -450,13 +430,12 @@ class EleventyFiles {
/* Ignored by `eleventy --watch` */
getGlobWatcherIgnores() {
// convert to format without ! since they are passed in as a separate argument to glob watcher
let entries = new Set(
this.fileIgnores.map((ignore) => TemplatePath.stripLeadingDotSlash(ignore)),
);

for (let ignore of this.config.watchIgnores) {
entries.add(TemplateGlob.normalizePath(this.localPathRoot || ".", ignore));
}
let entries = new Set([
...this.fileIgnores.map(TemplatePath.stripLeadingDotSlash),
...this.config.watchIgnores.map((ignore) =>
TemplateGlob.normalizePath(this.localPathRoot || ".", ignore),
),
]);

// de-duplicated
return Array.from(entries);
Expand All @@ -475,9 +454,7 @@ class EleventyFiles {
// never ignore the input directory (even if config file returns "" for these)
return entry && entry !== this.inputDir;
})
.map((entry) => {
return TemplateGlob.map(entry + "**");
});
.map((entry) => TemplateGlob.map(entry + "**"));
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/EleventyWatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,7 @@ class EleventyWatch {
}

hasQueuedFiles(files) {
for (const file of files) {
if (this.hasQueuedFile(file)) {
return true;
}
}
return false;
return files.some((file) => this.hasQueuedFile(file));
}

get pendingQueue() {
Expand Down
Loading