Skip to content

Commit

Permalink
Fix code to load Eleventy 3 internal modules
Browse files Browse the repository at this point in the history
`eleventy-sass` patches a few methods of classes of Eleventy.
It was easy because Eleventy 1 and 2 use CommonJS modules.
However, Eleventy 3 uses ES modules, which are much more restrictive,
and probably there is no way to load internal modules in an NPM package
whose type is "module" (ES module) from another NPM package whose type
is also "module".

Therefore, I keep the type of `eleventy-sass` package default
("commonjs") and decide to use `require()` to load ES modules, which is
an experimental feature of Node.js 22.

When using `eleventy-sass`, you must add `--experimental-require-module`
option to node commands, and, instead of `npx @11ty/eleventy`, you
should run Eleventy as follows:
npx --node-options="--experimental-require-module" @11ty/eleventy
  • Loading branch information
kentaroi committed Aug 16, 2024
1 parent c1d2cb7 commit ecefa02
Show file tree
Hide file tree
Showing 26 changed files with 45 additions and 35 deletions.
12 changes: 2 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
let eleventySass;

try {
require("@11ty/eleventy/src/GlobalDependencyMap");
require("./lib/eleventy/patch");
eleventySass = require("./lib/eleventy-sass");
} catch {
require("./lib/eleventy/patch-for-2.0.0-canary.18-and-below");
eleventySass = require("./lib/eleventy-sass-for-2.0.0-canary.18-and-below");
}
require("./lib/eleventy/patch");
const eleventySass = require("./lib/eleventy-sass");

