MCPcopy
hub / github.com/webpack/sass-loader / resolveEsmEntry

Function resolveEsmEntry

test/implementation-option.test.js:33–66  ·  view source on GitHub ↗

* Resolve the absolute `file:` URL that `await import(specifier)` from inside * `src/` would land on. Walks the package's `exports` field preferring the * `node` / `import` / `default` conditions, falling back to `module` / `main`. * Used to key `mock.module` so the loader's dynamic `import("sass

(specifier)

Source from the content-addressed store, hash-verified

31 * @returns {string} `file:` URL of the package's ESM entry
32 */
33function resolveEsmEntry(specifier) {
34 const pkgPath = findPackageJSON(
35 specifier,
36 new URL("../src/utils.js", import.meta.url),
37 );
38 const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
39
40 const walk = (node) => {
41 if (typeof node === "string") return node;
42 if (Array.isArray(node)) {
43 for (const item of node) {
44 const r = walk(item);
45 if (r) return r;
46 }
47 }
48 if (node && typeof node === "object") {
49 for (const key of ["node", "import", "default"]) {
50 if (key in node) {
51 const r = walk(node[key]);
52 if (r) return r;
53 }
54 }
55 }
56 return null;
57 };
58
59 const entry =
60 (pkg.exports && walk(pkg.exports["."] ?? pkg.exports)) ??
61 pkg.module ??
62 pkg.main ??
63 "index.js";
64
65 return pathToFileURL(path.join(path.dirname(pkgPath), entry)).href;
66}
67
68const sassEsmURL = resolveEsmEntry("sass");
69const sassEmbeddedEsmURL = resolveEsmEntry("sass-embedded");

Callers 1

Calls 1

walkFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…