Skip to content
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
1 change: 1 addition & 0 deletions src/Data/TemplateData.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ class TemplateData {
if (inputDir) {
debugDev("dirStr: %o; inputDir: %o", dir, inputDir);
}
// TODO use DirContains
if (!inputDir || (dir.startsWith(inputDir) && dir !== inputDir)) {
if (this.config.dataFileDirBaseNameOverride) {
let indexDataFile = dir + "/" + this.config.dataFileDirBaseNameOverride;
Expand Down
57 changes: 47 additions & 10 deletions src/Eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import chalk from "kleur";
import { performance } from "node:perf_hooks";
import debugUtil from "debug";
import { filesize } from "filesize";
import path from "node:path";

/* Eleventy Deps */
import { TemplatePath } from "@11ty/eleventy-utils";
Expand Down Expand Up @@ -30,7 +31,11 @@ import { isGlobMatch } from "./Util/GlobMatcher.js";
import simplePlural from "./Util/Pluralize.js";
import checkPassthroughCopyBehavior from "./Util/PassthroughCopyBehaviorCheck.js";
import eventBus from "./EventBus.js";
import { getEleventyPackageJson, getWorkingProjectPackageJson } from "./Util/ImportJsonSync.js";
import {
getEleventyPackageJson,
importJsonSync,
getWorkingProjectPackageJsonPath,
} from "./Util/ImportJsonSync.js";
import { EleventyImport } from "./Util/Require.js";
import ProjectTemplateFormats from "./Util/ProjectTemplateFormats.js";
import { withResolvers } from "./Util/PromiseUtil.js";
Expand All @@ -41,6 +46,7 @@ import I18nPlugin, * as I18nPluginExtras from "./Plugins/I18nPlugin.js";
import HtmlBasePlugin, * as HtmlBasePluginExtras from "./Plugins/HtmlBasePlugin.js";
import { TransformPlugin as InputPathToUrlTransformPlugin } from "./Plugins/InputPathToUrl.js";
import { IdAttributePlugin } from "./Plugins/IdAttributePlugin.js";
import FileSystemRemap from "./Util/GlobRemap.js";

const pkg = getEleventyPackageJson();
const debug = debugUtil("Eleventy");
Expand All @@ -56,6 +62,8 @@ class Eleventy {
* @type {object|undefined}
*/
#projectPackageJson;
/** @type {string} */
#projectPackageJsonPath;
/** @type {ProjectTemplateFormats|undefined} */
#templateFormats;
/** @type {ConsoleLogger|undefined} */
Expand Down Expand Up @@ -589,14 +597,12 @@ Verbose Output: ${this.verboseMode}`;

let configPath = this.eleventyConfig.getLocalProjectConfigFile();
if (configPath) {
let absolutePathToConfig = TemplatePath.absolutePath(configPath);
values.config = absolutePathToConfig;

// TODO(zachleat): if config is not in root (e.g. using --config=)
let root = TemplatePath.getDirFromFilePath(absolutePathToConfig);
values.root = root;
values.config = TemplatePath.absolutePath(configPath);
}

// Fixed: instead of configuration directory, explicit root or working directory
values.root = TemplatePath.getWorkingDir();

values.source = this.source;

// Backwards compatibility
Expand Down Expand Up @@ -1056,7 +1062,11 @@ Arguments:
this.watchManager = new EleventyWatch();
this.watchManager.incremental = this.isIncremental;

this.watchTargets.add(["./package.json"]);
if (this.projectPackageJsonPath) {
this.watchTargets.add([
path.relative(TemplatePath.getWorkingDir(), this.projectPackageJsonPath),
]);
}
this.watchTargets.add(this.eleventyFiles.getGlobWatcherFiles());
this.watchTargets.add(this.eleventyFiles.getIgnoreFiles());

Expand All @@ -1075,9 +1085,17 @@ Arguments:
}

// fetch from project’s package.json
get projectPackageJsonPath() {
if (this.#projectPackageJsonPath === undefined) {
this.#projectPackageJsonPath = getWorkingProjectPackageJsonPath() || false;
}
return this.#projectPackageJsonPath;
}

get projectPackageJson() {
if (!this.#projectPackageJson) {
this.#projectPackageJson = getWorkingProjectPackageJson();
let p = this.projectPackageJsonPath;
this.#projectPackageJson = p ? importJsonSync(p) : {};
}
return this.#projectPackageJson;
}
Expand Down Expand Up @@ -1106,6 +1124,7 @@ Arguments:
return;
}

// TODO use DirContains
let dataDir = TemplatePath.stripLeadingDotSlash(this.templateData.getDataDir());
function filterOutGlobalDataFiles(path) {
return !dataDir || !TemplatePath.stripLeadingDotSlash(path).startsWith(dataDir);
Expand Down Expand Up @@ -1201,7 +1220,25 @@ Arguments:
let rawFiles = await this.getWatchedFiles();
debug("Watching for changes to: %o", rawFiles);

let watcher = chokidar.watch(rawFiles, this.getChokidarConfig());
let options = this.getChokidarConfig();

// Remap all paths to `cwd` if in play (Issue #3854)
let remapper = new FileSystemRemap(rawFiles);
let cwd = remapper.getCwd();

if (cwd) {
options.cwd = cwd;

rawFiles = remapper.getInput().map((entry) => {
return TemplatePath.stripLeadingDotSlash(entry);
});

options.ignored = remapper.getRemapped(options.ignored || []).map((entry) => {
return TemplatePath.stripLeadingDotSlash(entry);
});
}

let watcher = chokidar.watch(rawFiles, options);

initWatchBench.after();

Expand Down
4 changes: 2 additions & 2 deletions src/EleventyExtensionMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ class EleventyExtensionMap {
return this._getGlobs(this.unfilteredFormatKeys, inputDir);
}

_getGlobs(formatKeys, inputDir) {
let dir = TemplatePath.convertToRecursiveGlobSync(inputDir);
_getGlobs(formatKeys, inputDir = "") {
let extensions = new Set();

for (let key of formatKeys) {
Expand All @@ -156,6 +155,7 @@ class EleventyExtensionMap {
}
}

let dir = TemplatePath.convertToRecursiveGlobSync(inputDir);
if (extensions.size === 1) {
return [`${dir}/*.${Array.from(extensions)[0]}`];
} else if (extensions.size > 1) {
Expand Down
55 changes: 38 additions & 17 deletions src/EleventyFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from "node:fs";
import { TemplatePath, isPlainObject } from "@11ty/eleventy-utils";
import debugUtil from "debug";

import DirContains from "./Util/DirContains.js";
import TemplateData from "./Data/TemplateData.js";
import TemplateGlob from "./TemplateGlob.js";
import checkPassthroughCopyBehavior from "./Util/PassthroughCopyBehaviorCheck.js";
Expand All @@ -11,6 +12,7 @@ const debug = debugUtil("Eleventy:EleventyFiles");

class EleventyFiles {
#extensionMap;
#watcherGlobs;

constructor(formats, templateConfig) {
if (!templateConfig) {
Expand Down Expand Up @@ -69,8 +71,8 @@ class EleventyFiles {
this.setupGlobs();
}

get validTemplateGlobs() {
if (!this._validTemplateGlobs) {
#getWatcherGlobs() {
if (!this.#watcherGlobs) {
let globs;
// Input is a file
if (this.inputFile) {
Expand All @@ -79,9 +81,10 @@ class EleventyFiles {
// input is a directory
globs = this.extensionMap.getValidGlobs(this.inputDir);
}
this._validTemplateGlobs = globs;
this.#watcherGlobs = globs;
}
return this._validTemplateGlobs;

return this.#watcherGlobs;
}

get passthroughGlobs() {
Expand Down Expand Up @@ -154,7 +157,7 @@ class EleventyFiles {

setupGlobs() {
this.fileIgnores = this.getIgnores();
this.extraIgnores = this._getIncludesAndDataDirs();
this.extraIgnores = this.getIncludesAndDataDirs();
this.uniqueIgnores = this.getIgnoreGlobs();

// Conditional added for tests that don’t have a config
Expand All @@ -165,6 +168,13 @@ class EleventyFiles {
this.normalizedTemplateGlobs = this.templateGlobs;
}

normalizeIgnoreEntry(entry) {
if (!entry.startsWith("**/")) {
return TemplateGlob.normalizePath(this.localPathRoot || ".", entry);
}
return entry;
}

getIgnoreGlobs() {
let uniqueIgnores = new Set();
for (let ignore of this.fileIgnores) {
Expand All @@ -173,10 +183,12 @@ class EleventyFiles {
for (let ignore of this.extraIgnores) {
uniqueIgnores.add(ignore);
}

// 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));
uniqueIgnores.add(this.normalizeIgnoreEntry(ignore));
}

return Array.from(uniqueIgnores);
}

Expand Down Expand Up @@ -261,11 +273,12 @@ class EleventyFiles {
files.add(this.eleventyIgnoreContent);
}

// ignore output dir (unless this excludes all input)
// input: . and output: . (skip)
// input: ./content and output . (skip)
// input: . and output: ./_site (add)
if (!this.inputDir.startsWith(this.outputDir)) {
// Make sure output dir isn’t in the input dir (or it will ignore all input!)
// input: . and output: . (skip ignore)
// input: ./content and output . (skip ignore)
// input: . and output: ./_site (add ignore)
let outputContainsInputDir = DirContains(this.outputDir, this.inputDir);
if (!outputContainsInputDir) {
// both are already normalized in 3.0
files.add(TemplateGlob.map(this.outputDir + "/**"));
}
Expand All @@ -284,6 +297,7 @@ class EleventyFiles {
if (this.eleventyIgnoreContent === false) {
let absoluteInputDir = TemplatePath.absolutePath(this.inputDir);
ignoreFiles.add(TemplatePath.join(rootDirectory, ".eleventyignore"));

if (rootDirectory !== absoluteInputDir) {
ignoreFiles.add(TemplatePath.join(this.inputDir, ".eleventyignore"));
}
Expand Down Expand Up @@ -427,14 +441,16 @@ class EleventyFiles {
/* For `eleventy --watch` */
getGlobWatcherFiles() {
// TODO improvement: tie the includes and data to specific file extensions (currently using `**`)
let directoryGlobs = this._getIncludesAndDataDirs();
let directoryGlobs = this.getIncludesAndDataDirs();

let globs = this.#getWatcherGlobs();

if (checkPassthroughCopyBehavior(this.config, this.runMode)) {
return this.validTemplateGlobs.concat(directoryGlobs);
return globs.concat(directoryGlobs);
}

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

/* For `eleventy --watch` */
Expand All @@ -456,7 +472,12 @@ class EleventyFiles {
bench.before();
let results = TemplatePath.addLeadingDotSlashArray(
await this.fileSystemSearch.search("js-dependencies", globs, {
ignore: ["**/node_modules/**"],
ignore: [
"**/node_modules/**",
".git/**",
// TODO outputDir
// this.outputDir,
],
}),
);
bench.after();
Expand All @@ -471,14 +492,14 @@ class EleventyFiles {
);

for (let ignore of this.config.watchIgnores) {
entries.add(TemplateGlob.normalizePath(this.localPathRoot || ".", ignore));
entries.add(this.normalizeIgnoreEntry(ignore));
}

// de-duplicated
return Array.from(entries);
}

_getIncludesAndDataDirs() {
getIncludesAndDataDirs() {
let rawPaths = new Set();
rawPaths.add(this.includesDir);
if (this.layoutsDir) {
Expand Down
31 changes: 29 additions & 2 deletions src/FileSystemSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { glob } from "tinyglobby";
import { TemplatePath } from "@11ty/eleventy-utils";
import debugUtil from "debug";

import FileSystemRemap from "./Util/GlobRemap.js";
import { isGlobMatch } from "./Util/GlobMatcher.js";

const debug = debugUtil("Eleventy:FileSystemSearch");
Expand Down Expand Up @@ -32,8 +33,17 @@ class FileSystemSearch {
// Strip leading slashes from everything!
globs = globs.map((entry) => TemplatePath.stripLeadingDotSlash(entry));

let cwd = FileSystemRemap.getCwd(globs);
if (cwd) {
options.cwd = cwd;
}

if (options.ignore && Array.isArray(options.ignore)) {
options.ignore = options.ignore.map((entry) => TemplatePath.stripLeadingDotSlash(entry));
options.ignore = options.ignore.map((entry) => {
entry = TemplatePath.stripLeadingDotSlash(entry);

return FileSystemRemap.remapInput(entry, cwd);
});
debug("Glob search (%o) ignoring: %o", key, options.ignore);
}

Expand All @@ -52,6 +62,14 @@ class FileSystemSearch {

this.count++;

globs = globs.map((entry) => {
if (cwd && entry.startsWith(cwd)) {
return FileSystemRemap.remapInput(entry, cwd);
}

return entry;
});

this.promises[cacheKey] = glob(
globs,
Object.assign(
Expand All @@ -63,8 +81,12 @@ class FileSystemSearch {
),
).then((results) => {
this.outputs[cacheKey] = new Set(
results.map((entry) => TemplatePath.standardizeFilePath(entry)),
results.map((entry) => {
let remapped = FileSystemRemap.remapOutput(entry, options.cwd);
return TemplatePath.standardizeFilePath(remapped);
}),
);

return Array.from(this.outputs[cacheKey]);
});
}
Expand Down Expand Up @@ -97,6 +119,11 @@ class FileSystemSearch {
delete(path) {
this._modify(path, "delete");
}

// Issue #3859 get rid of chokidar globs
// getAllOutputFiles() {
// return Object.values(this.outputs).map(set => Array.from(set)).flat();
// }
}

export default FileSystemSearch;
9 changes: 5 additions & 4 deletions src/TemplateConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,17 @@ class TemplateConfig {
*/
getLocalProjectConfigFile() {
let configFiles = this.getLocalProjectConfigFiles();
// Add the configFiles[0] in case of a test, where no file exists on the file system
let configFile = configFiles.find((path) => path && fs.existsSync(path)) || configFiles[0];

let configFile = configFiles.find((path) => path && fs.existsSync(path));
if (configFile) {
return configFile;
}
}

getLocalProjectConfigFiles() {
if (this.projectConfigPaths?.length > 0) {
return TemplatePath.addLeadingDotSlashArray(this.projectConfigPaths.filter((path) => path));
let paths = this.projectConfigPaths;
if (paths?.length > 0) {
return TemplatePath.addLeadingDotSlashArray(paths.filter((path) => Boolean(path)));
}
return [];
}
Expand Down
Loading
Loading