Skip to content

Commit

Permalink
Windows fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Oct 20, 2023
1 parent d327c95 commit 03c048c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Util/ImportJsonSync.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "fs";
import path from "path";
import { TemplatePath } from "@11ty/eleventy-utils";
import { normalizeFilePathInEleventyPackage } from "./Require.js";

// async version of this in Require.js

Expand Down Expand Up @@ -38,7 +39,7 @@ function importJsonSync(filePath) {

// TODO cache
function getEleventyPackageJson() {
let filePath = path.resolve(import.meta.url.slice("file://".length), "../../../package.json");
let filePath = normalizeFilePathInEleventyPackage("package.json");
return importJsonSync(filePath);
}

Expand Down
16 changes: 15 additions & 1 deletion src/Util/Require.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,22 @@ async function dynamicImportAbsolutePath(absolutePath, type) {
return target;
}

function normalizeFilePathInEleventyPackage(file) {
let currentFile = import.meta.url.slice("file:///".length);
/* Transform:
* `file:///Users/` to `/Users/`, which is an absolute path on POSIX (per `path.isAbsolute`)
* `file:///C:/Users/` to `C:/Users/` which is an absolute path on Windows (per `path.isAbsolute`)
*/
if (!path.isAbsolute(currentFile)) {
currentFile = "/" + currentFile;
}
// Back up from ./src/Util/Require.js
return path.resolve(currentFile, "../../../", file);
}

async function dynamicImportFromEleventyPackage(file) {
// points to files relative to the top level Eleventy directory
let filePath = path.resolve(import.meta.url.slice("file://".length), "../../../", file);
let filePath = normalizeFilePathInEleventyPackage(file);
return dynamicImportAbsolutePath(filePath);
}

Expand All @@ -126,4 +139,5 @@ export {
loadContents as EleventyLoadContent,
dynamicImport as EleventyImport,
dynamicImportFromEleventyPackage as EleventyImportFromEleventy,
normalizeFilePathInEleventyPackage,
};

0 comments on commit 03c048c

Please sign in to comment.