solid-optimizer 1.0.0-next.8 → 1.0.0
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 +5 -5
- package/dist/{compile-O1Z60VsW.mjs → compile-Cvjy2e11.mjs} +430 -172
- package/dist/compile-Cvjy2e11.mjs.map +1 -0
- package/dist/{compile-X_SMDFr2.d.mts → compile-ioIkBKzR.d.mts} +1 -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 +1 -1
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +55 -14
- package/dist/vite.mjs.map +1 -1
- package/package.json +6 -6
- package/src/compile.ts +6 -8
- package/src/conditional.ts +29 -22
- package/src/context.ts +3 -3
- package/src/copy-size.ts +6 -5
- package/src/fold.ts +161 -53
- package/src/inline.ts +70 -60
- package/src/jsx.ts +16 -14
- package/src/memo.ts +12 -6
- package/src/scope.ts +115 -9
- package/src/text.ts +297 -0
- package/src/vite/index.ts +66 -12
- package/dist/compile-O1Z60VsW.mjs.map +0 -1
- package/dist/compile-X_SMDFr2.d.mts.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/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/vite/index.ts
CHANGED
|
@@ -248,6 +248,41 @@ function stripQuery(id: string): string {
|
|
|
248
248
|
return id.replace(/\?.*$/, '');
|
|
249
249
|
}
|
|
250
250
|
|
|
251
|
+
/**
|
|
252
|
+
* The extensions `options.extensions` registers, and whether each holds
|
|
253
|
+
* TypeScript, read the way `@solidjs/vite-plugin` reads them. A string
|
|
254
|
+
* that contains `tsx` makes every registered extension TypeScript, as it does there.
|
|
255
|
+
* `.tsrx` is left out: the official plugin compiles it on its own path, and
|
|
256
|
+
* the parser here does not read it.
|
|
257
|
+
*/
|
|
258
|
+
function readExtensions(extensions: Options['extensions']): Map<string, boolean> {
|
|
259
|
+
const list = extensions ?? [];
|
|
260
|
+
let anyTSX = false;
|
|
261
|
+
for (const extension of list) {
|
|
262
|
+
if (typeof extension === 'string' && extension.includes('tsx')) {
|
|
263
|
+
anyTSX = true;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
const registered = new Map<string, boolean>();
|
|
267
|
+
for (const extension of list) {
|
|
268
|
+
const [name, extensionOptions] =
|
|
269
|
+
typeof extension === 'string' ? [extension, undefined] : extension;
|
|
270
|
+
if (name === '.tsrx') {
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
const typescript = anyTSX || extensionOptions?.typescript === true;
|
|
274
|
+
registered.set(name, (registered.get(name) ?? false) || typescript);
|
|
275
|
+
}
|
|
276
|
+
return registered;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** The extension of an id, with its leading dot, as the official plugin reads it. */
|
|
280
|
+
function readExtension(id: string): string {
|
|
281
|
+
const file = stripQuery(id);
|
|
282
|
+
const index = file.lastIndexOf('.');
|
|
283
|
+
return index < 0 ? '' : file.slice(index);
|
|
284
|
+
}
|
|
285
|
+
|
|
251
286
|
/**
|
|
252
287
|
* Chains the maps of each step into one, or `null` when the build writes no
|
|
253
288
|
* source maps, since chaining the maps of a large chunk takes a while.
|
|
@@ -429,11 +464,29 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
429
464
|
return client.generate === 'dom' && client.hydratable !== true;
|
|
430
465
|
};
|
|
431
466
|
|
|
467
|
+
/** The extensions the official plugin lowers besides the JSX ones. */
|
|
468
|
+
const extensions = readExtensions(solidOptions.extensions);
|
|
469
|
+
|
|
470
|
+
/** Whether the official plugin lowers the module, so this plugin optimizes it. */
|
|
432
471
|
const isJSXModule = (id: string): boolean => {
|
|
433
472
|
filter ??= createFilter(solidOptions.include, solidOptions.exclude, {
|
|
434
473
|
resolve: config?.root,
|
|
435
474
|
});
|
|
436
|
-
return filter(id) && JSX_MODULE.test(stripQuery(id));
|
|
475
|
+
return filter(id) && (JSX_MODULE.test(stripQuery(id)) || extensions.has(readExtension(id)));
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* The file name the parser reads a module's code under. The parser picks
|
|
480
|
+
* its dialect from the extension, so a registered one borrows `.tsx` or
|
|
481
|
+
* `.jsx`, like the official plugin does when it lowers the module.
|
|
482
|
+
*/
|
|
483
|
+
const parserFilename = (id: string): string => {
|
|
484
|
+
const file = stripQuery(id);
|
|
485
|
+
const typescript = extensions.get(readExtension(id));
|
|
486
|
+
if (typescript === undefined || JSX_MODULE.test(file)) {
|
|
487
|
+
return file;
|
|
488
|
+
}
|
|
489
|
+
return `${file}${typescript ? '.tsx' : '.jsx'}`;
|
|
437
490
|
};
|
|
438
491
|
|
|
439
492
|
/** Modules that keep their JSX, which the bundler must parse as JSX. */
|
|
@@ -605,15 +658,16 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
605
658
|
id: string,
|
|
606
659
|
): Promise<CompileOptions> {
|
|
607
660
|
const filename = stripQuery(id);
|
|
661
|
+
const parsedAs = parserFilename(id);
|
|
608
662
|
// The dev server cannot give back the code of a loaded module.
|
|
609
663
|
if (optimizer.fold === false || !isBuild()) {
|
|
610
|
-
return { ...compileOptions, filename, sourceMap: wantsMaps() };
|
|
664
|
+
return { ...compileOptions, filename: parsedAs, sourceMap: wantsMaps() };
|
|
611
665
|
}
|
|
612
|
-
const { imports } = readModuleConstants(code, { filename });
|
|
666
|
+
const { imports } = readModuleConstants(code, { filename: parsedAs });
|
|
613
667
|
const imported = await importedConstants(context, imports, filename, new Set([filename]), id);
|
|
614
668
|
return {
|
|
615
669
|
...compileOptions,
|
|
616
|
-
filename,
|
|
670
|
+
filename: parsedAs,
|
|
617
671
|
sourceMap: wantsMaps(),
|
|
618
672
|
importedConstants: imported.constants,
|
|
619
673
|
globalConstants: globals,
|
|
@@ -842,7 +896,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
842
896
|
if (code === undefined) {
|
|
843
897
|
return undefined;
|
|
844
898
|
}
|
|
845
|
-
const stripped = await transformWithOxc(code,
|
|
899
|
+
const stripped = await transformWithOxc(code, parserFilename(target), {
|
|
846
900
|
jsx: 'preserve',
|
|
847
901
|
sourcemap: false,
|
|
848
902
|
});
|
|
@@ -1052,7 +1106,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1052
1106
|
// oxlint-disable-next-line no-await-in-loop
|
|
1053
1107
|
const code = await state.load(resolved.id);
|
|
1054
1108
|
const reexport =
|
|
1055
|
-
code === undefined ? undefined : readReexport(code,
|
|
1109
|
+
code === undefined ? undefined : readReexport(code, parserFilename(resolved.id), imported);
|
|
1056
1110
|
if (!reexport) {
|
|
1057
1111
|
break;
|
|
1058
1112
|
}
|
|
@@ -1084,7 +1138,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1084
1138
|
}
|
|
1085
1139
|
const component = readExportedComponent(
|
|
1086
1140
|
loaded,
|
|
1087
|
-
|
|
1141
|
+
parserFilename(resolved.id),
|
|
1088
1142
|
imported,
|
|
1089
1143
|
moduleSources,
|
|
1090
1144
|
);
|
|
@@ -1159,7 +1213,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1159
1213
|
load: (target: string) => Promise<string | undefined>,
|
|
1160
1214
|
copySkips?: Skip[],
|
|
1161
1215
|
): Promise<{ code: string; maps: string[]; skipped?: Skip[] } | undefined> {
|
|
1162
|
-
const filename =
|
|
1216
|
+
const filename = parserFilename(id);
|
|
1163
1217
|
const maps: string[] = [];
|
|
1164
1218
|
let skipped: Skip[] | undefined;
|
|
1165
1219
|
const excluded = new Set<string>();
|
|
@@ -1352,7 +1406,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1352
1406
|
source: string,
|
|
1353
1407
|
id: string,
|
|
1354
1408
|
): Promise<Rollup.TransformResult> {
|
|
1355
|
-
const filename =
|
|
1409
|
+
const filename = parserFilename(id);
|
|
1356
1410
|
const solidCompiler = getCompiler();
|
|
1357
1411
|
const maps: (string | null | undefined)[] = [];
|
|
1358
1412
|
let code = source;
|
|
@@ -1458,7 +1512,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1458
1512
|
maps.push(protectedRefs.map);
|
|
1459
1513
|
}
|
|
1460
1514
|
|
|
1461
|
-
code = await resolveLazyModuleUrls(context, code,
|
|
1515
|
+
code = await resolveLazyModuleUrls(context, code, stripQuery(id));
|
|
1462
1516
|
preserved.add(id);
|
|
1463
1517
|
return { code, map: combineMaps(wantsMaps(), maps) };
|
|
1464
1518
|
}
|
|
@@ -1507,7 +1561,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1507
1561
|
if ((chunkMode() && isClient(this)) || !isJSXModule(id)) {
|
|
1508
1562
|
return null;
|
|
1509
1563
|
}
|
|
1510
|
-
const filename =
|
|
1564
|
+
const filename = parserFilename(id);
|
|
1511
1565
|
const sourceOptions = await moduleOptions(this, source, id);
|
|
1512
1566
|
const result = compile(source, sourceOptions);
|
|
1513
1567
|
let code = result.code;
|
|
@@ -1684,7 +1738,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1684
1738
|
}
|
|
1685
1739
|
// `vite:oxc` prints JSX like Rolldown does, without the parentheses a
|
|
1686
1740
|
// comma expression needs in a JSX expression container.
|
|
1687
|
-
const repaired = repairJSXSequences(code,
|
|
1741
|
+
const repaired = repairJSXSequences(code, parserFilename(id));
|
|
1688
1742
|
return repaired
|
|
1689
1743
|
? { code: repaired.code, map: repaired.map, moduleType: 'jsx' }
|
|
1690
1744
|
: { code, moduleType: 'jsx' };
|