/
ExternalModule.ts
103 lines (88 loc) · 2.96 KB
/
ExternalModule.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
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
import ExternalVariable from './ast/variables/ExternalVariable';
import Graph from './Graph';
import { OutputOptions } from './rollup/types';
import { makeLegal } from './utils/identifierHelpers';
import { isAbsolute, normalize, relative } from './utils/path';
export default class ExternalModule {
chunk: void;
declarations: { [name: string]: ExternalVariable };
execIndex: number;
exportedVariables: Map<ExternalVariable, string>;
exportsNames = false;
exportsNamespace = false;
id: string;
moduleSideEffects: boolean;
mostCommonSuggestion = 0;
nameSuggestions: { [name: string]: number };
reexported = false;
renderPath: string = undefined as any;
renormalizeRenderPath = false;
used = false;
variableName: string;
private graph: Graph;
constructor(graph: Graph, id: string, moduleSideEffects: boolean) {
this.graph = graph;
this.id = id;
this.execIndex = Infinity;
this.moduleSideEffects = moduleSideEffects;
const parts = id.split(/[\\/]/);
this.variableName = makeLegal(parts.pop() as string);
this.nameSuggestions = Object.create(null);
this.declarations = Object.create(null);
this.exportedVariables = new Map();
}
getVariableForExportName(name: string, _isExportAllSearch?: boolean): ExternalVariable {
if (name !== 'default' && name !== '*') this.exportsNames = true;
if (name === '*') this.exportsNamespace = true;
let declaration = this.declarations[name];
if (declaration) return declaration;
this.declarations[name] = declaration = new ExternalVariable(this, name);
this.exportedVariables.set(declaration, name);
return declaration;
}
setRenderPath(options: OutputOptions, inputBase: string) {
this.renderPath = '';
if (options.paths) {
this.renderPath =
typeof options.paths === 'function' ? options.paths(this.id) : options.paths[this.id];
}
if (!this.renderPath) {
if (!isAbsolute(this.id)) {
this.renderPath = this.id;
} else {
this.renderPath = normalize(relative(inputBase, this.id));
this.renormalizeRenderPath = true;
}
}
return this.renderPath;
}
suggestName(name: string) {
if (!this.nameSuggestions[name]) this.nameSuggestions[name] = 0;
this.nameSuggestions[name] += 1;
if (this.nameSuggestions[name] > this.mostCommonSuggestion) {
this.mostCommonSuggestion = this.nameSuggestions[name];
this.variableName = name;
}
}
warnUnusedImports() {
const unused = Object.keys(this.declarations).filter(name => {
if (name === '*') return false;
const declaration = this.declarations[name];
return !declaration.included && !this.reexported && !declaration.referenced;
});
if (unused.length === 0) return;
const names =
unused.length === 1
? `'${unused[0]}' is`
: `${unused
.slice(0, -1)
.map(name => `'${name}'`)
.join(', ')} and '${unused.slice(-1)}' are`;
this.graph.warn({
code: 'UNUSED_EXTERNAL_IMPORT',
message: `${names} imported from external module '${this.id}' but never used`,
names: unused,
source: this.id
});
}
}