/
getInteropBlock.ts
99 lines (98 loc) · 2.55 KB
/
getInteropBlock.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
import { ModuleDeclarationDependency, ReexportSpecifier } from '../../Chunk';
import { GetInterop } from '../../rollup/types';
import {
defaultInteropHelpersByInteropType,
getDefaultOnlyHelper,
getHelpersBlock,
namespaceInteropHelpersByInteropType
} from '../../utils/interopHelpers';
export default function getInteropBlock(
dependencies: ModuleDeclarationDependency[],
varOrConst: string,
interop: GetInterop,
externalLiveBindings: boolean,
freeze: boolean,
namespaceToStringTag: boolean,
accessedGlobals: Set<string>,
_: string,
n: string,
s: string,
t: string
): string {
const neededInteropHelpers = new Set<string>();
const interopStatements: string[] = [];
const addInteropStatement = (
helperVariableName: string,
helper: string,
dependencyVariableName: string
): void => {
neededInteropHelpers.add(helper);
interopStatements.push(
`${varOrConst} ${helperVariableName}${_}=${_}/*#__PURE__*/${helper}(${dependencyVariableName});`
);
};
for (const {
defaultVariableName,
imports,
id,
isChunk,
name,
namedExportsMode,
namespaceVariableName,
reexports
} of dependencies) {
if (isChunk) {
for (const { imported, reexported } of [
...(imports || []),
...(reexports || [])
] as ReexportSpecifier[]) {
if (imported === '*' && reexported !== '*') {
if (!namedExportsMode) {
addInteropStatement(namespaceVariableName!, getDefaultOnlyHelper(), name);
}
break;
}
}
} else {
const moduleInterop = String(interop(id));
let hasDefault = false;
let hasNamespace = false;
for (const { imported, reexported } of [
...(imports || []),
...(reexports || [])
] as ReexportSpecifier[]) {
let helper: string | undefined | null;
let variableName: string | undefined;
if (imported === 'default') {
if (!hasDefault) {
hasDefault = true;
if (defaultVariableName !== namespaceVariableName) {
variableName = defaultVariableName!;
helper = defaultInteropHelpersByInteropType[moduleInterop];
}
}
} else if (imported === '*' && reexported !== '*') {
if (!hasNamespace) {
hasNamespace = true;
helper = namespaceInteropHelpersByInteropType[moduleInterop];
variableName = namespaceVariableName!;
}
}
if (helper) {
addInteropStatement(variableName!, helper, name);
}
}
}
}
return `${getHelpersBlock(
neededInteropHelpers,
accessedGlobals,
_,
n,
s,
t,
externalLiveBindings,
freeze,
namespaceToStringTag
)}${interopStatements.length > 0 ? `${interopStatements.join(n)}${n}${n}` : ''}`;
}