const plugin = {
configFunction: eleventySass,
Expand Down
9 changes: 9 additions & 0 deletions lib/eleventy-module-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const path = require("node:path");
const eleventyEleventyCommonJsPath = require.resolve("@11ty/eleventy");
const eleventySrcPath = path.dirname(eleventyEleventyCommonJsPath);

const eleventyModulePath = function(...name) {
return path.join(eleventySrcPath, ...name);
};

module.exports = eleventyModulePath;
7 changes: 5 additions & 2 deletions lib/eleventy-sass.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const compile = require("./compile");
const sourceMapStore = require("./source-map-store");
const hasPlugin = require("./has-plugin");
const clean = require("./clean");
const eleventyModulePath = require("./eleventy-module-path");

const debug = require("debug")("EleventySass");
const debugDev = require("debug")("Dev:EleventySass");
Expand Down Expand Up @@ -197,7 +198,8 @@ const eleventySass = function(eleventyConfig, userOptions = {}) {
}
options.compileOptions.permalink = eleventySassRevvedPermalink;

const TemplateContent = require("@11ty/eleventy/src/TemplateContent");
const templateContentPath = eleventyModulePath("TemplateContent");
const TemplateContent = require(templateContentPath).default;
if (TemplateContent.prototype._renderFunctionWithoutEleventySassFix === undefined)
TemplateContent.prototype._renderFunctionWithoutEleventySassFix =
TemplateContent.prototype._renderFunction;
Expand Down Expand Up @@ -241,7 +243,8 @@ const eleventySass = function(eleventyConfig, userOptions = {}) {
return result;
};
} else {
const TemplateContent = require("@11ty/eleventy/src/TemplateContent");
const templateContentPath = eleventyModulePath("TemplateContent");
const TemplateContent = require(templateContentPath).default;
if (TemplateContent.prototype._renderFunctionWithoutEleventySassFix) {
TemplateContent.prototype._renderFunction =
TemplateContent.prototype._renderFunctionWithoutEleventySassFix;
Expand Down
7 changes: 5 additions & 2 deletions lib/eleventy/patch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const TemplateRender = require("@11ty/eleventy/src/TemplateRender");
const eleventyModulePath = require("../eleventy-module-path");
const templateRenderPath = eleventyModulePath("TemplateRender");
const TemplateRender = require(templateRenderPath).default;

TemplateRender.prototype.getCompiledTemplateWithoutEleventySass =
TemplateRender.prototype.getCompiledTemplate;
Expand All @@ -11,7 +13,8 @@ TemplateRender.prototype.getCompiledTemplate = function(str) {
};


const CustomEngine = require("@11ty/eleventy/src/Engines/Custom");
const customEnginePath = eleventyModulePath("Engines", "Custom");
const CustomEngine = require(customEnginePath).default;

CustomEngine.prototype.compileWithoutEleventySass = CustomEngine.prototype.compile;

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"url": "https://github.com/kentaroi/eleventy-sass/issues"
},
"homepage": "https://github.com/kentaroi/eleventy-sass#readme",
"engines": {
"node": ">=22.0.0"
},
"dependencies": {
"debug": "^4.3.3",
"kleur": "^4.1.4",
Expand Down
2 changes: 1 addition & 1 deletion test/integration/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let dir;

test.before(async t => {
dir = createProject("build");
await exec("npx @11ty/eleventy", { cwd: dir });
await exec("npx --node-options=\"--experimental-require-module\" @11ty/eleventy", { cwd: dir });
});

test("build", async t => {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/computed-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test.before(async t => {
test("build (eleventyComputed.js has a property with String value) #13", async t => {
let result;
await t.notThrowsAsync(async () => {
result = await exec("npx @11ty/eleventy", { cwd: dir });
result = await exec("npx --node-options=\"--experimental-require-module\" @11ty/eleventy", { cwd: dir });
});
let stylePath = path.join(dir, "_site", "css", "style.css");
let styleCSS = await fs.readFile(stylePath, { encoding: "utf8" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let revHash = createHash("sha256").update(cssContent).digest("hex").slice(0, 8);

test.before(async t => {
dir = createProject("rev-with-async-permalink-function-that-returns-async-render-function");
await exec("npx @11ty/eleventy --config=config-with-async-permalink-function-that-returns-async-render-function.js", { cwd: dir });
await exec("npx --node-options=\"--experimental-require-module\" @11ty/eleventy --config=config-with-async-permalink-function-that-returns-async-render-function.js", { cwd: dir });
});

test("create css file with rev hash", async t => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let revHash = createHash("sha256").update(cssContent).digest("hex").slice(0, 8);

test.before(async t => {
dir = createProject("rev-with-async-permalink-function-that-returns-render-function");
await exec("npx @11ty/eleventy --config=config-with-async-permalink-function-that-returns-render-function.js", { cwd: dir });
await exec("npx --node-options=\"--experimental-require-module\" @11ty/eleventy --config=config-with-async-permalink-function-that-returns-render-function.js", { cwd: dir });
});

test("create css file with rev hash", async t => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let revHash = createHash("sha256").update(cssContent).digest("hex").slice(0, 8);

test.before(async t => {
dir = createProject("rev-with-async-permalink-function-that-returns-string");
await exec("npx @11ty/eleventy --config=config-with-async-permalink-function-that-returns-string.js", { cwd: dir });
await exec("npx --node-options=\"--experimental-require-module\" @11ty/eleventy --config=config-with-async-permalink-function-that-returns-string.js", { cwd: dir });
});

test("create css file with rev hash", async t => {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/rev-with-false-permalink.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let revHash = createHash("sha256").update(cssContent).digest("hex").slice(0, 8);

test.before(async t => {
dir = createProject("rev-with-false-permalink");
await exec("npx @11ty/eleventy --config=config-with-false-permalink.js", { cwd: dir });
await exec("npx --node-options=\"--experimental-require-module\" @11ty/eleventy --config=config-with-false-permalink.js", { cwd: dir });
});

test("create css file with rev hash", async t => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let revHash = createHash("sha256").update(cssContent).digest("hex").slice(0, 8);

test.before(async t => {
dir = createProject("rev-with-permalink-function-that-returns-async-render-function");
await exec("npx @11ty/eleventy --config=config-with-permalink-function-that-returns-async-render-function.js", { cwd: dir });
await exec("npx --node-options=\"--experimental-require-module\" @11ty/eleventy --config=config-with-permalink-function-that-returns-async-render-function.js", { cwd: dir });
});

test("create css file with rev hash", async t => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let revHash = createHash("sha256").update(cssContent).digest("hex").slice(0, 8);

test.before(async t => {
dir = createProject("rev-with-permalink-function-that-returns-false");
await exec("npx @11ty/eleventy --config=config-with-permalink-function-that-returns-false.js", { cwd: dir });
await exec("npx --node-options=\"--experimental-require-module\" @11ty/eleventy --config=config-with-permalink-function-that-returns-false.js", { cwd: dir });
});

test("create css file with rev hash", async t => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let revHash = createHash("sha256").update(cssContent).digest("hex").slice(0, 8);

test.before(async t => {
dir = createProject("rev-with-permalink-function-that-returns-raw");
await exec("npx @11ty/eleventy --config=config-with-permalink-function-that-returns-raw.js", { cwd: dir });
await exec("npx --node-options=\"--experimental-require-module\" @11ty/eleventy --config=config-with-permalink-function-that-returns-raw.js", { cwd: dir });
});

test("create css file with rev hash", async t => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let revHash = createHash("sha256").update(cssContent).digest("hex").slice(0, 8);

test.before(async t => {
dir = createProject("rev-with-permalink-function-that-returns-render-function-that-returns-false");
await exec("npx @11ty/eleventy --config=config-with-permalink-function-that-returns-render-function-that-returns-false.js", { cwd: dir });
await exec("npx --node-options=\"--experimental-require-module\" @11ty/eleventy --config=config-with-permalink-function-that-returns-render-function-that-returns-false.js", { cwd: dir });
});

test("create css file with rev hash", async t => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let revHash = createHash("sha256").update(cssContent).digest("hex").slice(0, 8);

test.before(async t => {
dir = createProject("rev-with-permalink-function-that-returns-render-function");
await exec("npx @11ty/eleventy --config=config-with-permalink-function-that-returns-render-function.js", { cwd: dir });
await exec("npx --node-options=\"--experimental-require-module\" @11ty/eleventy --config=config-with-permalink-function-that-returns-render-function.js", { cwd: dir });
});

test("create css file with rev hash", async t => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let revHash = createHash("sha256").update(cssContent).digest("hex").slice(0, 8);

test.before(async t => {
dir = createProject("rev-with-permalink-function-that-returns-string");
execSync("npx @11ty/eleventy --config=config-with-permalink-function-that-returns-string.js", { cwd: dir });
execSync("npx --node-options=\"--experimental-require-module\" @11ty/eleventy --config=config-with-permalink-function-that-returns-string.js", { cwd: dir });
});

test("create css file with rev hash", async t => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let revHash = createHash("sha256").update(cssContent).digest("hex").slice(0, 8);

test.before(async t => {
dir = createProject("rev-with-permalink-function-that-returns-undefined");
await exec("npx @11ty/eleventy --config=config-with-permalink-function-that-returns-undefined.js", { cwd: dir });
await exec("npx --node-options=\"--experimental-require-module\" @11ty/eleventy --config=config-with-permalink-function-that-returns-undefined.js", { cwd: dir });
});

test("create css file with rev hash", async t => {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/rev-without-permalink.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let revHash = createHash("sha256").update(cssContent).digest("hex").slice(0, 8);

test.before(async t => {
dir = createProject("rev-without-permalink");
await exec("npx @11ty/eleventy --config=config-without-permalink.js", { cwd: dir });
await exec("npx --node-options=\"--experimental-require-module\" @11ty/eleventy --config=config-without-permalink.js", { cwd: dir });
});

test("create css file with rev hash", async t => {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/watcher-adding-to-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test.before(async t => {
let sem = new Semaphore(1);
await sem.wait();
dir = createProject("watcher-adding-to-dependencies");
proc = spawn("npx", ["@11ty/eleventy", "--watch"], { cwd: dir, shell: true, timeout: 20000 });
proc = spawn("npx", ["--node-options=\"--experimental-require-module\"", "@11ty/eleventy", "--watch"], { cwd: dir, shell: true, timeout: 20000 });
proc.on("exit", (code, signal) => {
if (process.platform === "darwin")
pid = undefined;
Expand Down
2 changes: 1 addition & 1 deletion test/integration/watcher-dependency-file-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test.before(async t => {
let sem = new Semaphore(1);
await sem.wait();
dir = createProject("watcher-dependency-file-update");
proc = spawn("npx", ["@11ty/eleventy", "--watch"], { cwd: dir, shell: true, timeout: 20000 });
proc = spawn("npx", ["--node-options=\"--experimental-require-module\"", "@11ty/eleventy", "--watch"], { cwd: dir, shell: true, timeout: 20000 });
proc.on("exit", (code, signal) => {
if (process.platform === "darwin")
pid = undefined;
Expand Down
2 changes: 1 addition & 1 deletion test/integration/watcher-dependency-partial-file-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test.before(async t => {
let sem = new Semaphore(1);
await sem.wait();
dir = createProject("watcher-dependency-partial-file-update");
proc = spawn("npx", ["@11ty/eleventy", "--watch"], { cwd: dir, shell: true, timeout: 20000 });
proc = spawn("npx", ["--node-options=\"--experimental-require-module\"", "@11ty/eleventy", "--watch"], { cwd: dir, shell: true, timeout: 20000 });
proc.on("exit", (code, signal) => {
if (process.platform === "darwin")
pid = undefined;
Expand Down
2 changes: 1 addition & 1 deletion test/integration/watcher-removing-from-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test.before(async t => {
let sem = new Semaphore(1);
await sem.wait();
dir = createProject("watcher-removing-from-dependencies");
proc = spawn("npx", ["@11ty/eleventy", "--watch"], { cwd: dir, shell: true, timeout: 20000 });
proc = spawn("npx", ["--node-options=\"--experimental-require-module\"", "@11ty/eleventy", "--watch"], { cwd: dir, shell: true, timeout: 20000 });
proc.on("exit", (code, signal) => {
if (process.platform === "darwin")
pid = undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test.before(async t => {
let sem = new Semaphore(1);
await sem.wait();
dir = createProject("watcher-with-rev-dependency-file-update");
proc = spawn("npx", ["@11ty/eleventy", "--config=config-for-watcher-with-rev.js", "--watch"], { cwd: dir, shell: true, timeout: 20000 });
proc = spawn("npx", ["--node-options=\"--experimental-require-module\"", "@11ty/eleventy", "--config=config-for-watcher-with-rev.js", "--watch"], { cwd: dir, shell: true, timeout: 20000 });
proc.on("exit", (code, signal) => {
if (process.platform === "darwin")
pid = undefined;
Expand Down
2 changes: 1 addition & 1 deletion test/integration/watcher-with-rev.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ test.before(async t => {
let sem = new Semaphore(1);
await sem.wait();
dir = createProject("watcher-with-rev");
proc = spawn("npx", ["@11ty/eleventy", "--config=config-for-watcher-with-rev.js", "--watch"], { cwd: dir, shell: true, timeout: 20000 });
proc = spawn("npx", ["--node-options=\"--experimental-require-module\"", "@11ty/eleventy", "--config=config-for-watcher-with-rev.js", "--watch"], { cwd: dir, shell: true, timeout: 20000 });
proc.on("exit", (code, signal) => {
console.debug("exit");
if (process.platform === "darwin")
Expand Down
2 changes: 1 addition & 1 deletion test/integration/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test.before(async t => {
let sem = new Semaphore(1);
await sem.wait();
dir = createProject("watcher");
proc = spawn("npx", ["@11ty/eleventy", "--watch"], { cwd: dir, shell: true, timeout: 20000 });
proc = spawn("npx", ["--node-options=\"--experimental-require-module\"", "@11ty/eleventy", "--watch"], { cwd: dir, shell: true, timeout: 20000 });
proc.on("exit", (code, signal) => {
if (process.platform === "darwin")
pid = undefined;
Expand Down

0 comments on commit ecefa02

Please sign in to comment.