From d3d0d2d3f97abbc7c55edf3d48b4bbc92934b570 Mon Sep 17 00:00:00 2001 From: vrugtehagel Date: Tue, 23 Jul 2024 19:52:38 +0200 Subject: [PATCH 1/4] Add loader param --- cmd.cjs | 3 ++- src/Eleventy.js | 21 +++++++++++++++++++-- test/EleventyTest.js | 16 ++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/cmd.cjs b/cmd.cjs index 6e7fbc877..871e7bb96 100755 --- a/cmd.cjs +++ b/cmd.cjs @@ -24,7 +24,7 @@ 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", "incremental"], + string: ["input", "output", "formats", "config", "pathprefix", "port", "to", "incremental", "loader"], boolean: [ "quiet", "version", @@ -71,6 +71,7 @@ const debug = require("debug")("Eleventy:cmd"); pathPrefix: argv.pathprefix, runMode: argv.serve ? "serve" : argv.watch ? "watch" : "build", dryRun: argv.dryrun, + loader: argv.loader, }); // reuse ErrorHandler instance in Eleventy diff --git a/src/Eleventy.js b/src/Eleventy.js index 20cf93b81..2a081f25a 100644 --- a/src/Eleventy.js +++ b/src/Eleventy.js @@ -128,6 +128,12 @@ class Eleventy { * @default 0 */ this.buildCount = 0; + + /** + * @member {String} - Force ESM or CJS mode instead of detecting from package.json. Either cjs, esm, or auto. + * @default "auto" + */ + this.loader = this.options.loader ?? "auto"; } /** @@ -766,6 +772,9 @@ Arguments: --dryrun Don’t write any files. Useful in DEBUG mode, for example: \`DEBUG=Eleventy* npx @11ty/eleventy --dryrun\` + --loader + Set to "esm" to force ESM mode, "cjs" to force CommonJS mode, or "auto" (default) to infer it from package.json. + --to=json --to=ndjson Change the output to JSON or NDJSON (default: \`fs\`) @@ -1005,15 +1014,23 @@ Arguments: } get isEsm() { - if (this._isEsm === undefined) { + if (this._isEsm !== undefined) { + return this._isEsm; + } + if (this.loader == "esm") { + this._isEsm = true; + } else if (this.loader == "cjs") { + this._isEsm = false; + } else if (this.loader == "auto") { try { this._isEsm = this.projectPackageJson?.type === "module"; } catch (e) { debug("Could not find a project package.json for project’s ES Modules check: %O", e); this._isEsm = false; } + } else { + throw new Error("The 'loader' option must be one of 'esm', 'cjs', or 'auto'"); } - return this._isEsm; } diff --git a/test/EleventyTest.js b/test/EleventyTest.js index 8fbcb6f22..b8eab18b1 100644 --- a/test/EleventyTest.js +++ b/test/EleventyTest.js @@ -1618,3 +1618,19 @@ test("#3373: Throw an error when explicit config path is not found.", async (t) let e = await t.throwsAsync(() => elev.toJSON()); t.is(e.message, "A configuration file was specified but not found: this-file-is-not-found.js"); }); + +test("Eleventy loader can force ESM mode", async (t) => { + let elev = new Eleventy("./README.md", "./_site", { + loader: "esm", + }); + + t.is(elev.isEsm, true); +}); + +test("Eleventy loader can force CommonJS mode", async (t) => { + let elev = new Eleventy("./README.md", "./_site", { + loader: "cjs", + }); + + t.is(elev.isEsm, false); +}); From afe3f84ed257cd303cc58b8575d2111391f62eb3 Mon Sep 17 00:00:00 2001 From: vrugtehagel <41021050+vrugtehagel@users.noreply.github.com> Date: Thu, 25 Jul 2024 09:54:17 +0200 Subject: [PATCH 2/4] Remove accidental remainder from conlfict resolution --- cmd.cjs | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd.cjs b/cmd.cjs index 42ef0c0a8..f89168872 100755 --- a/cmd.cjs +++ b/cmd.cjs @@ -23,7 +23,6 @@ const debug = require("debug")("Eleventy:cmd"); const { EleventyErrorHandler } = await import("./src/Errors/EleventyErrorHandler.js"); try { - let errorHandler = new EleventyErrorHandler(); const argv = minimist(process.argv.slice(2), { string: ["input", "output", "formats", "config", "pathprefix", "port", "to", "incremental", "loader"], boolean: [ From e145d28cf5f0d274c5ec6da91a2577fcd4f0a44e Mon Sep 17 00:00:00 2001 From: Koen <41021050+vrugtehagel@users.noreply.github.com> Date: Tue, 13 Aug 2024 16:07:29 +0200 Subject: [PATCH 3/4] Update indentation style in cmd.cjs --- cmd.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd.cjs b/cmd.cjs index 78432a7cc..b51e5928f 100755 --- a/cmd.cjs +++ b/cmd.cjs @@ -84,7 +84,7 @@ const debug = require("debug")("Eleventy:cmd"); pathPrefix: argv.pathprefix, runMode: argv.serve ? "serve" : argv.watch ? "watch" : "build", dryRun: argv.dryrun, - loader: argv.loader, + loader: argv.loader, }); // reuse ErrorHandler instance in Eleventy From 05b8aa4dc9eb1825100f2451703f4e2e51960d4f Mon Sep 17 00:00:00 2001 From: Koen <41021050+vrugtehagel@users.noreply.github.com> Date: Tue, 13 Aug 2024 16:09:06 +0200 Subject: [PATCH 4/4] Update indentation style in Eleventy.js --- src/Eleventy.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Eleventy.js b/src/Eleventy.js index a86fb0d62..e700bad80 100644 --- a/src/Eleventy.js +++ b/src/Eleventy.js @@ -176,8 +176,8 @@ class Eleventy { */ this.loader = this.options.loader ?? "auto"; - /** - * @type {Number} + /** + * @type {Number} * @description The timestamp of Eleventy start. */ this.start = this.getNewTimestamp();