-
-
Notifications
You must be signed in to change notification settings - Fork 505
/
Copy pathJavaScriptDependencies.js
41 lines (34 loc) · 1.12 KB
/
JavaScriptDependencies.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import dependencyTree from "@11ty/dependency-tree";
import { find } from "@11ty/dependency-tree-esm";
import { TemplatePath } from "@11ty/eleventy-utils";
class JavaScriptDependencies {
static async getDependencies(inputFiles, isProjectUsingEsm) {
let depSet = new Set();
// TODO does this need to work with aliasing? what other JS extensions will have deps?
let commonJsFiles = inputFiles.filter(
(file) => (!isProjectUsingEsm && file.endsWith(".js")) || file.endsWith(".cjs"),
);
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);
}
}
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);
}
}
return Array.from(depSet).sort();
}
}
export default JavaScriptDependencies;