/
ExportDefaultDeclaration.ts
171 lines (158 loc) · 5.18 KB
/
ExportDefaultDeclaration.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import MagicString from 'magic-string';
import {
findFirstOccurrenceOutsideComment,
findNonWhiteSpace,
NodeRenderOptions,
RenderOptions
} from '../../utils/renderHelpers';
import { getSystemExportStatement } from '../../utils/systemJsRendering';
import { treeshakeNode } from '../../utils/treeshakeNode';
import { InclusionContext } from '../ExecutionContext';
import ModuleScope from '../scopes/ModuleScope';
import ExportDefaultVariable from '../variables/ExportDefaultVariable';
import ClassDeclaration from './ClassDeclaration';
import FunctionDeclaration from './FunctionDeclaration';
import Identifier from './Identifier';
import * as NodeType from './NodeType';
import { ExpressionNode, IncludeChildren, NodeBase } from './shared/Node';
// The header ends at the first non-white-space after "default"
function getDeclarationStart(code: string, start: number) {
return findNonWhiteSpace(code, findFirstOccurrenceOutsideComment(code, 'default', start) + 7);
}
function getIdInsertPosition(
code: string,
declarationKeyword: string,
endMarker: string,
start: number
) {
const declarationEnd =
findFirstOccurrenceOutsideComment(code, declarationKeyword, start) + declarationKeyword.length;
code = code.slice(
declarationEnd,
findFirstOccurrenceOutsideComment(code, endMarker, declarationEnd)
);
const generatorStarPos = findFirstOccurrenceOutsideComment(code, '*');
if (generatorStarPos === -1) {
return declarationEnd;
}
return declarationEnd + generatorStarPos + 1;
}
export default class ExportDefaultDeclaration extends NodeBase {
declaration!: FunctionDeclaration | ClassDeclaration | ExpressionNode;
needsBoundaries!: true;
scope!: ModuleScope;
type!: NodeType.tExportDefaultDeclaration;
variable!: ExportDefaultVariable;
private declarationName: string | undefined;
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
super.include(context, includeChildrenRecursively);
if (includeChildrenRecursively) {
this.context.includeVariableInModule(this.variable);
}
}
initialise(): void {
const declaration = this.declaration as FunctionDeclaration | ClassDeclaration;
this.declarationName =
(declaration.id && declaration.id.name) || (this.declaration as Identifier).name;
this.variable = this.scope.addExportDefaultDeclaration(
this.declarationName || this.context.getModuleName(),
this,
this.context
);
this.context.addExport(this);
}
render(code: MagicString, options: RenderOptions, nodeRenderOptions?: NodeRenderOptions): void {
const { start, end } = nodeRenderOptions as { end: number; start: number };
const declarationStart = getDeclarationStart(code.original, this.start);
if (this.declaration instanceof FunctionDeclaration) {
this.renderNamedDeclaration(
code,
declarationStart,
'function',
'(',
this.declaration.id === null,
options
);
} else if (this.declaration instanceof ClassDeclaration) {
this.renderNamedDeclaration(
code,
declarationStart,
'class',
'{',
this.declaration.id === null,
options
);
} else if (this.variable.getOriginalVariable() !== this.variable) {
// Remove altogether to prevent re-declaring the same variable
treeshakeNode(this, code, start, end);
return;
} else if (this.variable.included) {
this.renderVariableDeclaration(code, declarationStart, options);
} else {
code.remove(this.start, declarationStart);
this.declaration.render(code, options, {
renderedSurroundingElement: NodeType.ExpressionStatement
});
if (code.original[this.end - 1] !== ';') {
code.appendLeft(this.end, ';');
}
return;
}
this.declaration.render(code, options);
}
private renderNamedDeclaration(
code: MagicString,
declarationStart: number,
declarationKeyword: string,
endMarker: string,
needsId: boolean,
options: RenderOptions
) {
const name = this.variable.getName();
// Remove `export default`
code.remove(this.start, declarationStart);
if (needsId) {
code.appendLeft(
getIdInsertPosition(code.original, declarationKeyword, endMarker, declarationStart),
` ${name}`
);
}
if (
options.format === 'system' &&
this.declaration instanceof ClassDeclaration &&
options.exportNamesByVariable.has(this.variable)
) {
code.appendLeft(this.end, ` ${getSystemExportStatement([this.variable], options)};`);
}
}
private renderVariableDeclaration(
code: MagicString,
declarationStart: number,
options: RenderOptions
) {
const hasTrailingSemicolon = code.original.charCodeAt(this.end - 1) === 59; /*";"*/
const systemExportNames =
options.format === 'system' && options.exportNamesByVariable.get(this.variable);
if (systemExportNames) {
code.overwrite(
this.start,
declarationStart,
`${options.varOrConst} ${this.variable.getName()} = exports('${systemExportNames[0]}', `
);
code.appendRight(
hasTrailingSemicolon ? this.end - 1 : this.end,
')' + (hasTrailingSemicolon ? '' : ';')
);
} else {
code.overwrite(
this.start,
declarationStart,
`${options.varOrConst} ${this.variable.getName()} = `
);
if (!hasTrailingSemicolon) {
code.appendLeft(this.end, ';');
}
}
}
}
ExportDefaultDeclaration.prototype.needsBoundaries = true;