summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/dependency-tree-esm/main.js
blob: ad2e0d0786a974e7b3e0eb3055cc8fdec98c7339 (plain)
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
const path = require("path");
const { existsSync } = require("fs");
const { readFile } = require("fs/promises");

const acorn = require("acorn");
const normalizePath = require("normalize-path");
const { TemplatePath } = require("@11ty/eleventy-utils");

// 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 findByContents(contents, filePath, alreadyParsedSet) {
	// Should we use dependency-graph for these relationships?
	let sources = new Set();
	let nestedSources = 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);

				// Right now only `css` and `json` are valid but others might come later
				if(!importAttributeType) {
					nestedSources.add(normalized);
				}
			}
		}
	}

	// Recurse for nested deps
	for(let source of nestedSources) {
		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);
}

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 = await readFile(normalized, { encoding: 'utf8' });
	let sources = await findByContents(contents, normalized, alreadyParsedSet);

	return sources;
}

module.exports = {
	find
};