solid-optimizer 1.0.0-next.1 → 1.0.0-next.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -13
- package/dist/{compile-BpoiBa6b.mjs → compile-Cvjy2e11.mjs} +1910 -387
- package/dist/compile-Cvjy2e11.mjs.map +1 -0
- package/dist/{compile-BRCj4UN7.d.mts → compile-ioIkBKzR.d.mts} +15 -2
- package/dist/compile-ioIkBKzR.d.mts.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts +12 -2
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +401 -116
- package/dist/vite.mjs.map +1 -1
- package/package.json +5 -2
- package/src/ast.ts +4 -3
- package/src/compile.ts +93 -14
- package/src/conditional.ts +547 -0
- package/src/constants.ts +31 -5
- package/src/context.ts +5 -3
- package/src/copy-size.ts +68 -27
- package/src/fold.ts +334 -107
- package/src/inline.ts +832 -177
- package/src/jsx.ts +55 -34
- package/src/memo.ts +309 -38
- package/src/nesting.ts +325 -0
- package/src/provider.ts +62 -40
- package/src/scope.ts +115 -9
- package/src/size.ts +6 -1
- package/src/terminate.ts +66 -0
- package/src/text.ts +297 -0
- package/src/value.ts +32 -15
- package/src/vite/globals.ts +26 -11
- package/src/vite/index.ts +419 -117
- package/src/vite/inject.ts +91 -16
- package/src/vite/report.ts +21 -12
- package/src/vite/runtime.ts +32 -6
- package/src/vite/usage.ts +89 -48
- package/dist/compile-BRCj4UN7.d.mts.map +0 -1
- package/dist/compile-BpoiBa6b.mjs.map +0 -1
package/src/scope.ts
CHANGED
|
@@ -14,6 +14,7 @@ import type {
|
|
|
14
14
|
BindingPattern,
|
|
15
15
|
Class,
|
|
16
16
|
Directive,
|
|
17
|
+
FunctionBody,
|
|
17
18
|
Function as FunctionNode,
|
|
18
19
|
JSXElementName,
|
|
19
20
|
ModuleExportName,
|
|
@@ -69,7 +70,11 @@ export interface ScopeAnalysis {
|
|
|
69
70
|
readonly root: Scope;
|
|
70
71
|
/** What each identifier reference resolves to. `undefined` means a global. */
|
|
71
72
|
readonly references: Map<Node, Binding | undefined>;
|
|
72
|
-
/**
|
|
73
|
+
/**
|
|
74
|
+
* The scope each scope-creating node opens. A function body shares its
|
|
75
|
+
* function's scope, unless a parameter has a default or a computed key: then
|
|
76
|
+
* the body opens its own scope inside the function's, as in the spec.
|
|
77
|
+
*/
|
|
73
78
|
readonly scopes: Map<Node, Scope>;
|
|
74
79
|
/** Every identifier name in the program, for generating names that collide with none. */
|
|
75
80
|
readonly names: Set<string>;
|
|
@@ -123,6 +128,50 @@ export function isWithinScope(scope: Scope, ancestor: Scope): boolean {
|
|
|
123
128
|
return false;
|
|
124
129
|
}
|
|
125
130
|
|
|
131
|
+
/**
|
|
132
|
+
* Whether a parameter list holds an expression: a default value or a computed
|
|
133
|
+
* key, at any depth.
|
|
134
|
+
*/
|
|
135
|
+
function hasParameterExpressions(params: readonly (BindingPattern | ParamPattern)[]): boolean {
|
|
136
|
+
for (const param of params) {
|
|
137
|
+
if (hasPatternExpression(param)) {
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function hasPatternExpression(pattern: BindingPattern | ParamPattern): boolean {
|
|
145
|
+
switch (pattern.type) {
|
|
146
|
+
case 'AssignmentPattern':
|
|
147
|
+
return true;
|
|
148
|
+
case 'ObjectPattern':
|
|
149
|
+
for (const property of pattern.properties) {
|
|
150
|
+
if (property.type === 'RestElement') {
|
|
151
|
+
if (hasPatternExpression(property.argument)) {
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
} else if (property.computed || hasPatternExpression(property.value)) {
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return false;
|
|
159
|
+
case 'ArrayPattern':
|
|
160
|
+
for (const element of pattern.elements) {
|
|
161
|
+
if (element && hasPatternExpression(element)) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return false;
|
|
166
|
+
case 'RestElement':
|
|
167
|
+
return hasPatternExpression(pattern.argument);
|
|
168
|
+
case 'TSParameterProperty':
|
|
169
|
+
return hasPatternExpression(pattern.parameter);
|
|
170
|
+
default:
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
126
175
|
function exportName(name: ModuleExportName): string {
|
|
127
176
|
return name.type === 'Literal' ? name.value : name.name;
|
|
128
177
|
}
|
|
@@ -138,6 +187,9 @@ class Analyzer {
|
|
|
138
187
|
|
|
139
188
|
private readonly pending: PendingReference[] = [];
|
|
140
189
|
|
|
190
|
+
/** Function bodies with a scope split from the scope of their parameters. */
|
|
191
|
+
private readonly bodies = new Set<Node>();
|
|
192
|
+
|
|
141
193
|
constructor(program: Program) {
|
|
142
194
|
this.root = {
|
|
143
195
|
node: program,
|
|
@@ -191,6 +243,12 @@ class Analyzer {
|
|
|
191
243
|
while (!target.isFunction && target.parent) {
|
|
192
244
|
target = target.parent;
|
|
193
245
|
}
|
|
246
|
+
// A body `var` named like a parameter starts with the parameter's value.
|
|
247
|
+
// It is kept as one binding with the parameter, which the redeclaration
|
|
248
|
+
// below marks as mutated, so neither is folded.
|
|
249
|
+
if (this.bodies.has(target.node) && target.parent?.bindings.has(identifier.name)) {
|
|
250
|
+
target = target.parent;
|
|
251
|
+
}
|
|
194
252
|
}
|
|
195
253
|
this.names.add(identifier.name);
|
|
196
254
|
const existing = target.bindings.get(identifier.name);
|
|
@@ -324,12 +382,28 @@ class Analyzer {
|
|
|
324
382
|
this.declarePattern(param, 'param', node);
|
|
325
383
|
}
|
|
326
384
|
if (node.body) {
|
|
327
|
-
this.
|
|
328
|
-
this.visitStatements(node.body.body);
|
|
385
|
+
this.visitBody(node.body, node.params);
|
|
329
386
|
}
|
|
330
387
|
});
|
|
331
388
|
}
|
|
332
389
|
|
|
390
|
+
/**
|
|
391
|
+
* Visits the block body of a function. When a parameter holds an expression,
|
|
392
|
+
* the body gets its own scope, so the expression cannot see the body's
|
|
393
|
+
* declarations.
|
|
394
|
+
*/
|
|
395
|
+
private visitBody(body: FunctionBody, params: ParamPattern[]): void {
|
|
396
|
+
if (!hasParameterExpressions(params)) {
|
|
397
|
+
this.scopes.set(body, this.scope);
|
|
398
|
+
this.visitStatements(body.body);
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
this.bodies.add(body);
|
|
402
|
+
this.withScope(body, true, () => {
|
|
403
|
+
this.visitStatements(body.body);
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
|
|
333
407
|
private visitClass(node: Class): void {
|
|
334
408
|
for (const decorator of node.decorators) {
|
|
335
409
|
this.visit(decorator);
|
|
@@ -423,8 +497,7 @@ class Analyzer {
|
|
|
423
497
|
this.declarePattern(param, 'param', node);
|
|
424
498
|
}
|
|
425
499
|
if (node.body.type === 'BlockStatement') {
|
|
426
|
-
this.
|
|
427
|
-
this.visitStatements(node.body.body);
|
|
500
|
+
this.visitBody(node.body, node.params);
|
|
428
501
|
} else {
|
|
429
502
|
this.visit(node.body);
|
|
430
503
|
}
|
|
@@ -570,15 +643,48 @@ class Analyzer {
|
|
|
570
643
|
this.visitJSXName(node.closingElement.name);
|
|
571
644
|
}
|
|
572
645
|
break;
|
|
573
|
-
case 'TSEnumDeclaration':
|
|
574
646
|
case 'TSImportEqualsDeclaration':
|
|
575
647
|
this.declare(node.id, 'other', node);
|
|
576
648
|
break;
|
|
577
|
-
case '
|
|
578
|
-
|
|
579
|
-
|
|
649
|
+
case 'TSEnumDeclaration':
|
|
650
|
+
this.declare(node.id, 'other', node);
|
|
651
|
+
// A `declare enum` is stripped with the types.
|
|
652
|
+
if (node.declare) {
|
|
653
|
+
break;
|
|
580
654
|
}
|
|
655
|
+
// An initializer can read earlier members by name, so they shadow outer bindings.
|
|
656
|
+
this.withScope(node, false, () => {
|
|
657
|
+
for (const member of node.body.members) {
|
|
658
|
+
if (member.id.type === 'Identifier') {
|
|
659
|
+
this.declare(member.id, 'other', member);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
for (const member of node.body.members) {
|
|
663
|
+
if (member.initializer) {
|
|
664
|
+
this.visit(member.initializer);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
});
|
|
581
668
|
break;
|
|
669
|
+
case 'TSModuleDeclaration': {
|
|
670
|
+
// `namespace A.B {}` declares `A`. `declare module "m" {}` declares nothing.
|
|
671
|
+
let id: Node = node.id;
|
|
672
|
+
while (id.type === 'TSQualifiedName') {
|
|
673
|
+
id = id.left;
|
|
674
|
+
}
|
|
675
|
+
if (id.type === 'Identifier') {
|
|
676
|
+
this.declare(id, 'other', node);
|
|
677
|
+
}
|
|
678
|
+
// A `declare namespace` or `declare module` is stripped with the types.
|
|
679
|
+
// The body of any other one runs, and a `var` in it stays inside.
|
|
680
|
+
const { body } = node;
|
|
681
|
+
if (!node.declare && body) {
|
|
682
|
+
this.withScope(node, true, () => {
|
|
683
|
+
this.visitStatements(body.body);
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
break;
|
|
687
|
+
}
|
|
582
688
|
default:
|
|
583
689
|
forEachChild(node, (child) => {
|
|
584
690
|
this.visit(child);
|
package/src/size.ts
CHANGED
|
@@ -248,7 +248,12 @@ export class SizeEstimator {
|
|
|
248
248
|
size += name.length + GETTER + this.jsx(value, false);
|
|
249
249
|
}
|
|
250
250
|
}
|
|
251
|
-
const significant =
|
|
251
|
+
const significant: JSXChild[] = [];
|
|
252
|
+
for (const child of element.children) {
|
|
253
|
+
if (!isInsignificant(child)) {
|
|
254
|
+
significant.push(child);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
252
257
|
if (significant.length > 0) {
|
|
253
258
|
size += GETTER + 'children'.length + this.children(significant);
|
|
254
259
|
}
|
package/src/terminate.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ends each statement that relies on a line break.
|
|
3
|
+
*
|
|
4
|
+
* The other passes replace a read with text that can start with `(` or a
|
|
5
|
+
* template literal, like `(p.close)()` for `props.close()`. After a statement
|
|
6
|
+
* with no semicolon, that text continues the statement instead of starting a
|
|
7
|
+
* new one, so `open()` and `(p.close)()` on separate lines call what `open()`
|
|
8
|
+
* returned. A semicolon keeps the statements the parser found.
|
|
9
|
+
*/
|
|
10
|
+
import type { Node } from 'oxc-parser';
|
|
11
|
+
import { forEachChild } from './ast';
|
|
12
|
+
import type { PassContext } from './context';
|
|
13
|
+
|
|
14
|
+
const TERMINATED = new Set([
|
|
15
|
+
'BreakStatement',
|
|
16
|
+
'ContinueStatement',
|
|
17
|
+
'DebuggerStatement',
|
|
18
|
+
'DoWhileStatement',
|
|
19
|
+
'ExportAllDeclaration',
|
|
20
|
+
'ExpressionStatement',
|
|
21
|
+
'ImportDeclaration',
|
|
22
|
+
'ReturnStatement',
|
|
23
|
+
'ThrowStatement',
|
|
24
|
+
'TSTypeAliasDeclaration',
|
|
25
|
+
'VariableDeclaration',
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
const LOOP_HEADS = new Set(['ForStatement', 'ForInStatement', 'ForOfStatement']);
|
|
29
|
+
|
|
30
|
+
function isTerminated(node: Node, context: PassContext): boolean {
|
|
31
|
+
if (node.type === 'VariableDeclaration') {
|
|
32
|
+
const parent = context.parents.get(node);
|
|
33
|
+
return !(parent && LOOP_HEADS.has(parent.type) && Reflect.get(parent, 'body') !== node);
|
|
34
|
+
}
|
|
35
|
+
if (node.type === 'ExportNamedDeclaration') {
|
|
36
|
+
return !node.declaration;
|
|
37
|
+
}
|
|
38
|
+
if (node.type === 'ExportDefaultDeclaration') {
|
|
39
|
+
const { type } = node.declaration;
|
|
40
|
+
return type !== 'FunctionDeclaration' && type !== 'ClassDeclaration';
|
|
41
|
+
}
|
|
42
|
+
return TERMINATED.has(node.type);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const TRIVIA = /(?:\s|\/\/[^\n\r\u2028\u2029]*|\/\*[\s\S]*?\*\/)*/y;
|
|
46
|
+
|
|
47
|
+
/** Whether code other than the closing brace of a block follows `position`. */
|
|
48
|
+
function hasCodeAfter(code: string, position: number): boolean {
|
|
49
|
+
TRIVIA.lastIndex = position;
|
|
50
|
+
TRIVIA.exec(code);
|
|
51
|
+
return TRIVIA.lastIndex < code.length && code[TRIVIA.lastIndex] !== '}';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function terminateStatements(context: PassContext): boolean {
|
|
55
|
+
const { code, s } = context;
|
|
56
|
+
let changed = false;
|
|
57
|
+
const visit = (node: Node): void => {
|
|
58
|
+
if (isTerminated(node, context) && code[node.end - 1] !== ';' && hasCodeAfter(code, node.end)) {
|
|
59
|
+
s.appendLeft(node.end, ';');
|
|
60
|
+
changed = true;
|
|
61
|
+
}
|
|
62
|
+
forEachChild(node, visit);
|
|
63
|
+
};
|
|
64
|
+
visit(context.program);
|
|
65
|
+
return changed;
|
|
66
|
+
}
|
package/src/text.ts
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import type { DecodedSourceMap, SourceMapSegment } from '@jridgewell/remapping';
|
|
2
|
+
import MagicString from 'magic-string';
|
|
3
|
+
|
|
4
|
+
// Each character of a text has an origin, written as two UTF-16 code units
|
|
5
|
+
// that hold a 32-bit value:
|
|
6
|
+
// - an offset in the pass's input that the character was copied from,
|
|
7
|
+
// - `WRITTEN | offset` for a character a pass wrote at that offset, or
|
|
8
|
+
// - `LOOSE` for a character that takes the offset of the edit it ends up in.
|
|
9
|
+
const WRITTEN = 0x8000_0000;
|
|
10
|
+
const LOOSE = 0xffff_ffff;
|
|
11
|
+
const LOOSE_UNITS = '\uffff\uffff';
|
|
12
|
+
|
|
13
|
+
function encodeOrigin(value: number): string {
|
|
14
|
+
return String.fromCharCode(value >>> 16, value & 0xffff);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function decodeOrigin(origins: string, index: number): number {
|
|
18
|
+
return ((origins.charCodeAt(index * 2) << 16) | origins.charCodeAt(index * 2 + 1)) >>> 0;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function repeatOrigin(value: number, length: number): string {
|
|
22
|
+
return encodeOrigin(value).repeat(length);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The line of the original that holds an offset. */
|
|
26
|
+
function findLine(lineStarts: readonly number[], offset: number): number {
|
|
27
|
+
let low = 0;
|
|
28
|
+
let high = lineStarts.length - 1;
|
|
29
|
+
while (low < high) {
|
|
30
|
+
const middle = (low + high + 1) >> 1;
|
|
31
|
+
if ((lineStarts[middle] ?? 0) <= offset) {
|
|
32
|
+
low = middle;
|
|
33
|
+
} else {
|
|
34
|
+
high = middle - 1;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return low;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// The passes of one compile and the copies of one pass start from the same
|
|
41
|
+
// code, so the last identity is kept.
|
|
42
|
+
let identityCode: string | undefined;
|
|
43
|
+
let identity = '';
|
|
44
|
+
|
|
45
|
+
/** The origins of unedited code: each character its own offset. */
|
|
46
|
+
function createIdentity(code: string): string {
|
|
47
|
+
if (identityCode === code) {
|
|
48
|
+
return identity;
|
|
49
|
+
}
|
|
50
|
+
const chunks: string[] = [];
|
|
51
|
+
const units = new Uint16Array(Math.min(code.length, 4096) * 2);
|
|
52
|
+
for (let start = 0; start < code.length; start += 4096) {
|
|
53
|
+
const end = Math.min(code.length, start + 4096);
|
|
54
|
+
for (let index = start; index < end; index += 1) {
|
|
55
|
+
units[(index - start) * 2] = index >>> 16;
|
|
56
|
+
units[(index - start) * 2 + 1] = index & 0xffff;
|
|
57
|
+
}
|
|
58
|
+
chunks.push(String.fromCharCode(...units.subarray(0, (end - start) * 2)));
|
|
59
|
+
}
|
|
60
|
+
identityCode = code;
|
|
61
|
+
identity = chunks.join('');
|
|
62
|
+
return identity;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Code with the origin of each of its characters, so that the source map
|
|
67
|
+
* can point a copied character back at the one it copies.
|
|
68
|
+
*
|
|
69
|
+
* `origins` is `undefined` when the compile writes no source map.
|
|
70
|
+
*/
|
|
71
|
+
export class Text {
|
|
72
|
+
constructor(
|
|
73
|
+
readonly code: string,
|
|
74
|
+
readonly origins: string | undefined,
|
|
75
|
+
) {}
|
|
76
|
+
|
|
77
|
+
toString(): string {
|
|
78
|
+
return this.code;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export type Content = Text | string;
|
|
83
|
+
|
|
84
|
+
/** The code of a content. */
|
|
85
|
+
export function getCode(content: Content): string {
|
|
86
|
+
return typeof content === 'string' ? content : content.code;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Joins contents into one text. The characters of a string take the offset
|
|
91
|
+
* of the edit that writes the text.
|
|
92
|
+
*/
|
|
93
|
+
export function join(parts: readonly Content[], separator = ''): Text {
|
|
94
|
+
let code = '';
|
|
95
|
+
let origins: string | undefined = '';
|
|
96
|
+
for (let index = 0; index < parts.length; index += 1) {
|
|
97
|
+
const part = parts[index] ?? '';
|
|
98
|
+
if (index > 0 && separator) {
|
|
99
|
+
code += separator;
|
|
100
|
+
if (origins !== undefined) {
|
|
101
|
+
origins += repeatOrigin(LOOSE, separator.length);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (typeof part === 'string') {
|
|
105
|
+
code += part;
|
|
106
|
+
if (origins !== undefined) {
|
|
107
|
+
origins += repeatOrigin(LOOSE, part.length);
|
|
108
|
+
}
|
|
109
|
+
} else {
|
|
110
|
+
code += part.code;
|
|
111
|
+
// A text without origins comes from a compile that writes no map.
|
|
112
|
+
origins = part.origins === undefined ? undefined : origins + part.origins;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return new Text(code, origins);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Builds a text from a template, keeping the origins of the texts it holds. */
|
|
119
|
+
export function joinText(strings: TemplateStringsArray, ...values: readonly Content[]): Text {
|
|
120
|
+
const parts: Content[] = [];
|
|
121
|
+
for (let index = 0; index < strings.length; index += 1) {
|
|
122
|
+
parts.push(strings[index] ?? '');
|
|
123
|
+
if (index < values.length) {
|
|
124
|
+
parts.push(values[index] ?? '');
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return join(parts);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The origins of a content written at an offset. */
|
|
131
|
+
function resolveOrigins(content: Content, at: number): string {
|
|
132
|
+
if (typeof content === 'string') {
|
|
133
|
+
return repeatOrigin(WRITTEN | at, content.length);
|
|
134
|
+
}
|
|
135
|
+
const { origins } = content;
|
|
136
|
+
if (origins === undefined) {
|
|
137
|
+
return repeatOrigin(WRITTEN | at, content.code.length);
|
|
138
|
+
}
|
|
139
|
+
// Only an origin that starts at an even unit is one; an odd match spans two.
|
|
140
|
+
let result = '';
|
|
141
|
+
let from = 0;
|
|
142
|
+
const written = encodeOrigin(WRITTEN | at);
|
|
143
|
+
let found = origins.indexOf(LOOSE_UNITS);
|
|
144
|
+
while (found !== -1) {
|
|
145
|
+
if (found % 2 === 1) {
|
|
146
|
+
found = origins.indexOf(LOOSE_UNITS, found + 1);
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
result += origins.slice(from, found) + written;
|
|
150
|
+
from = found + 2;
|
|
151
|
+
found = origins.indexOf(LOOSE_UNITS, from);
|
|
152
|
+
}
|
|
153
|
+
return from === 0 ? origins : result + origins.slice(from);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Edits code like `MagicString`, and keeps the origin of each character of
|
|
158
|
+
* the result.
|
|
159
|
+
*
|
|
160
|
+
* A second `MagicString` holds the origins, two code units per character,
|
|
161
|
+
* and takes every edit the first one takes.
|
|
162
|
+
*/
|
|
163
|
+
export class Editor {
|
|
164
|
+
private readonly s: MagicString;
|
|
165
|
+
/** The origins, made at the first edit, since most passes change nothing. */
|
|
166
|
+
private o: MagicString | undefined;
|
|
167
|
+
|
|
168
|
+
constructor(
|
|
169
|
+
readonly original: string,
|
|
170
|
+
private readonly tracksOrigins: boolean,
|
|
171
|
+
) {
|
|
172
|
+
this.s = new MagicString(original);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** A new editor on the same original code, which keeps origins when this one does. */
|
|
176
|
+
fresh(): Editor {
|
|
177
|
+
return new Editor(this.original, this.tracksOrigins);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** The origins to edit, or `undefined` when this editor keeps none. */
|
|
181
|
+
private editOrigins(): MagicString | undefined {
|
|
182
|
+
if (this.tracksOrigins && !this.o) {
|
|
183
|
+
this.o = new MagicString(createIdentity(this.original));
|
|
184
|
+
}
|
|
185
|
+
return this.o;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Code that a pass writes in place of what is at `at`, mapped there. */
|
|
189
|
+
written(code: string, at: number): Text {
|
|
190
|
+
return new Text(code, this.tracksOrigins ? repeatOrigin(WRITTEN | at, code.length) : undefined);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
overwrite(start: number, end: number, content: Content): this {
|
|
194
|
+
this.s.overwrite(start, end, getCode(content));
|
|
195
|
+
this.editOrigins()?.overwrite(start * 2, end * 2, resolveOrigins(content, start));
|
|
196
|
+
return this;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
remove(start: number, end: number): this {
|
|
200
|
+
this.s.remove(start, end);
|
|
201
|
+
this.editOrigins()?.remove(start * 2, end * 2);
|
|
202
|
+
return this;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
prependLeft(index: number, content: Content): this {
|
|
206
|
+
this.s.prependLeft(index, getCode(content));
|
|
207
|
+
this.editOrigins()?.prependLeft(index * 2, resolveOrigins(content, index));
|
|
208
|
+
return this;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
prependRight(index: number, content: Content): this {
|
|
212
|
+
this.s.prependRight(index, getCode(content));
|
|
213
|
+
this.editOrigins()?.prependRight(index * 2, resolveOrigins(content, index));
|
|
214
|
+
return this;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
appendLeft(index: number, content: Content): this {
|
|
218
|
+
this.s.appendLeft(index, getCode(content));
|
|
219
|
+
this.editOrigins()?.appendLeft(index * 2, resolveOrigins(content, index));
|
|
220
|
+
return this;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** The edited code of characters `start` to `end` of the original, with origins. */
|
|
224
|
+
slice(start: number, end: number): Text {
|
|
225
|
+
let origins: string | undefined;
|
|
226
|
+
if (this.o) {
|
|
227
|
+
origins = this.o.slice(start * 2, end * 2);
|
|
228
|
+
} else if (this.tracksOrigins) {
|
|
229
|
+
origins = createIdentity(this.original).slice(start * 2, end * 2);
|
|
230
|
+
}
|
|
231
|
+
return new Text(this.s.slice(start, end), origins);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
toString(): string {
|
|
235
|
+
return this.s.toString();
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* A map from the edited code to the original. A copied character maps to
|
|
240
|
+
* the one it copies, and a written run maps to where it was written.
|
|
241
|
+
*/
|
|
242
|
+
generateMap(source: string): DecodedSourceMap {
|
|
243
|
+
const code = this.s.toString();
|
|
244
|
+
const origins = this.o?.toString() ?? createIdentity(this.original);
|
|
245
|
+
|
|
246
|
+
// Where each line of the original starts, to turn an offset into a line and column.
|
|
247
|
+
const lineStarts = [0];
|
|
248
|
+
for (let index = 0; index < this.original.length; index += 1) {
|
|
249
|
+
if (this.original.charCodeAt(index) === 10) {
|
|
250
|
+
lineStarts.push(index + 1);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Each copied character maps to its own, and a written run maps from its
|
|
255
|
+
// first character on each line. A copied run continues on the line of
|
|
256
|
+
// the character before it, so only a jump needs a search.
|
|
257
|
+
const mappings: SourceMapSegment[][] = [[]];
|
|
258
|
+
let line = mappings[0] ?? [];
|
|
259
|
+
let column = 0;
|
|
260
|
+
// No origin is `LOOSE` here, so the first character never continues a run.
|
|
261
|
+
let previous = LOOSE;
|
|
262
|
+
let sourceLine = 0;
|
|
263
|
+
let sourceColumn = 0;
|
|
264
|
+
for (let index = 0; index < code.length; index += 1) {
|
|
265
|
+
const origin = decodeOrigin(origins, index);
|
|
266
|
+
if (origin < WRITTEN) {
|
|
267
|
+
if (origin === previous + 1 && this.original.charCodeAt(previous) !== 10) {
|
|
268
|
+
sourceColumn += 1;
|
|
269
|
+
} else {
|
|
270
|
+
sourceLine = findLine(lineStarts, origin);
|
|
271
|
+
sourceColumn = origin - (lineStarts[sourceLine] ?? 0);
|
|
272
|
+
}
|
|
273
|
+
line.push([column, 0, sourceLine, sourceColumn]);
|
|
274
|
+
} else if (origin !== previous || column === 0) {
|
|
275
|
+
const offset = origin - WRITTEN;
|
|
276
|
+
const written = findLine(lineStarts, offset);
|
|
277
|
+
line.push([column, 0, written, offset - (lineStarts[written] ?? 0)]);
|
|
278
|
+
}
|
|
279
|
+
previous = origin;
|
|
280
|
+
if (code.charCodeAt(index) === 10) {
|
|
281
|
+
line = [];
|
|
282
|
+
mappings.push(line);
|
|
283
|
+
column = 0;
|
|
284
|
+
} else {
|
|
285
|
+
column += 1;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return {
|
|
289
|
+
version: 3,
|
|
290
|
+
file: null,
|
|
291
|
+
names: [],
|
|
292
|
+
sources: [source],
|
|
293
|
+
sourcesContent: [this.original],
|
|
294
|
+
mappings,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
}
|
package/src/value.ts
CHANGED
|
@@ -240,6 +240,15 @@ export function truthiness(node: Node, lookup: ConstantLookup): boolean | undefi
|
|
|
240
240
|
return value ? !!value.value : undefined;
|
|
241
241
|
}
|
|
242
242
|
|
|
243
|
+
function areSideEffectFree(nodes: readonly Node[]): boolean {
|
|
244
|
+
for (const node of nodes) {
|
|
245
|
+
if (!isSideEffectFree(node)) {
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return true;
|
|
250
|
+
}
|
|
251
|
+
|
|
243
252
|
/**
|
|
244
253
|
* Whether skipping `node` entirely loses nothing observable.
|
|
245
254
|
*
|
|
@@ -259,7 +268,7 @@ export function isSideEffectFree(node: Node): boolean {
|
|
|
259
268
|
case 'ParenthesizedExpression':
|
|
260
269
|
return isSideEffectFree(node.expression);
|
|
261
270
|
case 'TemplateLiteral':
|
|
262
|
-
return node.expressions
|
|
271
|
+
return areSideEffectFree(node.expressions);
|
|
263
272
|
case 'UnaryExpression':
|
|
264
273
|
// `delete` changes its target.
|
|
265
274
|
return node.operator !== 'delete' && isSideEffectFree(node.argument);
|
|
@@ -275,7 +284,7 @@ export function isSideEffectFree(node: Node): boolean {
|
|
|
275
284
|
case 'LogicalExpression':
|
|
276
285
|
return isSideEffectFree(node.left) && isSideEffectFree(node.right);
|
|
277
286
|
case 'SequenceExpression':
|
|
278
|
-
return node.expressions
|
|
287
|
+
return areSideEffectFree(node.expressions);
|
|
279
288
|
case 'ConditionalExpression':
|
|
280
289
|
return (
|
|
281
290
|
isSideEffectFree(node.test) &&
|
|
@@ -284,18 +293,24 @@ export function isSideEffectFree(node: Node): boolean {
|
|
|
284
293
|
);
|
|
285
294
|
case 'ArrayExpression':
|
|
286
295
|
// A spread iterates its argument, which is observable.
|
|
287
|
-
|
|
288
|
-
(element)
|
|
289
|
-
|
|
290
|
-
|
|
296
|
+
for (const element of node.elements) {
|
|
297
|
+
if (element !== null && (element.type === 'SpreadElement' || !isSideEffectFree(element))) {
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return true;
|
|
291
302
|
case 'ObjectExpression':
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
property.type
|
|
296
|
-
(
|
|
297
|
-
isSideEffectFree(property.value)
|
|
298
|
-
|
|
303
|
+
for (const property of node.properties) {
|
|
304
|
+
// A spread reads the source's own properties, which a getter observes.
|
|
305
|
+
if (
|
|
306
|
+
property.type !== 'Property' ||
|
|
307
|
+
(property.computed && !isSideEffectFree(property.key)) ||
|
|
308
|
+
!isSideEffectFree(property.value)
|
|
309
|
+
) {
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return true;
|
|
299
314
|
default:
|
|
300
315
|
// A class is never skippable. Evaluating one runs its heritage
|
|
301
316
|
// expression, computed keys, static fields, and static blocks.
|
|
@@ -333,8 +348,10 @@ export function arrayLiteralLength(node: Node): number | undefined {
|
|
|
333
348
|
if (node.type !== 'ArrayExpression') {
|
|
334
349
|
return undefined;
|
|
335
350
|
}
|
|
336
|
-
|
|
337
|
-
|
|
351
|
+
for (const element of node.elements) {
|
|
352
|
+
if (element?.type === 'SpreadElement') {
|
|
353
|
+
return undefined;
|
|
354
|
+
}
|
|
338
355
|
}
|
|
339
356
|
return node.elements.length;
|
|
340
357
|
}
|
package/src/vite/globals.ts
CHANGED
|
@@ -24,18 +24,33 @@ export function sharedGlobals(
|
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
const environments = Object.values(config.environments);
|
|
27
|
-
const defines =
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
const defines: Record<string, unknown>[] = [];
|
|
28
|
+
if (environments.length > 0) {
|
|
29
|
+
for (const environment of environments) {
|
|
30
|
+
defines.push(environment.define ?? config.define ?? {});
|
|
31
|
+
}
|
|
32
|
+
} else {
|
|
33
|
+
defines.push(config.define ?? {});
|
|
34
|
+
}
|
|
35
|
+
const keys = new Set<string>();
|
|
36
|
+
for (const define of defines) {
|
|
37
|
+
for (const key of Object.keys(define)) {
|
|
38
|
+
keys.add(key);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
32
41
|
for (const key of keys) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
42
|
+
// Every environment must define the key as the same value.
|
|
43
|
+
let first: { value: Primitive } | undefined;
|
|
44
|
+
let same = true;
|
|
45
|
+
for (const define of defines) {
|
|
46
|
+
const value = defineValue(define[key]);
|
|
47
|
+
first ??= value;
|
|
48
|
+
if (value === undefined || !Object.is(value.value, first?.value)) {
|
|
49
|
+
same = false;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (first !== undefined && same) {
|
|
39
54
|
globals[key] = first.value;
|
|
40
55
|
}
|
|
41
56
|
}
|