forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExportDefaultDeclaration.ts
171 lines (156 loc) · 5.59 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 type MagicString from 'magic-string';
import {
findFirstOccurrenceOutsideComment,
findNonWhiteSpace,
type NodeRenderOptions,
type RenderOptions
} from '../../utils/renderHelpers';
import { getSystemExportStatement } from '../../utils/systemJsRendering';
import { treeshakeNode } from '../../utils/treeshakeNode';
import type { InclusionContext } from '../ExecutionContext';
import type ModuleScope from '../scopes/ModuleScope';
import type ExportDefaultVariable from '../variables/ExportDefaultVariable';
import ClassDeclaration from './ClassDeclaration';
import FunctionDeclaration from './FunctionDeclaration';
import type Identifier from './Identifier';
import * as NodeType from './NodeType';
import { type ExpressionNode, type IncludeChildren, NodeBase } from './shared/Node';
// The header ends at the first non-white-space after "default"
function getDeclarationStart(code: string, start: number): number {
return findNonWhiteSpace(code, findFirstOccurrenceOutsideComment(code, 'default', start) + 7);
}
function getFunctionIdInsertPosition(code: string, start: number): number {
const declarationEnd =
findFirstOccurrenceOutsideComment(code, 'function', start) + 'function'.length;
code = code.slice(declarationEnd, findFirstOccurrenceOutsideComment(code, '(', declarationEnd));
const generatorStarPos = findFirstOccurrenceOutsideComment(code, '*');
if (generatorStarPos === -1) {
return declarationEnd;
}
return declarationEnd + generatorStarPos + 1;
}
export default class ExportDefaultDeclaration extends NodeBase {
declare declaration: FunctionDeclaration | ClassDeclaration | ExpressionNode;
declare needsBoundaries: true;
declare scope: ModuleScope;
declare type: NodeType.tExportDefaultDeclaration;
declare variable: ExportDefaultVariable;
private declare declarationName: string | undefined;
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
super.include(context, includeChildrenRecursively);
if (includeChildrenRecursively) {
this.scope.context.includeVariableInModule(this.variable);
}
}
initialise(): void {
super.initialise();
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.scope.context.getModuleName(),
this,
this.scope.context
);
this.scope.context.addExport(this);
}
removeAnnotations(code: MagicString) {
this.declaration.removeAnnotations(code);
}
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,
this.declaration.id === null
? getFunctionIdInsertPosition(code.original, declarationStart)
: null,
options
);
} else if (this.declaration instanceof ClassDeclaration) {
this.renderNamedDeclaration(
code,
declarationStart,
this.declaration.id === null
? findFirstOccurrenceOutsideComment(code.original, 'class', start) + 'class'.length
: 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);
}
protected applyDeoptimizations() {}
private renderNamedDeclaration(
code: MagicString,
declarationStart: number,
idInsertPosition: number | null,
options: RenderOptions
): void {
const {
exportNamesByVariable,
format,
snippets: { getPropertyAccess }
} = options;
const name = this.variable.getName(getPropertyAccess);
// Remove `export default`
code.remove(this.start, declarationStart);
if (idInsertPosition !== null) {
code.appendLeft(idInsertPosition, ` ${name}`);
}
if (
format === 'system' &&
this.declaration instanceof ClassDeclaration &&
exportNamesByVariable.has(this.variable)
) {
code.appendLeft(this.end, ` ${getSystemExportStatement([this.variable], options)};`);
}
}
private renderVariableDeclaration(
code: MagicString,
declarationStart: number,
{ format, exportNamesByVariable, snippets: { cnst, getPropertyAccess } }: RenderOptions
): void {
const hasTrailingSemicolon = code.original.charCodeAt(this.end - 1) === 59; /*";"*/
const systemExportNames = format === 'system' && exportNamesByVariable.get(this.variable);
if (systemExportNames) {
code.overwrite(
this.start,
declarationStart,
`${cnst} ${this.variable.getName(getPropertyAccess)} = exports(${JSON.stringify(
systemExportNames[0]
)}, `
);
code.appendRight(
hasTrailingSemicolon ? this.end - 1 : this.end,
')' + (hasTrailingSemicolon ? '' : ';')
);
} else {
code.overwrite(
this.start,
declarationStart,
`${cnst} ${this.variable.getName(getPropertyAccess)} = `
);
if (!hasTrailingSemicolon) {
code.appendLeft(this.end, ';');
}
}
}
}
ExportDefaultDeclaration.prototype.needsBoundaries = true;