Skip to content

Commit

Permalink
Improved error messaging for #3128
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Jun 27, 2024
1 parent 1298634 commit 743cd86
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/Util/JavaScriptDependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import dependencyTree from "@11ty/dependency-tree";
import { find } from "@11ty/dependency-tree-esm";
import { TemplatePath } from "@11ty/eleventy-utils";

import EleventyBaseError from "../Errors/EleventyBaseError.js";

class JavaScriptDependencies {
static getErrorMessage(file, type) {
return `A problem was encountered looking for JavaScript dependencies in ${type} file: ${file}. This only affects --watch and --serve behavior and does not affect your build.`;
}

static async getDependencies(inputFiles, isProjectUsingEsm) {
let depSet = new Set();

Expand All @@ -12,25 +18,33 @@ class JavaScriptDependencies {
);

for (let file of commonJsFiles) {
let modules = dependencyTree(file, {
nodeModuleNames: "exclude",
allowNotFound: true,
}).map((dependency) => {
return TemplatePath.addLeadingDotSlash(TemplatePath.relativePath(dependency));
});

for (let dep of modules) {
depSet.add(dep);
try {
let modules = dependencyTree(file, {
nodeModuleNames: "exclude",
allowNotFound: true,
}).map((dependency) => {
return TemplatePath.addLeadingDotSlash(TemplatePath.relativePath(dependency));
});

for (let dep of modules) {
depSet.add(dep);
}
} catch (e) {
throw new EleventyBaseError(this.getErrorMessage(file, "CommonJS"), e);
}
}

let esmFiles = inputFiles.filter(
(file) => (isProjectUsingEsm && file.endsWith(".js")) || file.endsWith(".mjs"),
);
for (let file of esmFiles) {
let modules = await find(file);
for (let dep of modules) {
depSet.add(dep);
try {
let modules = await find(file);
for (let dep of modules) {
depSet.add(dep);
}
} catch (e) {
throw new EleventyBaseError(this.getErrorMessage(file, "ESM"), e);
}
}

Expand Down

0 comments on commit 743cd86

Please sign in to comment.