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

Add --loader flag to force ESM/CJS mode #3377

Merged
merged 7 commits into from
Sep 3, 2024
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
3 changes: 2 additions & 1 deletion cmd.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const debug = require("debug")("Eleventy:cmd");

try {
const argv = 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",
Expand Down Expand Up @@ -84,6 +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,
});

// reuse ErrorHandler instance in Eleventy
Expand Down
21 changes: 19 additions & 2 deletions src/Eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ class Eleventy {
*/
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";

/**
* @type {Number}
* @description The timestamp of Eleventy start.
Expand Down Expand Up @@ -796,6 +802,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\`)
Expand Down Expand Up @@ -1019,15 +1028,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;
}

Expand Down
16 changes: 16 additions & 0 deletions test/EleventyTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1571,3 +1571,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);
});