This repository was archived by the owner on Dec 19, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.js
More file actions
148 lines (124 loc) · 3.97 KB
/
main.js
File metadata and controls
148 lines (124 loc) · 3.97 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const path = require("node:path");
const { readFileSync, existsSync } = require("node:fs");
const acorn = require("acorn");
const normalizePath = require("normalize-path");
const { TemplatePath } = require("@11ty/eleventy-utils");
const { DepGraph } = require("dependency-graph");
// Is *not* a bare specifier (e.g. 'some-package')
// https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#terminology
function isNonBareSpecifier(importSource) {
// Change \\ to / on Windows
let normalized = normalizePath(importSource);
// Relative specifier (e.g. './startup.js')
if(normalized.startsWith("./") || normalized.startsWith("../")) {
return true;
}
// Absolute specifier (e.g. 'file:///opt/nodejs/config.js')
if(normalized.startsWith("file:")) {
return true;
}
return false;
}
function normalizeFilePath(filePath) {
return TemplatePath.standardizeFilePath(path.relative(".", filePath));
}
function normalizeImportSourceToFilePath(filePath, source) {
let { dir } = path.parse(filePath);
let normalized = path.join(dir, source);
return normalizeFilePath(normalized);
}
function getImportAttributeType(attributes = []) {
for(let node of attributes) {
if(node.type === "ImportAttribute" && node.key.type === "Identifier" && node.key.name === "type") {
return node.value.value;
}
}
}
async function getSources(filePath, contents) {
let sources = new Set();
let sourcesToRecurse = new Set();
let ast = acorn.parse(contents, {sourceType: "module", ecmaVersion: "latest"});
for(let node of ast.body) {
if(node.type === "ImportDeclaration" && isNonBareSpecifier(node.source.value)) {
let importAttributeType = getImportAttributeType(node?.attributes);
let normalized = normalizeImportSourceToFilePath(filePath, node.source.value);
if(normalized !== filePath) {
sources.add(normalized);
// Recurse typeless (JavaScript) import types only
// Right now only `css` and `json` are valid but others might come later
if(!importAttributeType) {
sourcesToRecurse.add(normalized);
}
}
}
}
return {
sources,
sourcesToRecurse,
}
}
async function find(filePath, alreadyParsedSet = new Set()) {
// TODO add a cache here
// Unfortunately we need to read the entire file, imports need to be at the top level but they can be anywhere 🫠
let normalized = normalizeFilePath(filePath);
if(alreadyParsedSet.has(normalized) || !existsSync(filePath)) {
return [];
}
alreadyParsedSet.add(normalized);
let contents = readFileSync(normalized, { encoding: 'utf8' });
let { sources, sourcesToRecurse } = await getSources(filePath, contents);
// Recurse for nested deps
for(let source of sourcesToRecurse) {
let s = await find(source, alreadyParsedSet);
for(let p of s) {
if(sources.has(p) || p === filePath) {
continue;
}
sources.add(p);
}
}
return Array.from(sources);
}
function mergeGraphs(rootGraph, ...graphs) {
if(!(rootGraph instanceof DepGraph)) {
throw new Error("Incorrect type passed to mergeGraphs, expected DepGraph");
}
for(let g of graphs) {
for(let node of g.overallOrder()) {
if(!rootGraph.hasNode(node)) {
rootGraph.addNode(node);
}
for(let dep of g.directDependenciesOf(node)) {
rootGraph.addDependency(node, dep);
}
}
}
}
async function findGraph(filePath, alreadyParsedSet = new Set()) {
let graph = new DepGraph();
let normalized = normalizeFilePath(filePath);
graph.addNode(filePath);
if(alreadyParsedSet.has(normalized) || !existsSync(filePath)) {
return graph;
}
alreadyParsedSet.add(normalized);
let contents = readFileSync(normalized, "utf8");
let { sources, sourcesToRecurse } = await getSources(filePath, contents);
for(let source of sources) {
if(!graph.hasNode(source)) {
graph.addNode(source);
}
graph.addDependency(normalized, source);
}
// Recurse for nested deps
for(let source of sourcesToRecurse) {
let recursedGraph = await findGraph(source, alreadyParsedSet);
mergeGraphs(graph, recursedGraph);
}
return graph;
}
module.exports = {
find,
findGraph,
mergeGraphs,
};