-
Notifications
You must be signed in to change notification settings - Fork 750
Expand file tree
/
Copy pathverify_imports.ts
More file actions
181 lines (157 loc) · 4.61 KB
/
verify_imports.ts
File metadata and controls
181 lines (157 loc) · 4.61 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import type { Plugin } from "vite";
import * as cl from "@std/fmt/colors";
import type { PluginContext } from "rolldown";
import path from "node:path";
import { pathWithRoot } from "../utils.ts";
/** A diagnostic message for an invalid import */
export interface ImportCheckDiagnostic {
type: "warn" | "error";
message: string;
description?: string;
hint?: string;
}
/** A check whether or not an import is valid or not for an environment */
export type ImportCheck = (
id: string,
env: string,
) => ImportCheckDiagnostic | void;
export interface CheckImportOptions {
checks: ImportCheck[];
}
export function checkImports(pluginOptions: CheckImportOptions): Plugin {
function check(
options: CheckImportOptions,
id: string,
env: "server" | "client",
): ImportCheckDiagnostic | undefined {
for (let i = 0; i < options.checks.length; i++) {
const check = options.checks[i];
const result = check(id, env);
if (result) return result;
}
}
let root = "";
let isDev = false;
const seen = new Set<string>();
return {
name: "fresh:check-imports",
sharedDuringBuild: true,
enforce: "pre",
applyToEnvironment() {
return true;
},
config(_, env) {
isDev = env.command === "serve";
},
configResolved(config) {
root = pathWithRoot(config.root);
},
resolveId: {
filter: {
id: [
/^(?!\0|[\\/]@fs[\\/]|fresh-island::|fresh:)/,
/[\\/]node_modules[\\/]/,
],
},
async handler(id, importer) {
if (
importer &&
(importer.startsWith("\0") || importer.includes("node_modules") ||
importer.includes("deno::"))
) {
return;
}
let result: ImportCheckDiagnostic | undefined;
if (id.startsWith(".")) {
const resolved = await this.resolve(id, importer);
if (resolved !== null) {
const key =
`${this.environment.config.consumer}::${resolved.id}::${importer}`;
if (!seen.has(key)) {
result = check(
pluginOptions,
resolved.id,
this.environment.config.consumer,
);
}
seen.add(key);
}
} else {
const key = `${this.environment.config.consumer}::${id}::${importer}`;
if (!seen.has(key)) {
result = check(pluginOptions, id, this.environment.config.consumer);
}
seen.add(key);
}
if (result) {
const label = result.type === "warn"
? cl.inverse(cl.yellow(` WARN `))
: cl.inverse(cl.red(` ERROR `));
// deno-lint-ignore no-console
console.log();
// deno-lint-ignore no-console
console.log();
// deno-lint-ignore no-console
console.log(`${label} ${result.message}`);
// deno-lint-ignore no-console
console.log();
if (importer !== undefined) {
const ancestors = findAncestors(this, importer, isDev);
if (ancestors && ancestors.length > 0) {
// deno-lint-ignore no-console
console.log(
`The specifier ${cl.cyan(`"${id}"`)} was imported in:`,
);
ancestors.forEach((spec) => {
if (path.isAbsolute(spec)) {
spec = path.relative(root, spec);
}
// deno-lint-ignore no-console
console.log(` - ${cl.cyan(spec)}`);
});
// deno-lint-ignore no-console
console.log();
}
}
if (result.hint) {
// deno-lint-ignore no-console
console.log(cl.bold(` hint: `) + result.hint);
// deno-lint-ignore no-console
console.log();
}
if (result.type === "error") {
this.error({
message: result.message,
id: importer,
});
} else {
this.warn({
message: result.message,
id: importer,
});
}
}
},
},
};
}
function findAncestors(
ctx: PluginContext,
id: string,
isDev: boolean,
): string[] | null {
const mod = ctx.getModuleInfo(id);
if (mod === null) return null;
if (isDev || mod.importers.length === 0) {
return [id];
}
for (let i = 0; i < mod.importers.length; i++) {
const importer = mod.importers[i];
const result = findAncestors(ctx, importer, isDev);
if (result !== null) {
result.push(id);
return result;
}
}
return null;
}