summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/eleventy/src/Util/EsmResolver.js
diff options
context:
space:
mode:
authorShipwreckt <me@shipwreckt.co.uk>2025-10-31 20:02:14 +0000
committerShipwreckt <me@shipwreckt.co.uk>2025-10-31 20:02:14 +0000
commit7a52ddeba2a68388b544f529d2d92104420f77b0 (patch)
tree15ddd47457a2cb4a96060747437d36474e4f6b4e /node_modules/@11ty/eleventy/src/Util/EsmResolver.js
parent53d6ae2b5568437afa5e4995580a3fb679b7b91b (diff)
Changed from static to 11ty!
Diffstat (limited to 'node_modules/@11ty/eleventy/src/Util/EsmResolver.js')
-rw-r--r--node_modules/@11ty/eleventy/src/Util/EsmResolver.js53
1 files changed, 53 insertions, 0 deletions
diff --git a/node_modules/@11ty/eleventy/src/Util/EsmResolver.js b/node_modules/@11ty/eleventy/src/Util/EsmResolver.js
new file mode 100644
index 0000000..c098ed8
--- /dev/null
+++ b/node_modules/@11ty/eleventy/src/Util/EsmResolver.js
@@ -0,0 +1,53 @@
+import debugUtil from "debug";
+import { fileURLToPath } from "node:url";
+import PathNormalizer from "./PathNormalizer.js";
+
+const debug = debugUtil("Eleventy:EsmResolver");
+
+let lastModifiedPaths = new Map();
+export async function initialize({ port }) {
+ // From `eleventy.importCacheReset` event in Require.js
+ port.on("message", ({ path, newDate }) => {
+ lastModifiedPaths.set(path, newDate);
+ });
+}
+
+// Fixes issue https://github.com/11ty/eleventy/issues/3270
+// Docs: https://nodejs.org/docs/latest/api/module.html#resolvespecifier-context-nextresolve
+export async function resolve(specifier, context, nextResolve) {
+ try {
+ // Not a relative import and not a file import
+ // Or from node_modules (perhaps better to check if the specifier is in the project directory instead)
+ if (
+ (!specifier.startsWith("../") &&
+ !specifier.startsWith("./") &&
+ !specifier.startsWith("file:")) ||
+ context.parentURL.includes("/node_modules/")
+ ) {
+ return nextResolve(specifier);
+ }
+
+ let fileUrl = new URL(specifier, context.parentURL);
+ if (fileUrl.searchParams.has("_cache_bust")) {
+ // already is cache busted outside resolver (wider compat, url was changed prior to import, probably in Require.js)
+ return nextResolve(specifier);
+ }
+
+ let absolutePath = PathNormalizer.normalizeSeperator(fileURLToPath(fileUrl));
+ // Bust the import cache if this is a recently modified file
+ if (lastModifiedPaths.has(absolutePath)) {
+ fileUrl.search = ""; // delete existing searchparams
+ fileUrl.searchParams.set("_cache_bust", lastModifiedPaths.get(absolutePath));
+ debug("Cache busting %o to %o", specifier, fileUrl.toString());
+
+ return nextResolve(fileUrl.toString());
+ }
+ } catch (e) {
+ debug("EsmResolver Error parsing specifier (%o): %o", specifier, e);
+ }
+
+ return nextResolve(specifier);
+}
+
+// export async function load(url, context, nextLoad) {
+// }