Skip to content

Commit

Permalink
Fixes #3324 adds support for passing in a file path to --incremental …
Browse files Browse the repository at this point in the history
…on command line.
  • Loading branch information
zachleat committed Jun 20, 2024
1 parent 6b8e53e commit 5c30238
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 48 deletions.
11 changes: 8 additions & 3 deletions cmd.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ const debug = require("debug")("Eleventy:cmd");
try {
let errorHandler = new EleventyErrorHandler();
const argv = require("minimist")(process.argv.slice(2), {
string: ["input", "output", "formats", "config", "pathprefix", "port", "to"],
string: ["input", "output", "formats", "config", "pathprefix", "port", "to", "incremental"],
boolean: [
"quiet",
"version",
"watch",
"dryrun",
"help",
"serve",
"incremental",
"ignore-initial",
],
default: {
Expand Down Expand Up @@ -90,8 +89,14 @@ const debug = require("debug")("Eleventy:cmd");
elev.setIsVerbose(false);
}

// Only relevant for watch/serve
elev.setIgnoreInitial(argv["ignore-initial"]);
elev.setIncrementalBuild(argv.incremental);

if(argv.incremental) {
elev.setIncrementalFile(argv.incremental);
} else if(argv.incremental !== undefined) {
elev.setIncrementalBuild(argv.incremental);
}

try {
if (argv.serve) {
Expand Down
3 changes: 2 additions & 1 deletion src/Data/TemplateData.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ class TemplateData {
let rawInput;

if (readFile) {
rawInput = await EleventyLoadContent(path, options);
rawInput = EleventyLoadContent(path, options);
}

if (readFile && !rawInput) {
Expand Down Expand Up @@ -524,6 +524,7 @@ class TemplateData {

dataBench.after();
aggregateDataBench.after();

return returnValue;
} else if (this.isUserDataExtension(extension)) {
// Other extensions
Expand Down
11 changes: 9 additions & 2 deletions src/Eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,10 @@ class Eleventy {
*/
setIncrementalBuild(isIncremental) {
this.isIncremental = !!isIncremental;
this.watchManager.incremental = !!isIncremental;

if (this.watchManager) {
this.watchManager.incremental = !!isIncremental;
}
}

/**
Expand All @@ -299,6 +302,10 @@ class Eleventy {
*/
setIgnoreInitial(ignoreInitialBuild) {
this.isRunInitialBuild = !ignoreInitialBuild;

if (this.writer) {
this.writer.setRunInitialBuild(this.isRunInitialBuild);
}
}

/**
Expand Down Expand Up @@ -657,7 +664,7 @@ Verbose Output: ${this.verboseMode}`;
this.setIgnoreInitial(true);
this.setIncrementalBuild(true);

this.programmaticApiIncrementalFile = incrementalFile;
this.programmaticApiIncrementalFile = TemplatePath.addLeadingDotSlash(incrementalFile);
}
}

Expand Down
94 changes: 52 additions & 42 deletions src/Util/Require.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ if ("register" in module) {
// important to clear the require.cache in CJS projects
const require = module.createRequire(import.meta.url);

const requestPromiseCache = new Map();

// Used for JSON imports, suffering from Node warning that import assertions experimental but also
// throwing an error if you try to import() a JSON file without an import assertion.
async function loadContents(path, options = {}) {
function loadContents(path, options = {}) {
let rawInput;
let encoding = "utf8"; // JSON is utf8
if ("encoding" in options) {
encoding = options.encoding;
}

try {
rawInput = await fs.promises.readFile(path, encoding);
rawInput = fs.readFileSync(path, encoding);
} catch (e) {
// if file does not exist, return nothing
}
Expand Down Expand Up @@ -70,7 +72,7 @@ eventBus.on("eleventy.importCacheReset", (fileQueue) => {
async function dynamicImportAbsolutePath(absolutePath, type, returnRaw = false) {
if (absolutePath.endsWith(".json") || type === "json") {
// https://v8.dev/features/import-assertions#dynamic-import() is still experimental in Node 20
let rawInput = await loadContents(absolutePath);
let rawInput = loadContents(absolutePath);
return JSON.parse(rawInput);
}

Expand All @@ -88,54 +90,62 @@ async function dynamicImportAbsolutePath(absolutePath, type, returnRaw = false)
urlPath = absolutePath;
}

let target = await import(urlPath);
let promise;
if (requestPromiseCache.has(urlPath)) {
promise = requestPromiseCache.get(urlPath);
} else {
promise = import(urlPath);
requestPromiseCache.set(urlPath, promise);
}

if (returnRaw) {
return target;
return promise;
}

// If the only export is `default`, elevate to top (for ESM and CJS)
if (Object.keys(target).length === 1 && "default" in target) {
return target.default;
}
return promise.then((target) => {
// If the only export is `default`, elevate to top (for ESM and CJS)
if (Object.keys(target).length === 1 && "default" in target) {
return target.default;
}

// When using import() on a CommonJS file that exports an object sometimes it
// returns duplicated values in `default` key, e.g. `{ default: {key: value}, key: value }`

// A few examples:
// module.exports = { key: false };
// returns `{ default: {key: false}, key: false }` as not expected.
// module.exports = { key: true };
// module.exports = { key: null };
// module.exports = { key: undefined };
// module.exports = { key: class {} };

// A few examples where it does not duplicate:
// module.exports = { key: 1 };
// returns `{ default: {key: 1} }` as expected.
// module.exports = { key: "value" };
// module.exports = { key: {} };
// module.exports = { key: [] };

if (type === "cjs" && "default" in target) {
let match = true;
for (let key in target) {
if (key === "default") {
continue;
}
if (target[key] !== target.default[key]) {
match = false;
// When using import() on a CommonJS file that exports an object sometimes it
// returns duplicated values in `default` key, e.g. `{ default: {key: value}, key: value }`

// A few examples:
// module.exports = { key: false };
// returns `{ default: {key: false}, key: false }` as not expected.
// module.exports = { key: true };
// module.exports = { key: null };
// module.exports = { key: undefined };
// module.exports = { key: class {} };

// A few examples where it does not duplicate:
// module.exports = { key: 1 };
// returns `{ default: {key: 1} }` as expected.
// module.exports = { key: "value" };
// module.exports = { key: {} };
// module.exports = { key: [] };

if (type === "cjs" && "default" in target) {
let match = true;
for (let key in target) {
if (key === "default") {
continue;
}
if (target[key] !== target.default[key]) {
match = false;
}
}
}

if (match) {
return target.default;
if (match) {
return target.default;
}
}
}

// Otherwise return { default: value, named: value }
// Object.assign here so we can add things to it in JavaScript.js
return Object.assign({}, target);
// Otherwise return { default: value, named: value }
// Object.assign here so we can add things to it in JavaScript.js
return Object.assign({}, target);
});
}

function normalizeFilePathInEleventyPackage(file) {
Expand Down

0 comments on commit 5c30238

Please sign in to comment.