-
Notifications
You must be signed in to change notification settings - Fork 59
/
resolve.ts
75 lines (70 loc) 路 2.24 KB
/
resolve.ts
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
import { Package } from "../package";
import { FatalError } from "../errors";
import { Plugin } from "rollup";
import normalizePath from "normalize-path";
import path from "path";
const allowedExtensionRegex = /\.([tj]sx?|json)$/;
export function resolveErrorsPlugin(
pkg: Package,
warnings: Set<string>,
isUmd: boolean
): Plugin {
return {
name: "resolve-errors",
async resolveId(source, importer) {
let resolved = await this.resolve(source, importer, {
skipSelf: true,
});
if (resolved === null) {
if (!source.startsWith(".")) {
warnings.add(
`"${source}" is imported ${
importer
? `by "${normalizePath(
path.relative(pkg.directory, importer!)
)}"`
: ""
} but the package is not specified in dependencies or peerDependencies`
);
return false;
}
throw new FatalError(
`Could not resolve ${source} ` +
(importer ? `from ${path.relative(pkg.directory, importer)}` : ""),
pkg.name
);
}
if (source.startsWith("\0") || resolved.id.startsWith("\0")) {
return resolved;
}
if (resolved.id.startsWith(pkg.directory)) {
if (!resolved.external && !allowedExtensionRegex.test(resolved.id)) {
warnings.add(
`only .ts, .tsx, .js, .jsx, and .json files can be imported but "${
importer === undefined
? source
: normalizePath(path.relative(path.dirname(importer), source))
}" is imported in ${
importer
? `"${normalizePath(path.relative(pkg.directory, importer))}"`
: "a module"
}`
);
return false;
}
return resolved;
}
if (isUmd) return resolved;
if (resolved.id !== source) {
warnings.add(
`all relative imports in a package should only import modules inside of their package directory but ${
importer
? `"${normalizePath(path.relative(pkg.directory, importer))}"`
: "a module"
} is importing "${source}"`
);
}
return false;
},
};
}