-
-
Notifications
You must be signed in to change notification settings - Fork 561
/
generate-exports.js
124 lines (113 loc) · 4.21 KB
/
generate-exports.js
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
import { MODULE_SUFFIX, wrapId } from './helpers';
export function wrapCode(magicString, uses, moduleName) {
const args = `module${uses.exports ? ', exports' : ''}`;
const passedArgs = `${moduleName}${uses.exports ? `, ${moduleName}.exports` : ''}`;
magicString
.trim()
.prepend(`(function (${args}) {\n`)
.append(`\n}(${passedArgs}));`);
}
export function rewriteExportsAndGetExportsBlock(
magicString,
moduleName,
wrapped,
topLevelModuleExportsAssignments,
topLevelExportsAssignmentsByName,
defineCompiledEsmExpressions,
deconflict,
code,
uses,
HELPERS_NAME,
id,
replacesModuleExports
) {
const exportDeclarations = [];
// TODO Lukas maybe introduce an export mode with
// - 'replace'
// - 'wrapped'
// - 'module'
// - 'exports'
// then consider extracting parts that "getExportDeclarations"
if (replacesModuleExports) {
if (topLevelModuleExportsAssignments.length > 0) {
for (const { left } of topLevelModuleExportsAssignments) {
magicString.overwrite(left.start, left.end, `var ${moduleName}`);
}
} else {
exportDeclarations.unshift(`var ${moduleName} = {};`);
}
exportDeclarations.push(
`export { ${moduleName} as __moduleExports };`,
`export { ${moduleName} as default };`
);
} else {
exportDeclarations.push(
`export { exports as __moduleExports } from ${JSON.stringify(wrapId(id, MODULE_SUFFIX))}`
);
if (wrapped) {
if (defineCompiledEsmExpressions.length > 0 || code.indexOf('__esModule') >= 0) {
// eslint-disable-next-line no-param-reassign
uses.commonjsHelpers = true;
exportDeclarations.push(
`export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${moduleName}.exports);`
);
} else {
exportDeclarations.push(`export default ${moduleName}.exports;`);
}
} else {
let deconflictedDefaultExportName;
// TODO Lukas if we have an assignment to module.exports and assignments to exports.foo or usages of exports we need to wrap
// TODO Lukas remove?
// Collect and rewrite module.exports assignments
for (const { left } of topLevelModuleExportsAssignments) {
magicString.overwrite(left.start, left.end, `${moduleName}.exports`);
}
// Collect and rewrite named exports
for (const [exportName, node] of topLevelExportsAssignmentsByName) {
const deconflicted = deconflict(exportName);
magicString.overwrite(node.start, node.left.end, `var ${deconflicted}`);
if (exportName === 'default') {
deconflictedDefaultExportName = deconflicted;
} else {
exportDeclarations.push(
exportName === deconflicted
? `export { ${exportName} };`
: `export { ${deconflicted} as ${exportName} };`
);
}
magicString.appendLeft(
code[node.end] === ';' ? node.end + 1 : node.end,
`\n${moduleName}.exports.${exportName} = ${deconflicted};`
);
}
// Collect and rewrite exports.__esModule assignments
let isRestorableCompiledEsm = false;
for (const expression of defineCompiledEsmExpressions) {
isRestorableCompiledEsm = true;
const moduleExportsExpression =
expression.type === 'CallExpression' ? expression.arguments[0] : expression.left.object;
magicString.overwrite(
moduleExportsExpression.start,
moduleExportsExpression.end,
`${moduleName}.exports`
);
}
if (isRestorableCompiledEsm) {
exportDeclarations.push(
deconflictedDefaultExportName
? `export {${deconflictedDefaultExportName} as default};`
: `export default ${moduleName}.exports;`
);
} else if (deconflictedDefaultExportName && code.indexOf('__esModule') >= 0) {
// eslint-disable-next-line no-param-reassign
uses.commonjsHelpers = true;
exportDeclarations.push(
`export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${moduleName}.exports);`
);
} else {
exportDeclarations.push(`export default ${moduleName}.exports;`);
}
}
}
return `\n\n${exportDeclarations.join('\n')}`;
}