/
getExportBlock.ts
122 lines (113 loc) · 3.8 KB
/
getExportBlock.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { ChunkDependencies, ChunkExports } from '../../Chunk';
export default function getExportBlock(
exports: ChunkExports,
dependencies: ChunkDependencies,
namedExportsMode: boolean,
interop: boolean,
compact: boolean,
t: string,
mechanism = 'return '
) {
const _ = compact ? '' : ' ';
const n = compact ? '' : '\n';
if (!namedExportsMode) {
let local;
exports.some(expt => {
if (expt.exported === 'default') {
local = expt.local;
return true;
}
return false;
});
// search for reexported default otherwise
if (!local) {
dependencies.some(dep => {
if (!dep.reexports) return false;
return dep.reexports.some(expt => {
if (expt.reexported === 'default') {
local = dep.namedExportsMode ? `${dep.name}.${expt.imported}` : dep.name;
return true;
}
return false;
});
});
}
return `${mechanism}${local};`;
}
let exportBlock = '';
// star exports must always output first for precedence
dependencies.forEach(({ name, reexports }) => {
if (reexports && namedExportsMode) {
reexports.forEach(specifier => {
if (specifier.reexported === '*') {
if (!compact && exportBlock) exportBlock += '\n';
if (specifier.needsLiveBinding) {
exportBlock +=
`Object.keys(${name}).forEach(function${_}(k)${_}{${n}` +
`${t}if${_}(k${_}!==${_}'default')${_}Object.defineProperty(exports,${_}k,${_}{${n}` +
`${t}${t}enumerable:${_}true,${n}` +
`${t}${t}get:${_}function${_}()${_}{${n}` +
`${t}${t}${t}return ${name}[k];${n}` +
`${t}${t}}${n}${t}});${n}});`;
} else {
exportBlock +=
`Object.keys(${name}).forEach(function${_}(k)${_}{${n}` +
`${t}if${_}(k${_}!==${_}'default')${_}exports[k]${_}=${_}${name}[k];${n}});`;
}
}
});
}
});
dependencies.forEach(
({ name, imports, reexports, isChunk, namedExportsMode: depNamedExportsMode }) => {
if (reexports && namedExportsMode) {
reexports.forEach(specifier => {
if (specifier.imported === 'default' && !isChunk) {
const exportsNamesOrNamespace =
(imports && imports.some(specifier => specifier.imported !== 'default')) ||
(reexports &&
reexports.some(
specifier => specifier.imported !== 'default' && specifier.imported !== '*'
));
const reexportsDefaultAsDefault =
reexports &&
reexports.some(
specifier => specifier.imported === 'default' && specifier.reexported === 'default'
);
if (exportBlock && !compact) exportBlock += '\n';
if (exportsNamesOrNamespace || reexportsDefaultAsDefault)
exportBlock += `exports.${specifier.reexported}${_}=${_}${name}${
interop !== false ? '__default' : '.default'
};`;
else exportBlock += `exports.${specifier.reexported}${_}=${_}${name};`;
} else if (specifier.imported !== '*') {
if (exportBlock && !compact) exportBlock += '\n';
const importName =
specifier.imported === 'default' && !depNamedExportsMode
? name
: `${name}.${specifier.imported}`;
exportBlock += specifier.needsLiveBinding
? `Object.defineProperty(exports,${_}'${specifier.reexported}',${_}{${n}` +
`${t}enumerable:${_}true,${n}` +
`${t}get:${_}function${_}()${_}{${n}` +
`${t}${t}return ${importName};${n}${t}}${n}});`
: `exports.${specifier.reexported}${_}=${_}${importName};`;
} else if (specifier.reexported !== '*') {
if (exportBlock && !compact) exportBlock += '\n';
exportBlock += `exports.${specifier.reexported}${_}=${_}${name};`;
}
});
}
}
);
exports.forEach(expt => {
const lhs = `exports.${expt.exported}`;
const rhs = expt.local;
if (lhs === rhs) {
return;
}
if (exportBlock && !compact) exportBlock += '\n';
exportBlock += `${lhs}${_}=${_}${rhs};`;
});
return exportBlock;
}