@teyik0/furin 0.2.0-alpha.5 → 0.3.0-alpha.1
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 +2 -3
- package/dist/adapter/static.d.ts +1 -1
- package/dist/build/bun-build.d.ts +3 -0
- package/dist/build/entry-template.d.ts +2 -0
- package/dist/build/entry-template.ts +3 -0
- package/dist/build/hydrate.d.ts +1 -1
- package/dist/build/index.js +6944 -5067
- package/dist/build/production-instrumentation.d.ts +1 -0
- package/dist/build/route-types.d.ts +1 -1
- package/dist/build/types.d.ts +1 -0
- package/dist/cli/index.js +7874 -5971
- package/dist/client/boundaries.d.ts +4 -4
- package/dist/client/internal/runtime-types.d.ts +30 -0
- package/dist/client/link.d.ts +2 -1
- package/dist/client/link.js +712 -338
- package/dist/client/router/boundary-tree.d.ts +1 -1
- package/dist/client/router/index.d.ts +1 -1
- package/dist/client/router/link-utils.d.ts +5 -1
- package/dist/client/router/sync-catch-up.d.ts +4 -4
- package/dist/client/router/types.d.ts +2 -2
- package/dist/client-only.d.ts +1 -0
- package/dist/client-only.js +1 -0
- package/dist/client.d.ts +108 -111
- package/dist/client.js +16 -17
- package/dist/config.d.ts +1 -1
- package/dist/config.js +2 -2
- package/dist/devtools/devtools-element.d.ts +1 -0
- package/dist/devtools/protocol.d.ts +119 -0
- package/dist/furin.d.ts +7 -6
- package/dist/furin.js +6547 -3187
- package/dist/isomorphic.d.ts +18 -0
- package/dist/plugin/binding-scope.d.ts +2 -0
- package/dist/plugin/dead-code-elimination.d.ts +3 -0
- package/dist/plugin/index.js +933 -423
- package/dist/plugin/transform-client.d.ts +0 -2
- package/dist/plugin/transform-isomorphic.d.ts +9 -0
- package/dist/rsc/build/environment.d.ts +2 -0
- package/dist/rsc/build/index.d.ts +15 -0
- package/dist/rsc/client-codec.d.ts +1 -0
- package/dist/rsc/codec.d.ts +4 -0
- package/dist/rsc/flight-drain.d.ts +11 -0
- package/dist/rsc/render-error.d.ts +17 -0
- package/dist/rsc/server-codec.d.ts +2 -0
- package/dist/rsc/server-codec.js +24 -0
- package/dist/rsc/shared.d.ts +30 -0
- package/dist/rsc/symbols.d.ts +2 -0
- package/dist/rsc/transport-benchmark.d.ts +13 -0
- package/dist/rsc/version.d.ts +7 -0
- package/dist/rsc-client.d.ts +4 -0
- package/dist/rsc-client.js +166 -0
- package/dist/rsc-server.d.ts +11 -0
- package/dist/rsc.d.ts +11 -0
- package/dist/rsc.js +353 -0
- package/dist/server/cache/index.d.ts +2 -2
- package/dist/server/cache/invalidation.d.ts +2 -3
- package/dist/server/cache/isr-ssg.d.ts +5 -1
- package/dist/server/cache/isr.d.ts +9 -0
- package/dist/server/cache/route-cache.d.ts +10 -8
- package/dist/server/cache/store-view.d.ts +7 -7
- package/dist/server/dev-page-plugin.d.ts +4 -0
- package/dist/server/devtools/hub.d.ts +11 -0
- package/dist/server/devtools/instrumentation.d.ts +22 -0
- package/dist/server/devtools/instrumentation.production.d.ts +44 -0
- package/dist/server/devtools/plugin.d.ts +3 -0
- package/dist/server/devtools/request-context.d.ts +6 -0
- package/dist/server/internal.d.ts +2 -0
- package/dist/server/internal.js +3 -3
- package/dist/server/lang-detect.d.ts +1 -0
- package/dist/server/render/assemble.d.ts +4 -0
- package/dist/server/render/element.d.ts +1 -1
- package/dist/server/render/index.d.ts +2 -2
- package/dist/server/render/index.ts +6 -2
- package/dist/server/render/loaders.d.ts +8 -0
- package/dist/server/render/not-found.d.ts +1 -1
- package/dist/server/render/ppr-route.d.ts +8 -0
- package/dist/server/render/route-frame-transport.d.ts +2 -0
- package/dist/server/render/shell.ts +0 -1
- package/dist/server/render/ssr.d.ts +2 -3
- package/dist/server/router/discovery.d.ts +1 -1
- package/dist/server/router/hmr.d.ts +1 -1
- package/dist/server/router/index.d.ts +2 -1
- package/dist/server/router/index.js +23 -23
- package/dist/server/router/index.ts +1 -1
- package/dist/server/router/patterns.d.ts +1 -1
- package/dist/server/router/plugin.d.ts +4 -7
- package/dist/server/router/schema-merge.d.ts +3 -0
- package/dist/server/router/schemas.d.ts +12 -7
- package/dist/server/router/types.d.ts +1 -1
- package/dist/server/runtime-env.js +2 -2
- package/dist/server/sync/adapter.d.ts +45 -26
- package/dist/server/sync/config.d.ts +5 -9
- package/dist/server/sync/fingerprint.d.ts +2 -1
- package/dist/server/sync/index.d.ts +15 -4
- package/dist/server/sync/index.js +2868 -0
- package/dist/server/sync/notifier.d.ts +18 -0
- package/dist/server/sync/plugin.d.ts +53 -54
- package/dist/server/sync/postgres/index.d.ts +19 -0
- package/dist/server/sync/postgres/index.js +252 -0
- package/dist/server/sync/postgres/migrate.d.ts +2 -0
- package/dist/server/sync/postgres/migrate.js +16 -0
- package/dist/server/sync/postgres/migration.sql +66 -0
- package/dist/server/sync/redis/index.d.ts +30 -0
- package/dist/server/sync/redis/index.js +336 -0
- package/dist/server/sync/redis/scripts.d.ts +4 -0
- package/dist/server/sync/response.d.ts +10 -1
- package/dist/server/sync/runtime.d.ts +6 -0
- package/dist/server/sync/sqlite/index.d.ts +20 -0
- package/dist/server/sync/sqlite/index.js +250 -0
- package/dist/server/sync/stream.d.ts +12 -11
- package/dist/server-only.d.ts +1 -0
- package/dist/server-only.js +1 -0
- package/dist/shared/parser.d.ts +2 -0
- package/dist/shared/route-frame.d.ts +49 -0
- package/dist/shared/utils/index.d.ts +1 -1
- package/package.json +54 -12
- package/src/adapter/bun.ts +73 -16
- package/src/adapter/package.ts +3 -1
- package/src/adapter/static.ts +94 -48
- package/src/build/bun-build.ts +11 -0
- package/src/build/client.ts +22 -24
- package/src/build/entry-template.ts +3 -0
- package/src/build/hydrate.ts +33 -8
- package/src/build/index.ts +12 -0
- package/src/build/production-instrumentation.ts +14 -0
- package/src/build/route-types.ts +7 -8
- package/src/build/scan-server.ts +3 -2
- package/src/build/ssg-cache.ts +7 -2
- package/src/build/types.ts +1 -0
- package/src/cli/index.ts +53 -22
- package/src/client/boundaries.tsx +42 -18
- package/src/client/default-screens.tsx +4 -4
- package/src/client/internal/runtime-types.ts +31 -0
- package/src/client/link.tsx +35 -37
- package/src/client/router/boundary-tree.tsx +2 -2
- package/src/client/router/context.ts +13 -9
- package/src/client/router/index.ts +3 -0
- package/src/client/router/link-utils.ts +66 -3
- package/src/client/router/navigation.ts +9 -15
- package/src/client/router/provider.tsx +74 -52
- package/src/client/router/search/index.ts +5 -7
- package/src/client/router/search-store.ts +5 -1
- package/src/client/router/spa-response.ts +3 -4
- package/src/client/router/sync-catch-up.ts +9 -8
- package/src/client/router/types.ts +2 -2
- package/src/client/sync.ts +3 -2
- package/src/client-only.ts +1 -0
- package/src/client.ts +221 -174
- package/src/devtools/devtools-element.js +884 -0
- package/src/devtools/protocol.ts +139 -0
- package/src/furin.ts +185 -96
- package/src/isomorphic.ts +66 -0
- package/src/plugin/binding-scope.ts +158 -0
- package/src/plugin/dead-code-elimination.ts +202 -0
- package/src/plugin/index.ts +14 -18
- package/src/plugin/transform-client.ts +237 -273
- package/src/plugin/transform-isomorphic.ts +464 -0
- package/src/rsc/build/environment.ts +30 -0
- package/src/rsc/build/index.ts +94 -0
- package/src/rsc/client-codec.ts +11 -0
- package/src/rsc/codec.ts +85 -0
- package/src/rsc/flight-drain.ts +68 -0
- package/src/rsc/react-server-dom-webpack.d.ts +23 -0
- package/src/rsc/render-error.ts +120 -0
- package/src/rsc/server-codec.ts +25 -0
- package/src/rsc/shared.tsx +113 -0
- package/src/rsc/symbols.ts +2 -0
- package/src/rsc/transport-benchmark.ts +73 -0
- package/src/rsc/version.ts +57 -0
- package/src/rsc-client.ts +12 -0
- package/src/rsc-server.tsx +91 -0
- package/src/rsc.tsx +75 -0
- package/src/server/auto-invalidate/runtime.ts +32 -6
- package/src/server/cache/dev-loader.ts +70 -10
- package/src/server/cache/index.ts +7 -3
- package/src/server/cache/invalidation.ts +33 -15
- package/src/server/cache/isr-ssg.ts +12 -3
- package/src/server/cache/isr.ts +84 -9
- package/src/server/cache/route-cache.ts +49 -36
- package/src/server/cache/store-view.ts +7 -7
- package/src/server/context-logger.ts +7 -7
- package/src/server/dev-page-plugin.ts +8 -6
- package/src/server/devtools/hub.ts +64 -0
- package/src/server/devtools/instrumentation.production.ts +80 -0
- package/src/server/devtools/instrumentation.ts +72 -0
- package/src/server/devtools/plugin.ts +267 -0
- package/src/server/devtools/request-context.ts +76 -0
- package/src/server/instance.ts +1 -1
- package/src/server/internal.ts +2 -0
- package/src/server/lang-detect.ts +14 -10
- package/src/server/render/assemble.ts +73 -10
- package/src/server/render/element.tsx +11 -8
- package/src/server/render/index.ts +6 -2
- package/src/server/render/isr.ts +154 -110
- package/src/server/render/loaders.ts +259 -45
- package/src/server/render/not-found.ts +10 -15
- package/src/server/render/ppr-route.ts +146 -0
- package/src/server/render/route-frame-transport.ts +62 -0
- package/src/server/render/shell.ts +0 -1
- package/src/server/render/ssg.ts +6 -5
- package/src/server/render/ssr.ts +155 -67
- package/src/server/render/template.ts +2 -1
- package/src/server/router/discovery.ts +39 -22
- package/src/server/router/hmr.ts +15 -13
- package/src/server/router/index.ts +1 -1
- package/src/server/router/patterns.ts +16 -15
- package/src/server/router/plugin.ts +151 -158
- package/src/server/router/schema-merge.ts +52 -0
- package/src/server/router/schemas.ts +55 -73
- package/src/server/router/types.ts +1 -1
- package/src/server/sync/adapter.ts +44 -28
- package/src/server/sync/config.ts +9 -27
- package/src/server/sync/fingerprint.ts +8 -3
- package/src/server/sync/index.ts +37 -13
- package/src/server/sync/notifier.ts +109 -0
- package/src/server/sync/plugin.ts +184 -89
- package/src/server/sync/postgres/index.ts +301 -0
- package/src/server/sync/postgres/migrate.ts +15 -0
- package/src/server/sync/postgres/migration.sql +66 -0
- package/src/server/sync/redis/index.ts +342 -0
- package/src/server/sync/redis/scripts.ts +74 -0
- package/src/server/sync/response.ts +141 -16
- package/src/server/sync/runtime.ts +27 -0
- package/src/server/sync/sql.d.ts +4 -0
- package/src/server/sync/sqlite/index.ts +337 -0
- package/src/server/sync/sqlite/migration.sql +70 -0
- package/src/server/sync/stream.ts +137 -81
- package/src/server-only.ts +1 -0
- package/src/shared/await.tsx +4 -4
- package/src/shared/deferred-ndjson.ts +34 -6
- package/src/shared/digest.ts +2 -2
- package/src/shared/parser.ts +5 -0
- package/src/shared/route-frame.ts +429 -0
- package/src/shared/search-params.ts +7 -7
- package/src/shared/utils/index.ts +5 -3
- package/dist/server/dev-inspector.d.ts +0 -89
- package/dist/server/sync/memory-adapter.d.ts +0 -17
- package/src/server/dev-inspector.ts +0 -60
- package/src/server/sync/memory-adapter.ts +0 -196
package/dist/plugin/index.js
CHANGED
|
@@ -1,4 +1,40 @@
|
|
|
1
1
|
// @bun
|
|
2
|
+
// src/server/lang-detect.ts
|
|
3
|
+
function detectLangFromPath(filePath) {
|
|
4
|
+
if (filePath.endsWith(".d.ts")) {
|
|
5
|
+
return "dts";
|
|
6
|
+
}
|
|
7
|
+
if (filePath.endsWith(".tsx")) {
|
|
8
|
+
return "tsx";
|
|
9
|
+
}
|
|
10
|
+
if (filePath.endsWith(".ts")) {
|
|
11
|
+
return "ts";
|
|
12
|
+
}
|
|
13
|
+
if (filePath.endsWith(".jsx")) {
|
|
14
|
+
return "jsx";
|
|
15
|
+
}
|
|
16
|
+
return "js";
|
|
17
|
+
}
|
|
18
|
+
function detectLoaderFromPath(filePath) {
|
|
19
|
+
const lang = detectLangFromPath(filePath);
|
|
20
|
+
return lang === "dts" ? "ts" : lang;
|
|
21
|
+
}
|
|
22
|
+
var TS_WRAPPER_TYPES = new Set([
|
|
23
|
+
"TSAsExpression",
|
|
24
|
+
"TSSatisfiesExpression",
|
|
25
|
+
"TSTypeAssertion",
|
|
26
|
+
"TSNonNullExpression",
|
|
27
|
+
"TSInstantiationExpression",
|
|
28
|
+
"ParenthesizedExpression"
|
|
29
|
+
]);
|
|
30
|
+
function unwrapTSExpression(node) {
|
|
31
|
+
let current = node;
|
|
32
|
+
while (current && typeof current === "object" && TS_WRAPPER_TYPES.has(current.type) && current.expression && typeof current.expression === "object") {
|
|
33
|
+
current = current.expression;
|
|
34
|
+
}
|
|
35
|
+
return current;
|
|
36
|
+
}
|
|
37
|
+
|
|
2
38
|
// ../../node_modules/.bun/@jridgewell+sourcemap-codec@1.5.5/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.mjs
|
|
3
39
|
var comma = 44;
|
|
4
40
|
var semicolon = 59;
|
|
@@ -87,10 +123,10 @@ function encode(decoded) {
|
|
|
87
123
|
return writer.flush();
|
|
88
124
|
}
|
|
89
125
|
|
|
90
|
-
// ../../node_modules/.bun/magic-string@
|
|
91
|
-
|
|
126
|
+
// ../../node_modules/.bun/magic-string@1.1.0/node_modules/magic-string/dist/index.mjs
|
|
127
|
+
var BitSet = class BitSet2 {
|
|
92
128
|
constructor(arg) {
|
|
93
|
-
this.bits = arg instanceof
|
|
129
|
+
this.bits = arg instanceof BitSet2 ? arg.bits.slice() : [];
|
|
94
130
|
}
|
|
95
131
|
add(n) {
|
|
96
132
|
this.bits[n >> 5] |= 1 << (n & 31);
|
|
@@ -98,9 +134,8 @@ class BitSet {
|
|
|
98
134
|
has(n) {
|
|
99
135
|
return !!(this.bits[n >> 5] & 1 << (n & 31));
|
|
100
136
|
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
class Chunk {
|
|
137
|
+
};
|
|
138
|
+
var Chunk = class Chunk2 {
|
|
104
139
|
constructor(start, end, content) {
|
|
105
140
|
this.start = start;
|
|
106
141
|
this.end = end;
|
|
@@ -110,10 +145,8 @@ class Chunk {
|
|
|
110
145
|
this.content = content;
|
|
111
146
|
this.storeName = false;
|
|
112
147
|
this.edited = false;
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
this.next = null;
|
|
116
|
-
}
|
|
148
|
+
this.previous = null;
|
|
149
|
+
this.next = null;
|
|
117
150
|
}
|
|
118
151
|
appendLeft(content) {
|
|
119
152
|
this.outro += content;
|
|
@@ -122,7 +155,7 @@ class Chunk {
|
|
|
122
155
|
this.intro = this.intro + content;
|
|
123
156
|
}
|
|
124
157
|
clone() {
|
|
125
|
-
const chunk = new
|
|
158
|
+
const chunk = new Chunk2(this.start, this.end, this.original);
|
|
126
159
|
chunk.intro = this.intro;
|
|
127
160
|
chunk.outro = this.outro;
|
|
128
161
|
chunk.content = this.content;
|
|
@@ -134,14 +167,16 @@ class Chunk {
|
|
|
134
167
|
return this.start < index && index < this.end;
|
|
135
168
|
}
|
|
136
169
|
eachNext(fn) {
|
|
137
|
-
|
|
170
|
+
fn(this);
|
|
171
|
+
let chunk = this.next;
|
|
138
172
|
while (chunk) {
|
|
139
173
|
fn(chunk);
|
|
140
174
|
chunk = chunk.next;
|
|
141
175
|
}
|
|
142
176
|
}
|
|
143
177
|
eachPrevious(fn) {
|
|
144
|
-
|
|
178
|
+
fn(this);
|
|
179
|
+
let chunk = this.previous;
|
|
145
180
|
while (chunk) {
|
|
146
181
|
fn(chunk);
|
|
147
182
|
chunk = chunk.previous;
|
|
@@ -177,16 +212,15 @@ class Chunk {
|
|
|
177
212
|
const originalBefore = this.original.slice(0, sliceIndex);
|
|
178
213
|
const originalAfter = this.original.slice(sliceIndex);
|
|
179
214
|
this.original = originalBefore;
|
|
180
|
-
const newChunk = new
|
|
215
|
+
const newChunk = new Chunk2(index, this.end, originalAfter);
|
|
181
216
|
newChunk.outro = this.outro;
|
|
182
217
|
this.outro = "";
|
|
183
218
|
this.end = index;
|
|
184
219
|
if (this.edited) {
|
|
185
220
|
newChunk.edit("", false);
|
|
186
221
|
this.content = "";
|
|
187
|
-
} else
|
|
222
|
+
} else
|
|
188
223
|
this.content = originalBefore;
|
|
189
|
-
}
|
|
190
224
|
newChunk.next = this.next;
|
|
191
225
|
if (newChunk.next)
|
|
192
226
|
newChunk.next.previous = newChunk;
|
|
@@ -203,12 +237,11 @@ class Chunk {
|
|
|
203
237
|
return true;
|
|
204
238
|
const trimmed = this.content.replace(rx, "");
|
|
205
239
|
if (trimmed.length) {
|
|
206
|
-
if (trimmed !== this.content)
|
|
207
|
-
|
|
208
|
-
if (this.edited) {
|
|
240
|
+
if (trimmed !== this.content)
|
|
241
|
+
if (this.edited)
|
|
209
242
|
this.edit(trimmed, this.storeName, true);
|
|
210
|
-
|
|
211
|
-
|
|
243
|
+
else
|
|
244
|
+
this.split(this.start + trimmed.length).edit("", undefined, true);
|
|
212
245
|
return true;
|
|
213
246
|
} else {
|
|
214
247
|
this.edit("", undefined, true);
|
|
@@ -223,13 +256,13 @@ class Chunk {
|
|
|
223
256
|
return true;
|
|
224
257
|
const trimmed = this.content.replace(rx, "");
|
|
225
258
|
if (trimmed.length) {
|
|
226
|
-
if (trimmed !== this.content)
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
259
|
+
if (trimmed !== this.content)
|
|
260
|
+
if (this.edited)
|
|
261
|
+
this.edit(trimmed, this.storeName, true);
|
|
262
|
+
else {
|
|
263
|
+
this.split(this.end - trimmed.length);
|
|
264
|
+
this.edit("", undefined, true);
|
|
230
265
|
}
|
|
231
|
-
this.edit("", undefined, true);
|
|
232
|
-
}
|
|
233
266
|
return true;
|
|
234
267
|
} else {
|
|
235
268
|
this.edit("", undefined, true);
|
|
@@ -238,21 +271,19 @@ class Chunk {
|
|
|
238
271
|
return true;
|
|
239
272
|
}
|
|
240
273
|
}
|
|
241
|
-
}
|
|
274
|
+
};
|
|
242
275
|
function getBtoa() {
|
|
243
|
-
if (typeof globalThis !== "undefined" && typeof globalThis.btoa === "function")
|
|
276
|
+
if (typeof globalThis !== "undefined" && typeof globalThis.btoa === "function")
|
|
244
277
|
return (str) => globalThis.btoa(unescape(encodeURIComponent(str)));
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
}
|
|
278
|
+
const buffer = globalThis["Buffer"];
|
|
279
|
+
if (buffer)
|
|
280
|
+
return (str) => buffer.from(str, "utf-8").toString("base64");
|
|
281
|
+
return () => {
|
|
282
|
+
throw new Error("Unsupported environment: `window.btoa` or `Buffer` should be supported.");
|
|
283
|
+
};
|
|
252
284
|
}
|
|
253
285
|
var btoa = /* @__PURE__ */ getBtoa();
|
|
254
|
-
|
|
255
|
-
class SourceMap {
|
|
286
|
+
var SourceMap = class {
|
|
256
287
|
constructor(properties) {
|
|
257
288
|
this.version = 3;
|
|
258
289
|
this.file = properties.file;
|
|
@@ -260,36 +291,42 @@ class SourceMap {
|
|
|
260
291
|
this.sourcesContent = properties.sourcesContent;
|
|
261
292
|
this.names = properties.names;
|
|
262
293
|
this.mappings = encode(properties.mappings);
|
|
263
|
-
if (typeof properties.x_google_ignoreList !== "undefined")
|
|
294
|
+
if (typeof properties.x_google_ignoreList !== "undefined")
|
|
264
295
|
this.x_google_ignoreList = properties.x_google_ignoreList;
|
|
265
|
-
|
|
266
|
-
if (typeof properties.debugId !== "undefined") {
|
|
296
|
+
if (typeof properties.debugId !== "undefined")
|
|
267
297
|
this.debugId = properties.debugId;
|
|
268
|
-
}
|
|
269
298
|
}
|
|
270
299
|
toString() {
|
|
271
300
|
return JSON.stringify(this);
|
|
272
301
|
}
|
|
273
302
|
toUrl() {
|
|
274
|
-
return
|
|
303
|
+
return `data:application/json;charset=utf-8;base64,${btoa(this.toString())}`;
|
|
275
304
|
}
|
|
276
|
-
}
|
|
277
|
-
function
|
|
278
|
-
const
|
|
305
|
+
};
|
|
306
|
+
function getLocator(source) {
|
|
307
|
+
const originalLines = source.split(`
|
|
279
308
|
`);
|
|
280
|
-
const
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
}
|
|
285
|
-
if (tabbed.length >= spaced.length) {
|
|
286
|
-
return "\t";
|
|
309
|
+
const lineOffsets = [];
|
|
310
|
+
for (let i = 0, pos = 0;i < originalLines.length; i++) {
|
|
311
|
+
lineOffsets.push(pos);
|
|
312
|
+
pos += originalLines[i].length + 1;
|
|
287
313
|
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
314
|
+
return function locate(index) {
|
|
315
|
+
let i = 0;
|
|
316
|
+
let j = lineOffsets.length;
|
|
317
|
+
while (i < j) {
|
|
318
|
+
const m = i + j >> 1;
|
|
319
|
+
if (index < lineOffsets[m])
|
|
320
|
+
j = m;
|
|
321
|
+
else
|
|
322
|
+
i = m + 1;
|
|
323
|
+
}
|
|
324
|
+
const line = i - 1;
|
|
325
|
+
return {
|
|
326
|
+
line,
|
|
327
|
+
column: index - lineOffsets[line]
|
|
328
|
+
};
|
|
329
|
+
};
|
|
293
330
|
}
|
|
294
331
|
function getRelativePath(from, to) {
|
|
295
332
|
const fromParts = from.split(/[/\\]/);
|
|
@@ -306,37 +343,27 @@ function getRelativePath(from, to) {
|
|
|
306
343
|
}
|
|
307
344
|
return fromParts.concat(toParts).join("/");
|
|
308
345
|
}
|
|
346
|
+
function guessIndent(code) {
|
|
347
|
+
const lines = code.split(`
|
|
348
|
+
`);
|
|
349
|
+
const tabbed = lines.filter((line) => /^\t+/.test(line));
|
|
350
|
+
const spaced = lines.filter((line) => /^ {2,}/.test(line));
|
|
351
|
+
if (tabbed.length === 0 && spaced.length === 0)
|
|
352
|
+
return null;
|
|
353
|
+
if (tabbed.length >= spaced.length)
|
|
354
|
+
return "\t";
|
|
355
|
+
const min = spaced.reduce((previous, current) => {
|
|
356
|
+
const numSpaces = /^ +/.exec(current)[0].length;
|
|
357
|
+
return Math.min(numSpaces, previous);
|
|
358
|
+
}, Infinity);
|
|
359
|
+
return " ".repeat(min);
|
|
360
|
+
}
|
|
309
361
|
var toString = Object.prototype.toString;
|
|
310
362
|
function isObject(thing) {
|
|
311
363
|
return toString.call(thing) === "[object Object]";
|
|
312
364
|
}
|
|
313
|
-
function getLocator(source) {
|
|
314
|
-
const originalLines = source.split(`
|
|
315
|
-
`);
|
|
316
|
-
const lineOffsets = [];
|
|
317
|
-
for (let i = 0, pos = 0;i < originalLines.length; i++) {
|
|
318
|
-
lineOffsets.push(pos);
|
|
319
|
-
pos += originalLines[i].length + 1;
|
|
320
|
-
}
|
|
321
|
-
return function locate(index) {
|
|
322
|
-
let i = 0;
|
|
323
|
-
let j = lineOffsets.length;
|
|
324
|
-
while (i < j) {
|
|
325
|
-
const m = i + j >> 1;
|
|
326
|
-
if (index < lineOffsets[m]) {
|
|
327
|
-
j = m;
|
|
328
|
-
} else {
|
|
329
|
-
i = m + 1;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
const line = i - 1;
|
|
333
|
-
const column = index - lineOffsets[line];
|
|
334
|
-
return { line, column };
|
|
335
|
-
};
|
|
336
|
-
}
|
|
337
365
|
var wordRegex = /\w/;
|
|
338
|
-
|
|
339
|
-
class Mappings {
|
|
366
|
+
var Mappings = class {
|
|
340
367
|
constructor(hires) {
|
|
341
368
|
this.hires = hires;
|
|
342
369
|
this.generatedCodeLine = 0;
|
|
@@ -352,10 +379,14 @@ class Mappings {
|
|
|
352
379
|
`, 0);
|
|
353
380
|
let previousContentLineEnd = -1;
|
|
354
381
|
while (contentLineEnd >= 0 && contentLengthMinusOne > contentLineEnd) {
|
|
355
|
-
const segment2 = [
|
|
356
|
-
|
|
382
|
+
const segment2 = [
|
|
383
|
+
this.generatedCodeColumn,
|
|
384
|
+
sourceIndex,
|
|
385
|
+
loc.line,
|
|
386
|
+
loc.column
|
|
387
|
+
];
|
|
388
|
+
if (nameIndex >= 0)
|
|
357
389
|
segment2.push(nameIndex);
|
|
358
|
-
}
|
|
359
390
|
this.rawSegments.push(segment2);
|
|
360
391
|
this.generatedCodeLine += 1;
|
|
361
392
|
this.raw[this.generatedCodeLine] = this.rawSegments = [];
|
|
@@ -364,10 +395,14 @@ class Mappings {
|
|
|
364
395
|
contentLineEnd = content.indexOf(`
|
|
365
396
|
`, contentLineEnd + 1);
|
|
366
397
|
}
|
|
367
|
-
const segment = [
|
|
368
|
-
|
|
398
|
+
const segment = [
|
|
399
|
+
this.generatedCodeColumn,
|
|
400
|
+
sourceIndex,
|
|
401
|
+
loc.line,
|
|
402
|
+
loc.column
|
|
403
|
+
];
|
|
404
|
+
if (nameIndex >= 0)
|
|
369
405
|
segment.push(nameIndex);
|
|
370
|
-
}
|
|
371
406
|
this.rawSegments.push(segment);
|
|
372
407
|
this.advance(content.slice(previousContentLineEnd + 1));
|
|
373
408
|
} else if (this.pending) {
|
|
@@ -392,8 +427,13 @@ class Mappings {
|
|
|
392
427
|
charInHiresBoundary = false;
|
|
393
428
|
} else {
|
|
394
429
|
if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
|
|
395
|
-
const segment = [
|
|
396
|
-
|
|
430
|
+
const segment = [
|
|
431
|
+
this.generatedCodeColumn,
|
|
432
|
+
sourceIndex,
|
|
433
|
+
loc.line,
|
|
434
|
+
loc.column
|
|
435
|
+
];
|
|
436
|
+
if (this.hires === "boundary")
|
|
397
437
|
if (wordRegex.test(original[originalCharIndex])) {
|
|
398
438
|
if (!charInHiresBoundary) {
|
|
399
439
|
this.rawSegments.push(segment);
|
|
@@ -403,9 +443,8 @@ class Mappings {
|
|
|
403
443
|
this.rawSegments.push(segment);
|
|
404
444
|
charInHiresBoundary = false;
|
|
405
445
|
}
|
|
406
|
-
|
|
446
|
+
else
|
|
407
447
|
this.rawSegments.push(segment);
|
|
408
|
-
}
|
|
409
448
|
}
|
|
410
449
|
loc.column += 1;
|
|
411
450
|
this.generatedCodeColumn += 1;
|
|
@@ -429,37 +468,83 @@ class Mappings {
|
|
|
429
468
|
}
|
|
430
469
|
this.generatedCodeColumn += lines[lines.length - 1].length;
|
|
431
470
|
}
|
|
432
|
-
}
|
|
471
|
+
};
|
|
433
472
|
var n = `
|
|
434
473
|
`;
|
|
474
|
+
var NEWLINE_CHAR = 10;
|
|
475
|
+
var CR_CHAR = 13;
|
|
435
476
|
var warned = {
|
|
436
477
|
insertLeft: false,
|
|
437
478
|
insertRight: false,
|
|
438
479
|
storeName: false
|
|
439
480
|
};
|
|
440
|
-
|
|
441
|
-
class MagicString {
|
|
481
|
+
var MagicString = class MagicString2 {
|
|
442
482
|
constructor(string, options = {}) {
|
|
443
483
|
const chunk = new Chunk(0, string.length, string);
|
|
444
484
|
Object.defineProperties(this, {
|
|
445
|
-
original: {
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
485
|
+
original: {
|
|
486
|
+
writable: true,
|
|
487
|
+
value: string
|
|
488
|
+
},
|
|
489
|
+
outro: {
|
|
490
|
+
writable: true,
|
|
491
|
+
value: ""
|
|
492
|
+
},
|
|
493
|
+
intro: {
|
|
494
|
+
writable: true,
|
|
495
|
+
value: ""
|
|
496
|
+
},
|
|
497
|
+
firstChunk: {
|
|
498
|
+
writable: true,
|
|
499
|
+
value: chunk
|
|
500
|
+
},
|
|
501
|
+
lastChunk: {
|
|
502
|
+
writable: true,
|
|
503
|
+
value: chunk
|
|
504
|
+
},
|
|
505
|
+
lastSearchedChunk: {
|
|
506
|
+
writable: true,
|
|
507
|
+
value: chunk
|
|
508
|
+
},
|
|
509
|
+
byStart: {
|
|
510
|
+
writable: true,
|
|
511
|
+
value: {}
|
|
512
|
+
},
|
|
513
|
+
byEnd: {
|
|
514
|
+
writable: true,
|
|
515
|
+
value: {}
|
|
516
|
+
},
|
|
517
|
+
filename: {
|
|
518
|
+
writable: true,
|
|
519
|
+
value: options.filename
|
|
520
|
+
},
|
|
521
|
+
indentExclusionRanges: {
|
|
522
|
+
writable: true,
|
|
523
|
+
value: options.indentExclusionRanges
|
|
524
|
+
},
|
|
525
|
+
sourcemapLocations: {
|
|
526
|
+
writable: true,
|
|
527
|
+
value: new BitSet
|
|
528
|
+
},
|
|
529
|
+
storedNames: {
|
|
530
|
+
writable: true,
|
|
531
|
+
value: {}
|
|
532
|
+
},
|
|
533
|
+
indentStr: {
|
|
534
|
+
writable: true,
|
|
535
|
+
value: undefined
|
|
536
|
+
},
|
|
537
|
+
ignoreList: {
|
|
538
|
+
writable: true,
|
|
539
|
+
value: options.ignoreList
|
|
540
|
+
},
|
|
541
|
+
offset: {
|
|
542
|
+
writable: true,
|
|
543
|
+
value: options.offset || 0
|
|
544
|
+
}
|
|
460
545
|
});
|
|
461
|
-
this.byStart[0
|
|
462
|
-
this.byEnd[string.length
|
|
546
|
+
this.byStart = /* @__PURE__ */ new Map([[0, chunk]]);
|
|
547
|
+
this.byEnd = /* @__PURE__ */ new Map([[string.length, chunk]]);
|
|
463
548
|
}
|
|
464
549
|
addSourcemapLocation(char) {
|
|
465
550
|
this.sourcemapLocations.add(char);
|
|
@@ -475,12 +560,11 @@ class MagicString {
|
|
|
475
560
|
if (typeof content !== "string")
|
|
476
561
|
throw new TypeError("inserted content must be a string");
|
|
477
562
|
this._split(index);
|
|
478
|
-
const chunk = this.byEnd
|
|
479
|
-
if (chunk)
|
|
563
|
+
const chunk = this.byEnd.get(index);
|
|
564
|
+
if (chunk)
|
|
480
565
|
chunk.appendLeft(content);
|
|
481
|
-
|
|
566
|
+
else
|
|
482
567
|
this.intro += content;
|
|
483
|
-
}
|
|
484
568
|
return this;
|
|
485
569
|
}
|
|
486
570
|
appendRight(index, content) {
|
|
@@ -488,21 +572,23 @@ class MagicString {
|
|
|
488
572
|
if (typeof content !== "string")
|
|
489
573
|
throw new TypeError("inserted content must be a string");
|
|
490
574
|
this._split(index);
|
|
491
|
-
const chunk = this.byStart
|
|
492
|
-
if (chunk)
|
|
575
|
+
const chunk = this.byStart.get(index);
|
|
576
|
+
if (chunk)
|
|
493
577
|
chunk.appendRight(content);
|
|
494
|
-
|
|
578
|
+
else
|
|
495
579
|
this.outro += content;
|
|
496
|
-
}
|
|
497
580
|
return this;
|
|
498
581
|
}
|
|
499
582
|
clone() {
|
|
500
|
-
const cloned = new
|
|
583
|
+
const cloned = new MagicString2(this.original, {
|
|
584
|
+
filename: this.filename,
|
|
585
|
+
offset: this.offset
|
|
586
|
+
});
|
|
501
587
|
let originalChunk = this.firstChunk;
|
|
502
588
|
let clonedChunk = cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone();
|
|
503
589
|
while (originalChunk) {
|
|
504
|
-
cloned.byStart
|
|
505
|
-
cloned.byEnd
|
|
590
|
+
cloned.byStart.set(clonedChunk.start, clonedChunk);
|
|
591
|
+
cloned.byEnd.set(clonedChunk.end, clonedChunk);
|
|
506
592
|
const nextOriginalChunk = originalChunk.next;
|
|
507
593
|
const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
|
|
508
594
|
if (nextClonedChunk) {
|
|
@@ -513,9 +599,8 @@ class MagicString {
|
|
|
513
599
|
originalChunk = nextOriginalChunk;
|
|
514
600
|
}
|
|
515
601
|
cloned.lastChunk = clonedChunk;
|
|
516
|
-
if (this.indentExclusionRanges)
|
|
602
|
+
if (this.indentExclusionRanges)
|
|
517
603
|
cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
|
|
518
|
-
}
|
|
519
604
|
cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
|
|
520
605
|
cloned.intro = this.intro;
|
|
521
606
|
cloned.outro = this.outro;
|
|
@@ -527,29 +612,24 @@ class MagicString {
|
|
|
527
612
|
const names = Object.keys(this.storedNames);
|
|
528
613
|
const mappings = new Mappings(options.hires);
|
|
529
614
|
const locate = getLocator(this.original);
|
|
530
|
-
if (this.intro)
|
|
615
|
+
if (this.intro)
|
|
531
616
|
mappings.advance(this.intro);
|
|
532
|
-
}
|
|
533
617
|
this.firstChunk.eachNext((chunk) => {
|
|
534
618
|
const loc = locate(chunk.start);
|
|
535
619
|
if (chunk.intro.length)
|
|
536
620
|
mappings.advance(chunk.intro);
|
|
537
|
-
if (chunk.edited)
|
|
621
|
+
if (chunk.edited)
|
|
538
622
|
mappings.addEdit(sourceIndex, chunk.content, loc, chunk.storeName ? names.indexOf(chunk.original) : -1);
|
|
539
|
-
|
|
623
|
+
else
|
|
540
624
|
mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
|
|
541
|
-
}
|
|
542
625
|
if (chunk.outro.length)
|
|
543
626
|
mappings.advance(chunk.outro);
|
|
544
627
|
});
|
|
545
|
-
if (this.outro)
|
|
628
|
+
if (this.outro)
|
|
546
629
|
mappings.advance(this.outro);
|
|
547
|
-
}
|
|
548
630
|
return {
|
|
549
631
|
file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
|
|
550
|
-
sources: [
|
|
551
|
-
options.source ? getRelativePath(options.file || "", options.source) : options.file || ""
|
|
552
|
-
],
|
|
632
|
+
sources: [options.source ? getRelativePath(options.file || "", options.source) : options.file || ""],
|
|
553
633
|
sourcesContent: options.includeContent ? [this.original] : undefined,
|
|
554
634
|
names,
|
|
555
635
|
mappings: mappings.raw,
|
|
@@ -560,9 +640,8 @@ class MagicString {
|
|
|
560
640
|
return new SourceMap(this.generateDecodedMap(options));
|
|
561
641
|
}
|
|
562
642
|
_ensureindentStr() {
|
|
563
|
-
if (this.indentStr === undefined)
|
|
643
|
+
if (this.indentStr === undefined)
|
|
564
644
|
this.indentStr = guessIndent(this.original);
|
|
565
|
-
}
|
|
566
645
|
}
|
|
567
646
|
_getRawIndentString() {
|
|
568
647
|
this._ensureindentStr();
|
|
@@ -584,57 +663,74 @@ class MagicString {
|
|
|
584
663
|
}
|
|
585
664
|
if (indentStr === "")
|
|
586
665
|
return this;
|
|
666
|
+
const resolvedIndentStr = indentStr;
|
|
587
667
|
options = options || {};
|
|
588
668
|
const isExcluded = {};
|
|
589
|
-
if (options.exclude)
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
for (let i = exclusion[0];i < exclusion[1]; i += 1) {
|
|
669
|
+
if (options.exclude)
|
|
670
|
+
(typeof options.exclude[0] === "number" ? [options.exclude] : options.exclude).forEach((exclusion) => {
|
|
671
|
+
for (let i = exclusion[0];i < exclusion[1]; i += 1)
|
|
593
672
|
isExcluded[i] = true;
|
|
594
|
-
}
|
|
595
673
|
});
|
|
596
|
-
}
|
|
597
674
|
let shouldIndentNextCharacter = options.indentStart !== false;
|
|
598
675
|
const replacer = (match) => {
|
|
599
676
|
if (shouldIndentNextCharacter)
|
|
600
|
-
return `${
|
|
677
|
+
return `${resolvedIndentStr}${match}`;
|
|
601
678
|
shouldIndentNextCharacter = true;
|
|
602
679
|
return match;
|
|
603
680
|
};
|
|
604
681
|
this.intro = this.intro.replace(pattern, replacer);
|
|
605
682
|
let charIndex = 0;
|
|
606
683
|
let chunk = this.firstChunk;
|
|
684
|
+
const indentAt = (index) => {
|
|
685
|
+
shouldIndentNextCharacter = false;
|
|
686
|
+
if (index === chunk.start)
|
|
687
|
+
chunk.prependRight(resolvedIndentStr);
|
|
688
|
+
else {
|
|
689
|
+
this._splitChunk(chunk, index);
|
|
690
|
+
chunk = chunk.next;
|
|
691
|
+
chunk.prependRight(resolvedIndentStr);
|
|
692
|
+
}
|
|
693
|
+
};
|
|
607
694
|
while (chunk) {
|
|
608
695
|
const end = chunk.end;
|
|
609
696
|
if (chunk.edited) {
|
|
610
697
|
if (!isExcluded[charIndex]) {
|
|
611
698
|
chunk.content = chunk.content.replace(pattern, replacer);
|
|
612
|
-
if (chunk.content.length)
|
|
699
|
+
if (chunk.content.length)
|
|
613
700
|
shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === `
|
|
614
701
|
`;
|
|
615
|
-
}
|
|
616
702
|
}
|
|
617
|
-
} else {
|
|
703
|
+
} else if (options.exclude) {
|
|
618
704
|
charIndex = chunk.start;
|
|
619
705
|
while (charIndex < end) {
|
|
620
706
|
if (!isExcluded[charIndex]) {
|
|
621
|
-
const char = this.original
|
|
622
|
-
if (char ===
|
|
623
|
-
`) {
|
|
707
|
+
const char = this.original.charCodeAt(charIndex);
|
|
708
|
+
if (char === NEWLINE_CHAR)
|
|
624
709
|
shouldIndentNextCharacter = true;
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
if (charIndex === chunk.start) {
|
|
628
|
-
chunk.prependRight(indentStr);
|
|
629
|
-
} else {
|
|
630
|
-
this._splitChunk(chunk, charIndex);
|
|
631
|
-
chunk = chunk.next;
|
|
632
|
-
chunk.prependRight(indentStr);
|
|
633
|
-
}
|
|
634
|
-
}
|
|
710
|
+
else if (char !== CR_CHAR && shouldIndentNextCharacter)
|
|
711
|
+
indentAt(charIndex);
|
|
635
712
|
}
|
|
636
713
|
charIndex += 1;
|
|
637
714
|
}
|
|
715
|
+
} else {
|
|
716
|
+
charIndex = chunk.start;
|
|
717
|
+
while (charIndex < end) {
|
|
718
|
+
if (!shouldIndentNextCharacter) {
|
|
719
|
+
const nextLine = this.original.indexOf(n, charIndex);
|
|
720
|
+
if (nextLine === -1 || nextLine >= end)
|
|
721
|
+
break;
|
|
722
|
+
shouldIndentNextCharacter = true;
|
|
723
|
+
charIndex = nextLine + 1;
|
|
724
|
+
continue;
|
|
725
|
+
}
|
|
726
|
+
const char = this.original.charCodeAt(charIndex);
|
|
727
|
+
if (char === NEWLINE_CHAR || char === CR_CHAR) {
|
|
728
|
+
charIndex += 1;
|
|
729
|
+
continue;
|
|
730
|
+
}
|
|
731
|
+
indentAt(charIndex);
|
|
732
|
+
charIndex += 1;
|
|
733
|
+
}
|
|
638
734
|
}
|
|
639
735
|
charIndex = chunk.end;
|
|
640
736
|
chunk = chunk.next;
|
|
@@ -663,16 +759,18 @@ class MagicString {
|
|
|
663
759
|
start = start + this.offset;
|
|
664
760
|
end = end + this.offset;
|
|
665
761
|
index = index + this.offset;
|
|
762
|
+
if (start === end)
|
|
763
|
+
return this;
|
|
666
764
|
if (index >= start && index <= end)
|
|
667
765
|
throw new Error("Cannot move a selection inside itself");
|
|
668
766
|
this._split(start);
|
|
669
767
|
this._split(end);
|
|
670
768
|
this._split(index);
|
|
671
|
-
const first = this.byStart
|
|
672
|
-
const last = this.byEnd
|
|
769
|
+
const first = this.byStart.get(start);
|
|
770
|
+
const last = this.byEnd.get(end);
|
|
673
771
|
const oldLeft = first.previous;
|
|
674
772
|
const oldRight = last.next;
|
|
675
|
-
const newRight = this.byStart
|
|
773
|
+
const newRight = this.byStart.get(index);
|
|
676
774
|
if (!newRight && last === this.lastChunk)
|
|
677
775
|
return this;
|
|
678
776
|
const newLeft = newRight ? newRight.previous : this.lastChunk;
|
|
@@ -699,8 +797,11 @@ class MagicString {
|
|
|
699
797
|
return this;
|
|
700
798
|
}
|
|
701
799
|
overwrite(start, end, content, options) {
|
|
702
|
-
|
|
703
|
-
return this.update(start, end, content, {
|
|
800
|
+
const optionObject = typeof options === "object" && options ? options : {};
|
|
801
|
+
return this.update(start, end, content, {
|
|
802
|
+
...optionObject,
|
|
803
|
+
overwrite: !optionObject.contentOnly
|
|
804
|
+
});
|
|
704
805
|
}
|
|
705
806
|
update(start, end, content, options) {
|
|
706
807
|
start = start + this.offset;
|
|
@@ -726,8 +827,9 @@ class MagicString {
|
|
|
726
827
|
}
|
|
727
828
|
options = { storeName: true };
|
|
728
829
|
}
|
|
729
|
-
const
|
|
730
|
-
const
|
|
830
|
+
const optionObject = typeof options === "object" && options ? options : {};
|
|
831
|
+
const storeName = optionObject.storeName || false;
|
|
832
|
+
const overwrite = optionObject.overwrite || false;
|
|
731
833
|
if (storeName) {
|
|
732
834
|
const original = this.original.slice(start, end);
|
|
733
835
|
Object.defineProperty(this.storedNames, original, {
|
|
@@ -736,14 +838,13 @@ class MagicString {
|
|
|
736
838
|
enumerable: true
|
|
737
839
|
});
|
|
738
840
|
}
|
|
739
|
-
const first = this.byStart
|
|
740
|
-
const last = this.byEnd
|
|
841
|
+
const first = this.byStart.get(start);
|
|
842
|
+
const last = this.byEnd.get(end);
|
|
741
843
|
if (first) {
|
|
742
844
|
let chunk = first;
|
|
743
845
|
while (chunk !== last) {
|
|
744
|
-
if (chunk.next !== this.byStart
|
|
846
|
+
if (chunk.next !== this.byStart.get(chunk.end))
|
|
745
847
|
throw new Error("Cannot overwrite across a split point");
|
|
746
|
-
}
|
|
747
848
|
chunk = chunk.next;
|
|
748
849
|
chunk.edit("", false);
|
|
749
850
|
}
|
|
@@ -766,12 +867,11 @@ class MagicString {
|
|
|
766
867
|
if (typeof content !== "string")
|
|
767
868
|
throw new TypeError("inserted content must be a string");
|
|
768
869
|
this._split(index);
|
|
769
|
-
const chunk = this.byEnd
|
|
770
|
-
if (chunk)
|
|
870
|
+
const chunk = this.byEnd.get(index);
|
|
871
|
+
if (chunk)
|
|
771
872
|
chunk.prependLeft(content);
|
|
772
|
-
|
|
873
|
+
else
|
|
773
874
|
this.intro = content + this.intro;
|
|
774
|
-
}
|
|
775
875
|
return this;
|
|
776
876
|
}
|
|
777
877
|
prependRight(index, content) {
|
|
@@ -779,12 +879,11 @@ class MagicString {
|
|
|
779
879
|
if (typeof content !== "string")
|
|
780
880
|
throw new TypeError("inserted content must be a string");
|
|
781
881
|
this._split(index);
|
|
782
|
-
const chunk = this.byStart
|
|
783
|
-
if (chunk)
|
|
882
|
+
const chunk = this.byStart.get(index);
|
|
883
|
+
if (chunk)
|
|
784
884
|
chunk.prependRight(content);
|
|
785
|
-
|
|
885
|
+
else
|
|
786
886
|
this.outro = content + this.outro;
|
|
787
|
-
}
|
|
788
887
|
return this;
|
|
789
888
|
}
|
|
790
889
|
remove(start, end) {
|
|
@@ -804,12 +903,12 @@ class MagicString {
|
|
|
804
903
|
throw new Error("end must be greater than start");
|
|
805
904
|
this._split(start);
|
|
806
905
|
this._split(end);
|
|
807
|
-
let chunk = this.byStart
|
|
906
|
+
let chunk = this.byStart.get(start);
|
|
808
907
|
while (chunk) {
|
|
809
908
|
chunk.intro = "";
|
|
810
909
|
chunk.outro = "";
|
|
811
910
|
chunk.edit("");
|
|
812
|
-
chunk = end > chunk.end ? this.byStart
|
|
911
|
+
chunk = end > chunk.end ? this.byStart.get(chunk.end) : null;
|
|
813
912
|
}
|
|
814
913
|
return this;
|
|
815
914
|
}
|
|
@@ -830,10 +929,10 @@ class MagicString {
|
|
|
830
929
|
throw new Error("end must be greater than start");
|
|
831
930
|
this._split(start);
|
|
832
931
|
this._split(end);
|
|
833
|
-
let chunk = this.byStart
|
|
932
|
+
let chunk = this.byStart.get(start);
|
|
834
933
|
while (chunk) {
|
|
835
934
|
chunk.reset();
|
|
836
|
-
chunk = end > chunk.end ? this.byStart
|
|
935
|
+
chunk = end > chunk.end ? this.byStart.get(chunk.end) : null;
|
|
837
936
|
}
|
|
838
937
|
return this;
|
|
839
938
|
}
|
|
@@ -841,14 +940,15 @@ class MagicString {
|
|
|
841
940
|
if (this.outro.length)
|
|
842
941
|
return this.outro[this.outro.length - 1];
|
|
843
942
|
let chunk = this.lastChunk;
|
|
844
|
-
|
|
943
|
+
while (chunk) {
|
|
845
944
|
if (chunk.outro.length)
|
|
846
945
|
return chunk.outro[chunk.outro.length - 1];
|
|
847
946
|
if (chunk.content.length)
|
|
848
947
|
return chunk.content[chunk.content.length - 1];
|
|
849
948
|
if (chunk.intro.length)
|
|
850
949
|
return chunk.intro[chunk.intro.length - 1];
|
|
851
|
-
|
|
950
|
+
chunk = chunk.previous;
|
|
951
|
+
}
|
|
852
952
|
if (this.intro.length)
|
|
853
953
|
return this.intro[this.intro.length - 1];
|
|
854
954
|
return "";
|
|
@@ -859,7 +959,7 @@ class MagicString {
|
|
|
859
959
|
return this.outro.substr(lineIndex + 1);
|
|
860
960
|
let lineStr = this.outro;
|
|
861
961
|
let chunk = this.lastChunk;
|
|
862
|
-
|
|
962
|
+
while (chunk) {
|
|
863
963
|
if (chunk.outro.length > 0) {
|
|
864
964
|
lineIndex = chunk.outro.lastIndexOf(n);
|
|
865
965
|
if (lineIndex !== -1)
|
|
@@ -878,7 +978,8 @@ class MagicString {
|
|
|
878
978
|
return chunk.intro.substr(lineIndex + 1) + lineStr;
|
|
879
979
|
lineStr = chunk.intro + lineStr;
|
|
880
980
|
}
|
|
881
|
-
|
|
981
|
+
chunk = chunk.previous;
|
|
982
|
+
}
|
|
882
983
|
lineIndex = this.intro.lastIndexOf(n);
|
|
883
984
|
if (lineIndex !== -1)
|
|
884
985
|
return this.intro.substr(lineIndex + 1) + lineStr;
|
|
@@ -896,30 +997,26 @@ class MagicString {
|
|
|
896
997
|
let result = "";
|
|
897
998
|
let chunk = this.firstChunk;
|
|
898
999
|
while (chunk && (chunk.start > start || chunk.end <= start)) {
|
|
899
|
-
if (chunk.start < end && chunk.end >= end)
|
|
1000
|
+
if (chunk.start < end && chunk.end >= end)
|
|
900
1001
|
return result;
|
|
901
|
-
}
|
|
902
1002
|
chunk = chunk.next;
|
|
903
1003
|
}
|
|
904
1004
|
if (chunk && chunk.edited && chunk.start !== start)
|
|
905
1005
|
throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
|
|
906
1006
|
const startChunk = chunk;
|
|
907
1007
|
while (chunk) {
|
|
908
|
-
if (chunk.intro && (startChunk !== chunk || chunk.start === start))
|
|
1008
|
+
if (chunk.intro && (startChunk !== chunk || chunk.start === start))
|
|
909
1009
|
result += chunk.intro;
|
|
910
|
-
}
|
|
911
1010
|
const containsEnd = chunk.start < end && chunk.end >= end;
|
|
912
1011
|
if (containsEnd && chunk.edited && chunk.end !== end)
|
|
913
1012
|
throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
|
|
914
1013
|
const sliceStart = startChunk === chunk ? start - chunk.start : 0;
|
|
915
1014
|
const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
|
|
916
1015
|
result += chunk.content.slice(sliceStart, sliceEnd);
|
|
917
|
-
if (chunk.outro && (!containsEnd || chunk.end === end))
|
|
1016
|
+
if (chunk.outro && (!containsEnd || chunk.end === end))
|
|
918
1017
|
result += chunk.outro;
|
|
919
|
-
|
|
920
|
-
if (containsEnd) {
|
|
1018
|
+
if (containsEnd)
|
|
921
1019
|
break;
|
|
922
|
-
}
|
|
923
1020
|
chunk = chunk.next;
|
|
924
1021
|
}
|
|
925
1022
|
return result;
|
|
@@ -931,7 +1028,7 @@ class MagicString {
|
|
|
931
1028
|
return clone;
|
|
932
1029
|
}
|
|
933
1030
|
_split(index) {
|
|
934
|
-
if (this.byStart
|
|
1031
|
+
if (this.byStart.get(index) || this.byEnd.get(index))
|
|
935
1032
|
return;
|
|
936
1033
|
let chunk = this.lastSearchedChunk;
|
|
937
1034
|
let previousChunk = chunk;
|
|
@@ -939,7 +1036,7 @@ class MagicString {
|
|
|
939
1036
|
while (chunk) {
|
|
940
1037
|
if (chunk.contains(index))
|
|
941
1038
|
return this._splitChunk(chunk, index);
|
|
942
|
-
chunk = searchForward ? this.byStart
|
|
1039
|
+
chunk = searchForward ? this.byStart.get(chunk.end) : this.byEnd.get(chunk.start);
|
|
943
1040
|
if (chunk === previousChunk)
|
|
944
1041
|
return;
|
|
945
1042
|
previousChunk = chunk;
|
|
@@ -951,9 +1048,9 @@ class MagicString {
|
|
|
951
1048
|
throw new Error(`Cannot split a chunk that has already been edited (${loc.line}:${loc.column} \u2013 "${chunk.original}")`);
|
|
952
1049
|
}
|
|
953
1050
|
const newChunk = chunk.split(index);
|
|
954
|
-
this.byEnd
|
|
955
|
-
this.byStart
|
|
956
|
-
this.byEnd
|
|
1051
|
+
this.byEnd.set(index, chunk);
|
|
1052
|
+
this.byStart.set(index, newChunk);
|
|
1053
|
+
this.byEnd.set(newChunk.end, newChunk);
|
|
957
1054
|
if (chunk === this.lastChunk)
|
|
958
1055
|
this.lastChunk = newChunk;
|
|
959
1056
|
this.lastSearchedChunk = chunk;
|
|
@@ -970,18 +1067,20 @@ class MagicString {
|
|
|
970
1067
|
}
|
|
971
1068
|
isEmpty() {
|
|
972
1069
|
let chunk = this.firstChunk;
|
|
973
|
-
|
|
1070
|
+
while (chunk) {
|
|
974
1071
|
if (chunk.intro.length && chunk.intro.trim() || chunk.content.length && chunk.content.trim() || chunk.outro.length && chunk.outro.trim())
|
|
975
1072
|
return false;
|
|
976
|
-
|
|
1073
|
+
chunk = chunk.next;
|
|
1074
|
+
}
|
|
977
1075
|
return true;
|
|
978
1076
|
}
|
|
979
1077
|
length() {
|
|
980
1078
|
let chunk = this.firstChunk;
|
|
981
1079
|
let length = 0;
|
|
982
|
-
|
|
1080
|
+
while (chunk) {
|
|
983
1081
|
length += chunk.intro.length + chunk.content.length + chunk.outro.length;
|
|
984
|
-
|
|
1082
|
+
chunk = chunk.next;
|
|
1083
|
+
}
|
|
985
1084
|
return length;
|
|
986
1085
|
}
|
|
987
1086
|
trimLines() {
|
|
@@ -991,7 +1090,7 @@ class MagicString {
|
|
|
991
1090
|
return this.trimStart(charType).trimEnd(charType);
|
|
992
1091
|
}
|
|
993
1092
|
trimEndAborted(charType) {
|
|
994
|
-
const rx = new RegExp(
|
|
1093
|
+
const rx = new RegExp(`${charType || "\\s"}+$`);
|
|
995
1094
|
this.outro = this.outro.replace(rx, "");
|
|
996
1095
|
if (this.outro.length)
|
|
997
1096
|
return true;
|
|
@@ -1000,12 +1099,11 @@ class MagicString {
|
|
|
1000
1099
|
const end = chunk.end;
|
|
1001
1100
|
const aborted = chunk.trimEnd(rx);
|
|
1002
1101
|
if (chunk.end !== end) {
|
|
1003
|
-
if (this.lastChunk === chunk)
|
|
1102
|
+
if (this.lastChunk === chunk)
|
|
1004
1103
|
this.lastChunk = chunk.next;
|
|
1005
|
-
|
|
1006
|
-
this.
|
|
1007
|
-
this.
|
|
1008
|
-
this.byEnd[chunk.next.end] = chunk.next;
|
|
1104
|
+
this.byEnd.set(chunk.end, chunk);
|
|
1105
|
+
this.byStart.set(chunk.next.start, chunk.next);
|
|
1106
|
+
this.byEnd.set(chunk.next.end, chunk.next);
|
|
1009
1107
|
}
|
|
1010
1108
|
if (aborted)
|
|
1011
1109
|
return true;
|
|
@@ -1018,7 +1116,7 @@ class MagicString {
|
|
|
1018
1116
|
return this;
|
|
1019
1117
|
}
|
|
1020
1118
|
trimStartAborted(charType) {
|
|
1021
|
-
const rx = new RegExp(
|
|
1119
|
+
const rx = new RegExp(`^${charType || "\\s"}+`);
|
|
1022
1120
|
this.intro = this.intro.replace(rx, "");
|
|
1023
1121
|
if (this.intro.length)
|
|
1024
1122
|
return true;
|
|
@@ -1029,9 +1127,9 @@ class MagicString {
|
|
|
1029
1127
|
if (chunk.end !== end) {
|
|
1030
1128
|
if (chunk === this.lastChunk)
|
|
1031
1129
|
this.lastChunk = chunk.next;
|
|
1032
|
-
this.byEnd
|
|
1033
|
-
this.byStart
|
|
1034
|
-
this.byEnd
|
|
1130
|
+
this.byEnd.set(chunk.end, chunk);
|
|
1131
|
+
this.byStart.set(chunk.next.start, chunk.next);
|
|
1132
|
+
this.byEnd.set(chunk.next.end, chunk.next);
|
|
1035
1133
|
}
|
|
1036
1134
|
if (aborted)
|
|
1037
1135
|
return true;
|
|
@@ -1048,46 +1146,43 @@ class MagicString {
|
|
|
1048
1146
|
}
|
|
1049
1147
|
_replaceRegexp(searchValue, replacement) {
|
|
1050
1148
|
function getReplacement(match, str) {
|
|
1051
|
-
if (typeof replacement === "string")
|
|
1149
|
+
if (typeof replacement === "string")
|
|
1052
1150
|
return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
|
|
1053
1151
|
if (i === "$")
|
|
1054
1152
|
return "$";
|
|
1055
1153
|
if (i === "&")
|
|
1056
1154
|
return match[0];
|
|
1057
|
-
|
|
1058
|
-
if (num < match.length)
|
|
1155
|
+
if (+i < match.length)
|
|
1059
1156
|
return match[+i];
|
|
1060
1157
|
return `$${i}`;
|
|
1061
1158
|
});
|
|
1062
|
-
|
|
1063
|
-
return replacement(...match, match.index, str, match.groups);
|
|
1064
|
-
}
|
|
1159
|
+
else
|
|
1160
|
+
return replacement(match[0], ...match.slice(1), match.index, str, match.groups);
|
|
1065
1161
|
}
|
|
1066
1162
|
function matchAll(re, str) {
|
|
1067
|
-
let match;
|
|
1068
1163
|
const matches = [];
|
|
1069
|
-
while (
|
|
1164
|
+
while (true) {
|
|
1165
|
+
const match = re.exec(str);
|
|
1166
|
+
if (!match)
|
|
1167
|
+
break;
|
|
1070
1168
|
matches.push(match);
|
|
1071
1169
|
}
|
|
1072
1170
|
return matches;
|
|
1073
1171
|
}
|
|
1074
|
-
if (searchValue.global)
|
|
1075
|
-
|
|
1076
|
-
matches.forEach((match) => {
|
|
1172
|
+
if (searchValue.global)
|
|
1173
|
+
matchAll(searchValue, this.original).forEach((match) => {
|
|
1077
1174
|
if (match.index != null) {
|
|
1078
1175
|
const replacement2 = getReplacement(match, this.original);
|
|
1079
|
-
if (replacement2 !== match[0])
|
|
1176
|
+
if (replacement2 !== match[0])
|
|
1080
1177
|
this.overwrite(match.index, match.index + match[0].length, replacement2);
|
|
1081
|
-
}
|
|
1082
1178
|
}
|
|
1083
1179
|
});
|
|
1084
|
-
|
|
1180
|
+
else {
|
|
1085
1181
|
const match = this.original.match(searchValue);
|
|
1086
1182
|
if (match && match.index != null) {
|
|
1087
1183
|
const replacement2 = getReplacement(match, this.original);
|
|
1088
|
-
if (replacement2 !== match[0])
|
|
1184
|
+
if (replacement2 !== match[0])
|
|
1089
1185
|
this.overwrite(match.index, match.index + match[0].length, replacement2);
|
|
1090
|
-
}
|
|
1091
1186
|
}
|
|
1092
1187
|
}
|
|
1093
1188
|
return this;
|
|
@@ -1096,19 +1191,16 @@ class MagicString {
|
|
|
1096
1191
|
const { original } = this;
|
|
1097
1192
|
const index = original.indexOf(string);
|
|
1098
1193
|
if (index !== -1) {
|
|
1099
|
-
if (typeof replacement === "function")
|
|
1194
|
+
if (typeof replacement === "function")
|
|
1100
1195
|
replacement = replacement(string, index, original);
|
|
1101
|
-
|
|
1102
|
-
if (string !== replacement) {
|
|
1196
|
+
if (string !== replacement)
|
|
1103
1197
|
this.overwrite(index, index + string.length, replacement);
|
|
1104
|
-
}
|
|
1105
1198
|
}
|
|
1106
1199
|
return this;
|
|
1107
1200
|
}
|
|
1108
1201
|
replace(searchValue, replacement) {
|
|
1109
|
-
if (typeof searchValue === "string")
|
|
1202
|
+
if (typeof searchValue === "string")
|
|
1110
1203
|
return this._replaceString(searchValue, replacement);
|
|
1111
|
-
}
|
|
1112
1204
|
return this._replaceRegexp(searchValue, replacement);
|
|
1113
1205
|
}
|
|
1114
1206
|
_replaceAllString(string, replacement) {
|
|
@@ -1116,62 +1208,105 @@ class MagicString {
|
|
|
1116
1208
|
const stringLength = string.length;
|
|
1117
1209
|
for (let index = original.indexOf(string);index !== -1; index = original.indexOf(string, index + stringLength)) {
|
|
1118
1210
|
const previous = original.slice(index, index + stringLength);
|
|
1119
|
-
|
|
1120
|
-
if (typeof replacement === "function") {
|
|
1121
|
-
_replacement = replacement(previous, index, original);
|
|
1122
|
-
}
|
|
1211
|
+
const _replacement = typeof replacement === "function" ? replacement(previous, index, original) : replacement;
|
|
1123
1212
|
if (previous !== _replacement)
|
|
1124
1213
|
this.overwrite(index, index + stringLength, _replacement);
|
|
1125
1214
|
}
|
|
1126
1215
|
return this;
|
|
1127
1216
|
}
|
|
1128
1217
|
replaceAll(searchValue, replacement) {
|
|
1129
|
-
if (typeof searchValue === "string")
|
|
1218
|
+
if (typeof searchValue === "string")
|
|
1130
1219
|
return this._replaceAllString(searchValue, replacement);
|
|
1131
|
-
|
|
1132
|
-
if (!searchValue.global) {
|
|
1220
|
+
if (!searchValue.global)
|
|
1133
1221
|
throw new TypeError("MagicString.prototype.replaceAll called with a non-global RegExp argument");
|
|
1134
|
-
}
|
|
1135
1222
|
return this._replaceRegexp(searchValue, replacement);
|
|
1136
1223
|
}
|
|
1137
|
-
}
|
|
1224
|
+
};
|
|
1138
1225
|
|
|
1139
1226
|
// src/plugin/transform-client.ts
|
|
1140
|
-
import {
|
|
1141
|
-
parse
|
|
1142
|
-
} from "yuku-parser";
|
|
1227
|
+
import { walk as walk2 } from "yuku-parser";
|
|
1143
1228
|
|
|
1144
|
-
// src/
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1229
|
+
// src/shared/parser.ts
|
|
1230
|
+
import { parse } from "yuku-parser";
|
|
1231
|
+
function parseSource(code, lang) {
|
|
1232
|
+
return parse(code, { lang, sourceType: "module" });
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
// src/plugin/binding-scope.ts
|
|
1236
|
+
function bindingPatternHasName(pattern, name) {
|
|
1237
|
+
if (!(pattern && typeof pattern === "object")) {
|
|
1238
|
+
return false;
|
|
1239
|
+
}
|
|
1240
|
+
const node = pattern;
|
|
1241
|
+
if (node.type === "Identifier") {
|
|
1242
|
+
return node.name === name;
|
|
1243
|
+
}
|
|
1244
|
+
if (node.type === "AssignmentPattern") {
|
|
1245
|
+
return bindingPatternHasName(node.left, name);
|
|
1246
|
+
}
|
|
1247
|
+
if (node.type === "RestElement") {
|
|
1248
|
+
return bindingPatternHasName(node.argument, name);
|
|
1249
|
+
}
|
|
1250
|
+
if (node.type === "ArrayPattern") {
|
|
1251
|
+
return Array.isArray(node.elements) && node.elements.some((element) => bindingPatternHasName(element, name));
|
|
1148
1252
|
}
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1253
|
+
if (node.type === "ObjectPattern") {
|
|
1254
|
+
return Array.isArray(node.properties) && node.properties.some((property) => {
|
|
1255
|
+
if (!(property && typeof property === "object")) {
|
|
1256
|
+
return false;
|
|
1257
|
+
}
|
|
1258
|
+
const propertyNode = property;
|
|
1259
|
+
return bindingPatternHasName(propertyNode.type === "Property" ? propertyNode.value : propertyNode.argument, name);
|
|
1260
|
+
});
|
|
1261
|
+
}
|
|
1262
|
+
if (node.type === "TSParameterProperty") {
|
|
1263
|
+
return bindingPatternHasName(node.parameter, name);
|
|
1159
1264
|
}
|
|
1265
|
+
return false;
|
|
1160
1266
|
}
|
|
1161
|
-
|
|
1162
|
-
"
|
|
1163
|
-
|
|
1164
|
-
"TSTypeAssertion",
|
|
1165
|
-
"TSNonNullExpression",
|
|
1166
|
-
"TSInstantiationExpression",
|
|
1167
|
-
"ParenthesizedExpression"
|
|
1168
|
-
]);
|
|
1169
|
-
function unwrapTSExpression(node) {
|
|
1170
|
-
let current = node;
|
|
1171
|
-
while (current && typeof current === "object" && TS_WRAPPER_TYPES.has(current.type) && current.expression && typeof current.expression === "object") {
|
|
1172
|
-
current = current.expression;
|
|
1267
|
+
function declarationHasName(node, name) {
|
|
1268
|
+
if (node.type === "VariableDeclaration" && Array.isArray(node.declarations)) {
|
|
1269
|
+
return node.declarations.some((declaration) => declaration && typeof declaration === "object" ? bindingPatternHasName(declaration.id, name) : false);
|
|
1173
1270
|
}
|
|
1174
|
-
|
|
1271
|
+
if (node.type === "FunctionDeclaration" || node.type === "ClassDeclaration") {
|
|
1272
|
+
return bindingPatternHasName(node.id, name);
|
|
1273
|
+
}
|
|
1274
|
+
return false;
|
|
1275
|
+
}
|
|
1276
|
+
function functionBodyHasVarName(value, name, root) {
|
|
1277
|
+
if (!value || typeof value !== "object") {
|
|
1278
|
+
return false;
|
|
1279
|
+
}
|
|
1280
|
+
if (Array.isArray(value)) {
|
|
1281
|
+
return value.some((entry) => functionBodyHasVarName(entry, name, false));
|
|
1282
|
+
}
|
|
1283
|
+
const node = value;
|
|
1284
|
+
if (!root && (node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression" || node.type === "StaticBlock")) {
|
|
1285
|
+
return false;
|
|
1286
|
+
}
|
|
1287
|
+
if (node.type === "VariableDeclaration" && node.kind === "var" && Array.isArray(node.declarations) && node.declarations.some((declaration) => declaration && typeof declaration === "object" ? bindingPatternHasName(declaration.id, name) : false)) {
|
|
1288
|
+
return true;
|
|
1289
|
+
}
|
|
1290
|
+
return Object.values(node).some((entry) => functionBodyHasVarName(entry, name, false));
|
|
1291
|
+
}
|
|
1292
|
+
function functionScopeHasName(scope, name) {
|
|
1293
|
+
if (scope.type !== "FunctionDeclaration" && scope.type !== "FunctionExpression" && scope.type !== "ArrowFunctionExpression") {
|
|
1294
|
+
return false;
|
|
1295
|
+
}
|
|
1296
|
+
return Array.isArray(scope.params) && scope.params.some((parameter) => bindingPatternHasName(parameter, name)) || scope.type === "FunctionExpression" && bindingPatternHasName(scope.id, name) || functionBodyHasVarName(scope.body, name, true);
|
|
1297
|
+
}
|
|
1298
|
+
function blockScopeHasName(scope, name) {
|
|
1299
|
+
return scope.type === "BlockStatement" && Array.isArray(scope.body) && scope.body.some((statement) => statement && typeof statement === "object" ? declarationHasName(statement, name) : false);
|
|
1300
|
+
}
|
|
1301
|
+
function loopScopeHasName(scope, name) {
|
|
1302
|
+
const declaration = scope.type === "ForStatement" ? scope.init : scope.left;
|
|
1303
|
+
return (scope.type === "ForStatement" || scope.type === "ForInStatement" || scope.type === "ForOfStatement") && !!declaration && typeof declaration === "object" && declarationHasName(declaration, name);
|
|
1304
|
+
}
|
|
1305
|
+
function switchScopeHasName(scope, name) {
|
|
1306
|
+
return scope.type === "SwitchStatement" && Array.isArray(scope.cases) && scope.cases.some((switchCase) => switchCase && typeof switchCase === "object" && Array.isArray(switchCase.consequent) && switchCase.consequent.some((statement) => statement && typeof statement === "object" && declarationHasName(statement, name)));
|
|
1307
|
+
}
|
|
1308
|
+
function hasShadowingDeclaration(name, ancestors) {
|
|
1309
|
+
return ancestors.some((scope) => functionScopeHasName(scope, name) || scope.type === "CatchClause" && bindingPatternHasName(scope.param, name) || blockScopeHasName(scope, name) || loopScopeHasName(scope, name) || switchScopeHasName(scope, name));
|
|
1175
1310
|
}
|
|
1176
1311
|
|
|
1177
1312
|
// src/shared/utils/ast-walk.ts
|
|
@@ -1206,72 +1341,7 @@ function walkAST(node, visitor) {
|
|
|
1206
1341
|
}
|
|
1207
1342
|
}
|
|
1208
1343
|
|
|
1209
|
-
// src/plugin/
|
|
1210
|
-
var SERVER_ONLY_PROPERTIES = new Set(["loader", "query", "params", "staticParams"]);
|
|
1211
|
-
function isCreateRouteCall(node) {
|
|
1212
|
-
return node.callee.type === "Identifier" && node.callee.name === "createRoute";
|
|
1213
|
-
}
|
|
1214
|
-
function isRoutePageCall(node) {
|
|
1215
|
-
const { callee } = node;
|
|
1216
|
-
if (callee.type === "Identifier" && callee.name === "page") {
|
|
1217
|
-
return true;
|
|
1218
|
-
}
|
|
1219
|
-
if (callee.type === "MemberExpression" && callee.property.type === "Identifier" && callee.property.name === "page") {
|
|
1220
|
-
return true;
|
|
1221
|
-
}
|
|
1222
|
-
return false;
|
|
1223
|
-
}
|
|
1224
|
-
function isTargetCall(node) {
|
|
1225
|
-
return isCreateRouteCall(node) || isRoutePageCall(node);
|
|
1226
|
-
}
|
|
1227
|
-
function isObjectExpressionNode(node) {
|
|
1228
|
-
return node.type === "ObjectExpression";
|
|
1229
|
-
}
|
|
1230
|
-
function removeServerProperties(s, source, obj) {
|
|
1231
|
-
const toRemove = obj.properties.filter((p) => {
|
|
1232
|
-
if (p.type !== "Property") {
|
|
1233
|
-
return false;
|
|
1234
|
-
}
|
|
1235
|
-
const { key } = p;
|
|
1236
|
-
if (p.computed) {
|
|
1237
|
-
return false;
|
|
1238
|
-
}
|
|
1239
|
-
if (key.type === "Identifier" && typeof key.name === "string") {
|
|
1240
|
-
return SERVER_ONLY_PROPERTIES.has(key.name);
|
|
1241
|
-
}
|
|
1242
|
-
if (key.type === "Literal" && typeof key.value === "string") {
|
|
1243
|
-
return SERVER_ONLY_PROPERTIES.has(key.value);
|
|
1244
|
-
}
|
|
1245
|
-
return false;
|
|
1246
|
-
});
|
|
1247
|
-
if (toRemove.length === 0) {
|
|
1248
|
-
return false;
|
|
1249
|
-
}
|
|
1250
|
-
for (const prop of toRemove) {
|
|
1251
|
-
let removeEnd = prop.end;
|
|
1252
|
-
while (removeEnd < source.length && (source[removeEnd] === "," || source[removeEnd] === " " || source[removeEnd] === `
|
|
1253
|
-
` || source[removeEnd] === "\r" || source[removeEnd] === "\t")) {
|
|
1254
|
-
if (source[removeEnd] === ",") {
|
|
1255
|
-
removeEnd++;
|
|
1256
|
-
break;
|
|
1257
|
-
}
|
|
1258
|
-
removeEnd++;
|
|
1259
|
-
}
|
|
1260
|
-
let removeStart = prop.start;
|
|
1261
|
-
while (removeStart > 0 && (source[removeStart - 1] === " " || source[removeStart - 1] === "\t")) {
|
|
1262
|
-
removeStart--;
|
|
1263
|
-
}
|
|
1264
|
-
if (removeStart > 0 && source[removeStart - 1] === `
|
|
1265
|
-
`) {
|
|
1266
|
-
removeStart--;
|
|
1267
|
-
if (removeStart > 0 && source[removeStart - 1] === "\r") {
|
|
1268
|
-
removeStart--;
|
|
1269
|
-
}
|
|
1270
|
-
}
|
|
1271
|
-
s.remove(removeStart, removeEnd);
|
|
1272
|
-
}
|
|
1273
|
-
return true;
|
|
1274
|
-
}
|
|
1344
|
+
// src/plugin/dead-code-elimination.ts
|
|
1275
1345
|
var TYPE_SCOPE_NODES = new Set([
|
|
1276
1346
|
"TSTypeAnnotation",
|
|
1277
1347
|
"TSTypeParameterDeclaration",
|
|
@@ -1279,7 +1349,8 @@ var TYPE_SCOPE_NODES = new Set([
|
|
|
1279
1349
|
"TSInterfaceDeclaration",
|
|
1280
1350
|
"TSTypeAliasDeclaration",
|
|
1281
1351
|
"TSTypePredicate",
|
|
1282
|
-
"TSClassImplements"
|
|
1352
|
+
"TSClassImplements",
|
|
1353
|
+
"TSExpressionWithTypeArguments"
|
|
1283
1354
|
]);
|
|
1284
1355
|
function markTypeDescendants(node, excluded) {
|
|
1285
1356
|
if (!node || typeof node !== "object") {
|
|
@@ -1291,15 +1362,15 @@ function markTypeDescendants(node, excluded) {
|
|
|
1291
1362
|
}
|
|
1292
1363
|
return;
|
|
1293
1364
|
}
|
|
1294
|
-
const
|
|
1295
|
-
if (
|
|
1296
|
-
excluded.add(
|
|
1365
|
+
const astNode = node;
|
|
1366
|
+
if (astNode.type === "Identifier" || astNode.type === "JSXIdentifier") {
|
|
1367
|
+
excluded.add(astNode);
|
|
1297
1368
|
}
|
|
1298
|
-
for (const key of Object.keys(
|
|
1369
|
+
for (const key of Object.keys(astNode)) {
|
|
1299
1370
|
if (key === "type" || key === "start" || key === "end") {
|
|
1300
1371
|
continue;
|
|
1301
1372
|
}
|
|
1302
|
-
markTypeDescendants(
|
|
1373
|
+
markTypeDescendants(astNode[key], excluded);
|
|
1303
1374
|
}
|
|
1304
1375
|
}
|
|
1305
1376
|
function excludeTypePositionIdentifiers(node, excluded) {
|
|
@@ -1314,11 +1385,11 @@ function excludeTypePositionIdentifiers(node, excluded) {
|
|
|
1314
1385
|
function collectReferencedNames(program) {
|
|
1315
1386
|
const refs = new Set;
|
|
1316
1387
|
const excluded = new Set;
|
|
1317
|
-
for (const
|
|
1318
|
-
if (
|
|
1388
|
+
for (const statement of program.body) {
|
|
1389
|
+
if (statement.type === "ImportDeclaration") {
|
|
1319
1390
|
continue;
|
|
1320
1391
|
}
|
|
1321
|
-
walkAST(
|
|
1392
|
+
walkAST(statement, (node) => {
|
|
1322
1393
|
if (node.type === "Property" && !node.computed) {
|
|
1323
1394
|
excluded.add(node.key);
|
|
1324
1395
|
}
|
|
@@ -1334,11 +1405,11 @@ function collectReferencedNames(program) {
|
|
|
1334
1405
|
excludeTypePositionIdentifiers(node, excluded);
|
|
1335
1406
|
});
|
|
1336
1407
|
}
|
|
1337
|
-
for (const
|
|
1338
|
-
if (
|
|
1408
|
+
for (const statement of program.body) {
|
|
1409
|
+
if (statement.type === "ImportDeclaration") {
|
|
1339
1410
|
continue;
|
|
1340
1411
|
}
|
|
1341
|
-
walkAST(
|
|
1412
|
+
walkAST(statement, (node) => {
|
|
1342
1413
|
if (excluded.has(node)) {
|
|
1343
1414
|
return;
|
|
1344
1415
|
}
|
|
@@ -1349,82 +1420,523 @@ function collectReferencedNames(program) {
|
|
|
1349
1420
|
}
|
|
1350
1421
|
return refs;
|
|
1351
1422
|
}
|
|
1352
|
-
function
|
|
1353
|
-
|
|
1423
|
+
function preserveImportSideEffect(transformed, code, declaration) {
|
|
1424
|
+
transformed.overwrite(declaration.start, declaration.end, `import ${code.slice(declaration.source.start, declaration.source.end)};`);
|
|
1425
|
+
}
|
|
1426
|
+
function removeEntireImport(transformed, code, declaration) {
|
|
1427
|
+
let removeEnd = declaration.end;
|
|
1354
1428
|
while (removeEnd < code.length && (code[removeEnd] === `
|
|
1355
1429
|
` || code[removeEnd] === "\r")) {
|
|
1356
|
-
removeEnd
|
|
1430
|
+
removeEnd += 1;
|
|
1357
1431
|
}
|
|
1358
|
-
|
|
1432
|
+
transformed.remove(declaration.start, removeEnd);
|
|
1359
1433
|
}
|
|
1360
|
-
function removeUnusedSpecifiers(
|
|
1361
|
-
const
|
|
1362
|
-
for (const
|
|
1363
|
-
let removeStart =
|
|
1364
|
-
let removeEnd =
|
|
1434
|
+
function removeUnusedSpecifiers(transformed, code, declaration, refs) {
|
|
1435
|
+
const removedSpecifiers = declaration.specifiers.filter((specifier) => !refs.has(specifier.local.name));
|
|
1436
|
+
for (const specifier of removedSpecifiers) {
|
|
1437
|
+
let removeStart = specifier.start;
|
|
1438
|
+
let removeEnd = specifier.end;
|
|
1365
1439
|
while (removeEnd < code.length && (code[removeEnd] === "," || code[removeEnd] === " ")) {
|
|
1366
|
-
removeEnd
|
|
1440
|
+
removeEnd += 1;
|
|
1367
1441
|
}
|
|
1368
|
-
if (!code.slice(
|
|
1442
|
+
if (!code.slice(specifier.end, removeEnd).includes(",")) {
|
|
1369
1443
|
while (removeStart > 0 && (code[removeStart - 1] === " " || code[removeStart - 1] === ",")) {
|
|
1370
|
-
removeStart
|
|
1444
|
+
removeStart -= 1;
|
|
1371
1445
|
}
|
|
1372
1446
|
}
|
|
1373
|
-
|
|
1447
|
+
transformed.remove(removeStart, removeEnd);
|
|
1374
1448
|
}
|
|
1375
1449
|
}
|
|
1376
|
-
function deadCodeElimination(
|
|
1377
|
-
const code =
|
|
1378
|
-
const { program, diagnostics } =
|
|
1379
|
-
const firstError = diagnostics.find((
|
|
1450
|
+
function deadCodeElimination(transformed, originalCode, lang) {
|
|
1451
|
+
const code = transformed.toString();
|
|
1452
|
+
const { program, diagnostics } = parseSource(code, lang);
|
|
1453
|
+
const firstError = diagnostics.find((diagnostic) => diagnostic.severity === "error");
|
|
1380
1454
|
if (firstError) {
|
|
1381
1455
|
console.error("[furin] DCE: failed to parse transformed output:", firstError.message);
|
|
1382
|
-
return
|
|
1456
|
+
return transformed;
|
|
1383
1457
|
}
|
|
1384
|
-
const
|
|
1458
|
+
const pruned = new MagicString(code);
|
|
1385
1459
|
const refs = collectReferencedNames(program);
|
|
1386
|
-
const
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1460
|
+
const originalParse = parseSource(originalCode, lang);
|
|
1461
|
+
const originalRefs = originalParse.diagnostics.some((diagnostic) => diagnostic.severity === "error") ? refs : collectReferencedNames(originalParse.program);
|
|
1462
|
+
for (let index = program.body.length - 1;index >= 0; index -= 1) {
|
|
1463
|
+
const statement = program.body[index];
|
|
1464
|
+
if (statement?.type !== "ImportDeclaration") {
|
|
1465
|
+
continue;
|
|
1466
|
+
}
|
|
1467
|
+
const declaration = statement;
|
|
1468
|
+
if (declaration.specifiers.length === 0) {
|
|
1390
1469
|
continue;
|
|
1391
1470
|
}
|
|
1471
|
+
const usedCount = declaration.specifiers.filter((specifier) => refs.has(specifier.local.name)).length;
|
|
1472
|
+
if (usedCount === 0) {
|
|
1473
|
+
const typeOnly = declaration.importKind === "type" || declaration.specifiers.every((specifier) => specifier.importKind === "type");
|
|
1474
|
+
const wasUsedBeforeTransform = declaration.specifiers.some((specifier) => originalRefs.has(specifier.local.name));
|
|
1475
|
+
if (typeOnly || wasUsedBeforeTransform) {
|
|
1476
|
+
removeEntireImport(pruned, code, declaration);
|
|
1477
|
+
} else {
|
|
1478
|
+
preserveImportSideEffect(pruned, code, declaration);
|
|
1479
|
+
}
|
|
1480
|
+
} else if (usedCount < declaration.specifiers.length) {
|
|
1481
|
+
removeUnusedSpecifiers(pruned, code, declaration, refs);
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
return pruned;
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
// src/plugin/transform-isomorphic.ts
|
|
1488
|
+
import { walk } from "yuku-parser";
|
|
1489
|
+
var FURIN_MODULES = new Set(["@teyik0/furin", "furin"]);
|
|
1490
|
+
function importedName(specifier) {
|
|
1491
|
+
const { imported } = specifier;
|
|
1492
|
+
if (!(imported && typeof imported === "object")) {
|
|
1493
|
+
return;
|
|
1494
|
+
}
|
|
1495
|
+
const node = imported;
|
|
1496
|
+
if (node.type === "Identifier" && typeof node.name === "string") {
|
|
1497
|
+
return node.name;
|
|
1498
|
+
}
|
|
1499
|
+
if (node.type === "Literal" && typeof node.value === "string") {
|
|
1500
|
+
return node.value;
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
function localName(specifier) {
|
|
1504
|
+
const local = specifier.local;
|
|
1505
|
+
return local?.type === "Identifier" && typeof local.name === "string" ? local.name : undefined;
|
|
1506
|
+
}
|
|
1507
|
+
function addImportSpecifier(specifier, bindings) {
|
|
1508
|
+
const local = localName(specifier);
|
|
1509
|
+
if (!local) {
|
|
1510
|
+
return;
|
|
1511
|
+
}
|
|
1512
|
+
if (specifier.type === "ImportNamespaceSpecifier") {
|
|
1513
|
+
bindings.namespaces.add(local);
|
|
1514
|
+
return;
|
|
1515
|
+
}
|
|
1516
|
+
if (specifier.type === "ImportSpecifier" && specifier.importKind !== "type" && importedName(specifier) === "createIsomorphicFn") {
|
|
1517
|
+
bindings.named.add(local);
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
function collectBindings(program) {
|
|
1521
|
+
const bindings = {
|
|
1522
|
+
named: new Set,
|
|
1523
|
+
namespaces: new Set
|
|
1524
|
+
};
|
|
1525
|
+
for (const statement of program.body) {
|
|
1526
|
+
if (statement.type !== "ImportDeclaration") {
|
|
1527
|
+
continue;
|
|
1528
|
+
}
|
|
1529
|
+
const declaration = statement;
|
|
1530
|
+
if (declaration.importKind === "type" || !FURIN_MODULES.has(String(declaration.source.value))) {
|
|
1531
|
+
continue;
|
|
1532
|
+
}
|
|
1533
|
+
for (const specifier of declaration.specifiers) {
|
|
1534
|
+
addImportSpecifier(specifier, bindings);
|
|
1535
|
+
}
|
|
1536
|
+
}
|
|
1537
|
+
return bindings;
|
|
1538
|
+
}
|
|
1539
|
+
function isCreateIsomorphicCall(node, bindings, ancestors) {
|
|
1540
|
+
if (node.type !== "CallExpression") {
|
|
1541
|
+
return false;
|
|
1542
|
+
}
|
|
1543
|
+
const call = node;
|
|
1544
|
+
if (call.callee.type === "Identifier" && typeof call.callee.name === "string" && bindings.named.has(call.callee.name) && !hasShadowingDeclaration(call.callee.name, ancestors)) {
|
|
1545
|
+
return true;
|
|
1546
|
+
}
|
|
1547
|
+
const { callee } = call;
|
|
1548
|
+
return callee.type === "MemberExpression" && !callee.computed && callee.object.type === "Identifier" && typeof callee.object.name === "string" && bindings.namespaces.has(callee.object.name) && !hasShadowingDeclaration(callee.object.name, ancestors) && callee.property.type === "Identifier" && callee.property.name === "createIsomorphicFn";
|
|
1549
|
+
}
|
|
1550
|
+
function environmentMethod(node) {
|
|
1551
|
+
if (node.type !== "CallExpression") {
|
|
1552
|
+
return;
|
|
1553
|
+
}
|
|
1554
|
+
const call = node;
|
|
1555
|
+
const { callee } = call;
|
|
1556
|
+
if (callee.type !== "MemberExpression" || callee.computed || callee.property.type !== "Identifier") {
|
|
1557
|
+
return;
|
|
1558
|
+
}
|
|
1559
|
+
return callee.property.name === "client" || callee.property.name === "server" ? callee.property.name : undefined;
|
|
1560
|
+
}
|
|
1561
|
+
function parseCandidate(source, filename, node, bindings, ancestors) {
|
|
1562
|
+
let current = node;
|
|
1563
|
+
let client;
|
|
1564
|
+
let server;
|
|
1565
|
+
for (;; ) {
|
|
1566
|
+
const method = environmentMethod(current);
|
|
1567
|
+
if (!method) {
|
|
1568
|
+
break;
|
|
1569
|
+
}
|
|
1570
|
+
const call = current;
|
|
1571
|
+
if (call.callee.type !== "MemberExpression") {
|
|
1572
|
+
return null;
|
|
1573
|
+
}
|
|
1574
|
+
const [implementation] = call.arguments;
|
|
1575
|
+
if (!implementation) {
|
|
1576
|
+
return null;
|
|
1577
|
+
}
|
|
1578
|
+
const unwrappedImplementation = unwrapTSExpression(implementation);
|
|
1579
|
+
if (unwrappedImplementation.type !== "ArrowFunctionExpression" && unwrappedImplementation.type !== "FunctionExpression" && unwrappedImplementation.type !== "Identifier") {
|
|
1580
|
+
throw new Error(`[furin] ${filename}:${sourcePosition(source, implementation.start)} createIsomorphicFn().${method}() must receive a function.`);
|
|
1581
|
+
}
|
|
1582
|
+
if (method === "client") {
|
|
1583
|
+
client = implementation;
|
|
1584
|
+
} else {
|
|
1585
|
+
server = implementation;
|
|
1586
|
+
}
|
|
1587
|
+
current = unwrapTSExpression(call.callee.object);
|
|
1588
|
+
}
|
|
1589
|
+
if (!isCreateIsomorphicCall(current, bindings, ancestors)) {
|
|
1590
|
+
return null;
|
|
1591
|
+
}
|
|
1592
|
+
return {
|
|
1593
|
+
client,
|
|
1594
|
+
end: node.end,
|
|
1595
|
+
server,
|
|
1596
|
+
start: node.start
|
|
1597
|
+
};
|
|
1598
|
+
}
|
|
1599
|
+
function collectCandidates(source, filename, program, bindings) {
|
|
1600
|
+
const candidates = [];
|
|
1601
|
+
walk(program, {
|
|
1602
|
+
CallExpression(node, context) {
|
|
1603
|
+
const candidate = parseCandidate(source, filename, node, bindings, context.ancestors());
|
|
1604
|
+
if (candidate) {
|
|
1605
|
+
candidates.push(candidate);
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
});
|
|
1609
|
+
return candidates.toSorted((left, right) => right.end - right.start - (left.end - left.start)).filter((candidate, index, all) => !all.slice(0, index).some((other) => other.start <= candidate.start && other.end >= candidate.end));
|
|
1610
|
+
}
|
|
1611
|
+
function sourcePosition(source, offset) {
|
|
1612
|
+
const before = source.slice(0, offset);
|
|
1613
|
+
const line = before.split(`
|
|
1614
|
+
`).length;
|
|
1615
|
+
const lastNewline = before.lastIndexOf(`
|
|
1616
|
+
`);
|
|
1617
|
+
return `${line}:${offset - lastNewline}`;
|
|
1618
|
+
}
|
|
1619
|
+
function lexicalBindingScope(ancestors) {
|
|
1620
|
+
return ancestors.findLast((ancestor) => ancestor.type === "Program" || ancestor.type === "BlockStatement" || ancestor.type === "SwitchStatement" || ancestor.type === "ForStatement" || ancestor.type === "ForInStatement" || ancestor.type === "ForOfStatement" || ancestor.type === "StaticBlock");
|
|
1621
|
+
}
|
|
1622
|
+
function varBindingScope(ancestors) {
|
|
1623
|
+
return ancestors.findLast((ancestor, index) => {
|
|
1624
|
+
if (ancestor.type === "Program" || ancestor.type === "StaticBlock") {
|
|
1625
|
+
return true;
|
|
1626
|
+
}
|
|
1627
|
+
if (ancestor.type !== "BlockStatement") {
|
|
1628
|
+
return false;
|
|
1629
|
+
}
|
|
1630
|
+
const parent = ancestors[index - 1];
|
|
1631
|
+
return parent?.type === "FunctionDeclaration" || parent?.type === "FunctionExpression" || parent?.type === "ArrowFunctionExpression";
|
|
1632
|
+
});
|
|
1633
|
+
}
|
|
1634
|
+
function collectBuilderBindings(program, bindings) {
|
|
1635
|
+
const builders = [];
|
|
1636
|
+
walk(program, {
|
|
1637
|
+
VariableDeclarator(node, context) {
|
|
1638
|
+
if (!(node.id && node.init && typeof node.id === "object" && typeof node.init === "object")) {
|
|
1639
|
+
return;
|
|
1640
|
+
}
|
|
1641
|
+
const identifier = node.id;
|
|
1642
|
+
const initializer = unwrapTSExpression(node.init);
|
|
1643
|
+
const ancestors = context.ancestors();
|
|
1644
|
+
const declaration = ancestors.at(-1);
|
|
1645
|
+
const scope = declaration?.type === "VariableDeclaration" && declaration.kind === "var" ? varBindingScope(ancestors) : lexicalBindingScope(ancestors);
|
|
1646
|
+
if (scope && identifier.type === "Identifier" && typeof identifier.name === "string" && isCreateIsomorphicCall(initializer, bindings, ancestors)) {
|
|
1647
|
+
builders.push({ name: identifier.name, scope });
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
});
|
|
1651
|
+
return builders;
|
|
1652
|
+
}
|
|
1653
|
+
function hasVisibleBuilder(name, ancestors, builders) {
|
|
1654
|
+
let scopeIndex = -1;
|
|
1655
|
+
for (const builder of builders) {
|
|
1656
|
+
if (builder.name === name) {
|
|
1657
|
+
scopeIndex = Math.max(scopeIndex, ancestors.lastIndexOf(builder.scope));
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
return scopeIndex >= 0 && !hasShadowingDeclaration(name, ancestors.slice(scopeIndex + 1));
|
|
1661
|
+
}
|
|
1662
|
+
function assertNoSplitChains(source, filename, program, builders) {
|
|
1663
|
+
if (builders.length === 0) {
|
|
1664
|
+
return;
|
|
1665
|
+
}
|
|
1666
|
+
walk(program, {
|
|
1667
|
+
MemberExpression(node, context) {
|
|
1668
|
+
if (node.computed || node.object.type !== "Identifier" || typeof node.object.name !== "string" || !hasVisibleBuilder(node.object.name, context.ancestors(), builders) || node.property.type !== "Identifier" || node.property.name !== "server" && node.property.name !== "client") {
|
|
1669
|
+
return;
|
|
1670
|
+
}
|
|
1671
|
+
throw new Error(`[furin] ${filename}:${sourcePosition(source, node.start)} createIsomorphicFn() must use one fluent chain.`);
|
|
1672
|
+
}
|
|
1673
|
+
});
|
|
1674
|
+
}
|
|
1675
|
+
function chainStartsWithCreateIsomorphicFn(expression, bindings, ancestors) {
|
|
1676
|
+
let current = unwrapTSExpression(expression);
|
|
1677
|
+
for (;; ) {
|
|
1678
|
+
if (isCreateIsomorphicCall(current, bindings, ancestors)) {
|
|
1679
|
+
return true;
|
|
1680
|
+
}
|
|
1681
|
+
if (current.type !== "CallExpression" || !environmentMethod(current)) {
|
|
1682
|
+
return false;
|
|
1683
|
+
}
|
|
1684
|
+
const call = current;
|
|
1685
|
+
if (call.callee.type !== "MemberExpression") {
|
|
1686
|
+
return false;
|
|
1687
|
+
}
|
|
1688
|
+
current = unwrapTSExpression(call.callee.object);
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1691
|
+
function assertStaticEnvironmentMethods(source, filename, program, bindings, builders) {
|
|
1692
|
+
walk(program, {
|
|
1693
|
+
MemberExpression(node, context) {
|
|
1694
|
+
if (!node.computed || node.property.type !== "Literal") {
|
|
1695
|
+
return;
|
|
1696
|
+
}
|
|
1697
|
+
if (node.property.value !== "server" && node.property.value !== "client") {
|
|
1698
|
+
return;
|
|
1699
|
+
}
|
|
1700
|
+
const ancestors = context.ancestors();
|
|
1701
|
+
const splitBuilder = node.object.type === "Identifier" && typeof node.object.name === "string" && hasVisibleBuilder(node.object.name, ancestors, builders);
|
|
1702
|
+
if (!(splitBuilder || chainStartsWithCreateIsomorphicFn(node.object, bindings, ancestors))) {
|
|
1703
|
+
return;
|
|
1704
|
+
}
|
|
1705
|
+
throw new Error(`[furin] ${filename}:${sourcePosition(source, node.start)} createIsomorphicFn() requires static .server() and .client() methods.`);
|
|
1706
|
+
}
|
|
1707
|
+
});
|
|
1708
|
+
}
|
|
1709
|
+
function transformIsomorphicFunctions(source, filename, environment) {
|
|
1710
|
+
const lang = detectLangFromPath(filename);
|
|
1711
|
+
if (lang === "dts") {
|
|
1712
|
+
return { code: source, map: null, transformed: false };
|
|
1713
|
+
}
|
|
1714
|
+
const { program, diagnostics } = parseSource(source, lang);
|
|
1715
|
+
const firstError = diagnostics.find((diagnostic) => diagnostic.severity === "error");
|
|
1716
|
+
if (firstError) {
|
|
1717
|
+
throw new Error(`Failed to parse ${filename}: ${firstError.message}`);
|
|
1718
|
+
}
|
|
1719
|
+
const bindings = collectBindings(program);
|
|
1720
|
+
const builders = collectBuilderBindings(program, bindings);
|
|
1721
|
+
assertStaticEnvironmentMethods(source, filename, program, bindings, builders);
|
|
1722
|
+
assertNoSplitChains(source, filename, program, builders);
|
|
1723
|
+
const candidates = collectCandidates(source, filename, program, bindings);
|
|
1724
|
+
if (candidates.length === 0) {
|
|
1725
|
+
return { code: source, map: null, transformed: false };
|
|
1726
|
+
}
|
|
1727
|
+
const transformed = new MagicString(source);
|
|
1728
|
+
for (const candidate of candidates) {
|
|
1729
|
+
const implementation = candidate[environment];
|
|
1730
|
+
transformed.overwrite(candidate.start, candidate.end, implementation ? `(${source.slice(implementation.start, implementation.end)})` : "(() => undefined)");
|
|
1731
|
+
}
|
|
1732
|
+
const pruned = deadCodeElimination(transformed, source, lang);
|
|
1733
|
+
return {
|
|
1734
|
+
code: pruned.toString(),
|
|
1735
|
+
map: pruned.generateMap({ includeContent: true, source: filename }),
|
|
1736
|
+
transformed: true
|
|
1737
|
+
};
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
// src/plugin/transform-client.ts
|
|
1741
|
+
var SERVER_ONLY_PROPERTIES = new Set([
|
|
1742
|
+
"loader",
|
|
1743
|
+
"requestLoader",
|
|
1744
|
+
"query",
|
|
1745
|
+
"params",
|
|
1746
|
+
"staticParams"
|
|
1747
|
+
]);
|
|
1748
|
+
var FURIN_CLIENT_MODULES = new Set(["@teyik0/furin/client", "furin/client"]);
|
|
1749
|
+
function isFurinClientModule(source) {
|
|
1750
|
+
return typeof source === "string" && FURIN_CLIENT_MODULES.has(source);
|
|
1751
|
+
}
|
|
1752
|
+
function isRelativeModule(source) {
|
|
1753
|
+
return typeof source === "string" && source.startsWith(".");
|
|
1754
|
+
}
|
|
1755
|
+
function importedName2(spec) {
|
|
1756
|
+
const { imported } = spec;
|
|
1757
|
+
if (!imported || typeof imported !== "object") {
|
|
1758
|
+
return null;
|
|
1759
|
+
}
|
|
1760
|
+
const node = imported;
|
|
1761
|
+
if (node.type === "Identifier" && typeof node.name === "string") {
|
|
1762
|
+
return node.name;
|
|
1763
|
+
}
|
|
1764
|
+
if (node.type === "Literal" && typeof node.value === "string") {
|
|
1765
|
+
return node.value;
|
|
1766
|
+
}
|
|
1767
|
+
return null;
|
|
1768
|
+
}
|
|
1769
|
+
function localName2(spec) {
|
|
1770
|
+
const { local } = spec;
|
|
1771
|
+
if (!local || typeof local !== "object") {
|
|
1772
|
+
return null;
|
|
1773
|
+
}
|
|
1774
|
+
const node = local;
|
|
1775
|
+
return node.type === "Identifier" && typeof node.name === "string" ? node.name : null;
|
|
1776
|
+
}
|
|
1777
|
+
function addImportedBinding(source, spec, bindings) {
|
|
1778
|
+
if (spec.type !== "ImportSpecifier" || spec.importKind === "type") {
|
|
1779
|
+
return;
|
|
1780
|
+
}
|
|
1781
|
+
const imported = importedName2(spec);
|
|
1782
|
+
const local = localName2(spec);
|
|
1783
|
+
if (!local) {
|
|
1784
|
+
return;
|
|
1785
|
+
}
|
|
1786
|
+
if (isFurinClientModule(source) && imported === "createRoute") {
|
|
1787
|
+
bindings.createRouteNames.add(local);
|
|
1788
|
+
}
|
|
1789
|
+
if (isRelativeModule(source) && imported === "route") {
|
|
1790
|
+
bindings.routeNames.add(local);
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
function collectImportedBindings(program) {
|
|
1794
|
+
const bindings = {
|
|
1795
|
+
createRouteNames: new Set,
|
|
1796
|
+
routeNames: new Set
|
|
1797
|
+
};
|
|
1798
|
+
for (const stmt of program.body) {
|
|
1392
1799
|
if (stmt.type !== "ImportDeclaration") {
|
|
1393
1800
|
continue;
|
|
1394
1801
|
}
|
|
1395
1802
|
const decl = stmt;
|
|
1396
|
-
if (
|
|
1803
|
+
if (decl.importKind === "type") {
|
|
1397
1804
|
continue;
|
|
1398
1805
|
}
|
|
1399
|
-
const
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
} else if (usedCount < decl.specifiers.length) {
|
|
1403
|
-
removeUnusedSpecifiers(fresh, code, decl, refs);
|
|
1806
|
+
const source = decl.source.value;
|
|
1807
|
+
for (const spec of decl.specifiers) {
|
|
1808
|
+
addImportedBinding(source, spec, bindings);
|
|
1404
1809
|
}
|
|
1405
1810
|
}
|
|
1406
|
-
return
|
|
1811
|
+
return bindings;
|
|
1407
1812
|
}
|
|
1408
|
-
function
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1813
|
+
function isCreateRouteCall(node, bindings, ancestors) {
|
|
1814
|
+
return node.callee.type === "Identifier" && bindings.createRouteNames.has(node.callee.name) && !hasShadowingDeclaration(node.callee.name, ancestors);
|
|
1815
|
+
}
|
|
1816
|
+
function isCreateRouteExpression(node, bindings, ancestors) {
|
|
1817
|
+
if (!node || typeof node !== "object") {
|
|
1818
|
+
return false;
|
|
1819
|
+
}
|
|
1820
|
+
const expression = unwrapTSExpression(node);
|
|
1821
|
+
return expression.type === "CallExpression" && isCreateRouteCall(expression, bindings, ancestors);
|
|
1822
|
+
}
|
|
1823
|
+
function collectLocalRouteBindings(program, bindings) {
|
|
1824
|
+
walk2(program, {
|
|
1825
|
+
VariableDeclarator(node, context) {
|
|
1826
|
+
if (node.type !== "VariableDeclarator") {
|
|
1827
|
+
return;
|
|
1828
|
+
}
|
|
1829
|
+
const { id, init } = node;
|
|
1830
|
+
if (!id || typeof id !== "object") {
|
|
1831
|
+
return;
|
|
1832
|
+
}
|
|
1833
|
+
const identifier = id;
|
|
1834
|
+
if (identifier.type !== "Identifier" || typeof identifier.name !== "string") {
|
|
1835
|
+
return;
|
|
1836
|
+
}
|
|
1837
|
+
if (isCreateRouteExpression(init, bindings, context.ancestors())) {
|
|
1838
|
+
bindings.routeNames.add(identifier.name);
|
|
1839
|
+
}
|
|
1413
1840
|
}
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1841
|
+
});
|
|
1842
|
+
}
|
|
1843
|
+
function collectRouteTransformBindings(program) {
|
|
1844
|
+
const bindings = collectImportedBindings(program);
|
|
1845
|
+
collectLocalRouteBindings(program, bindings);
|
|
1846
|
+
return bindings;
|
|
1847
|
+
}
|
|
1848
|
+
function isRoutePageCall(node, bindings, ancestors) {
|
|
1849
|
+
const { callee } = node;
|
|
1850
|
+
if (callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" && callee.property.name === "page") {
|
|
1851
|
+
const object = unwrapTSExpression(callee.object);
|
|
1852
|
+
if (object.type === "Identifier" && bindings.routeNames.has(object.name) && !hasShadowingDeclaration(object.name, ancestors)) {
|
|
1853
|
+
return true;
|
|
1417
1854
|
}
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1855
|
+
return isCreateRouteExpression(object, bindings, ancestors);
|
|
1856
|
+
}
|
|
1857
|
+
return false;
|
|
1858
|
+
}
|
|
1859
|
+
function isTargetCall(node, bindings, ancestors) {
|
|
1860
|
+
return isCreateRouteCall(node, bindings, ancestors) || isRoutePageCall(node, bindings, ancestors);
|
|
1861
|
+
}
|
|
1862
|
+
function isObjectExpressionNode(node) {
|
|
1863
|
+
return node.type === "ObjectExpression";
|
|
1864
|
+
}
|
|
1865
|
+
function staticPropertyName(property) {
|
|
1866
|
+
const { key } = property;
|
|
1867
|
+
if (key.type === "Identifier" && !property.computed && typeof key.name === "string") {
|
|
1868
|
+
return key.name;
|
|
1869
|
+
}
|
|
1870
|
+
if (key.type === "Literal" && typeof key.value === "string") {
|
|
1871
|
+
return key.value;
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
function assertRoutePropertiesAreStaticallySafe(obj) {
|
|
1875
|
+
for (const property of obj.properties) {
|
|
1876
|
+
if (property.type !== "Property") {
|
|
1877
|
+
throw new Error("[furin] Route configuration spreads are not allowed because server-only properties cannot be stripped safely.");
|
|
1421
1878
|
}
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
return;
|
|
1879
|
+
if (property.computed && staticPropertyName(property) === undefined) {
|
|
1880
|
+
throw new Error("[furin] A dynamic computed property in route configuration cannot be stripped safely.");
|
|
1425
1881
|
}
|
|
1426
|
-
|
|
1427
|
-
|
|
1882
|
+
}
|
|
1883
|
+
}
|
|
1884
|
+
function removeServerProperties(s, source, obj) {
|
|
1885
|
+
assertRoutePropertiesAreStaticallySafe(obj);
|
|
1886
|
+
const toRemove = obj.properties.filter((p) => {
|
|
1887
|
+
if (p.type !== "Property") {
|
|
1888
|
+
return false;
|
|
1889
|
+
}
|
|
1890
|
+
const name = staticPropertyName(p);
|
|
1891
|
+
return name !== undefined && SERVER_ONLY_PROPERTIES.has(name);
|
|
1892
|
+
});
|
|
1893
|
+
if (toRemove.length === 0) {
|
|
1894
|
+
return false;
|
|
1895
|
+
}
|
|
1896
|
+
for (const prop of toRemove) {
|
|
1897
|
+
let removeEnd = prop.end;
|
|
1898
|
+
while (removeEnd < source.length && (source[removeEnd] === "," || source[removeEnd] === " " || source[removeEnd] === `
|
|
1899
|
+
` || source[removeEnd] === "\r" || source[removeEnd] === "\t")) {
|
|
1900
|
+
if (source[removeEnd] === ",") {
|
|
1901
|
+
removeEnd += 1;
|
|
1902
|
+
break;
|
|
1903
|
+
}
|
|
1904
|
+
removeEnd += 1;
|
|
1905
|
+
}
|
|
1906
|
+
let removeStart = prop.start;
|
|
1907
|
+
while (removeStart > 0 && (source[removeStart - 1] === " " || source[removeStart - 1] === "\t")) {
|
|
1908
|
+
removeStart -= 1;
|
|
1909
|
+
}
|
|
1910
|
+
if (removeStart > 0 && source[removeStart - 1] === `
|
|
1911
|
+
`) {
|
|
1912
|
+
removeStart -= 1;
|
|
1913
|
+
if (removeStart > 0 && source[removeStart - 1] === "\r") {
|
|
1914
|
+
removeStart -= 1;
|
|
1915
|
+
}
|
|
1916
|
+
}
|
|
1917
|
+
s.remove(removeStart, removeEnd);
|
|
1918
|
+
}
|
|
1919
|
+
return true;
|
|
1920
|
+
}
|
|
1921
|
+
function removeServerExports(s, source, program) {
|
|
1922
|
+
let removedServerCode = false;
|
|
1923
|
+
const bindings = collectRouteTransformBindings(program);
|
|
1924
|
+
walk2(program, {
|
|
1925
|
+
CallExpression(call, context) {
|
|
1926
|
+
if (!isTargetCall(call, bindings, context.ancestors())) {
|
|
1927
|
+
return;
|
|
1928
|
+
}
|
|
1929
|
+
const [firstArg] = call.arguments;
|
|
1930
|
+
if (!firstArg) {
|
|
1931
|
+
return;
|
|
1932
|
+
}
|
|
1933
|
+
const arg = unwrapTSExpression(firstArg);
|
|
1934
|
+
if (!isObjectExpressionNode(arg)) {
|
|
1935
|
+
return;
|
|
1936
|
+
}
|
|
1937
|
+
if (removeServerProperties(s, source, arg)) {
|
|
1938
|
+
removedServerCode = true;
|
|
1939
|
+
}
|
|
1428
1940
|
}
|
|
1429
1941
|
});
|
|
1430
1942
|
return removedServerCode;
|
|
@@ -1434,19 +1946,22 @@ function transformForClient(code, filename) {
|
|
|
1434
1946
|
if (lang === "dts") {
|
|
1435
1947
|
return { code, map: null, removedServerCode: false };
|
|
1436
1948
|
}
|
|
1437
|
-
const
|
|
1949
|
+
const isomorphicResult = transformIsomorphicFunctions(code, filename, "client");
|
|
1950
|
+
const clientSource = isomorphicResult.code;
|
|
1951
|
+
const { program, diagnostics } = parseSource(clientSource, lang);
|
|
1438
1952
|
const firstError = diagnostics.find((d) => d.severity === "error");
|
|
1439
1953
|
if (firstError) {
|
|
1440
1954
|
throw new Error(`Failed to parse ${filename}: ${firstError.message}`);
|
|
1441
1955
|
}
|
|
1442
|
-
let s = new MagicString(
|
|
1443
|
-
const
|
|
1956
|
+
let s = new MagicString(clientSource);
|
|
1957
|
+
const removedRouteCode = removeServerExports(s, clientSource, program);
|
|
1958
|
+
const removedServerCode = isomorphicResult.transformed || removedRouteCode;
|
|
1444
1959
|
if (removedServerCode) {
|
|
1445
|
-
s = deadCodeElimination(s, lang);
|
|
1960
|
+
s = deadCodeElimination(s, code, lang);
|
|
1446
1961
|
}
|
|
1447
1962
|
return {
|
|
1448
1963
|
code: s.toString(),
|
|
1449
|
-
map: s.generateMap({
|
|
1964
|
+
map: s.generateMap({ includeContent: true, source: filename }),
|
|
1450
1965
|
removedServerCode
|
|
1451
1966
|
};
|
|
1452
1967
|
}
|
|
@@ -1455,7 +1970,7 @@ function transformForClient(code, filename) {
|
|
|
1455
1970
|
var ELYSIA_FILTER = /^elysia$/;
|
|
1456
1971
|
var BUN_BUILTIN_FILTER = /^bun:/;
|
|
1457
1972
|
var ANY_FILTER = /.*/;
|
|
1458
|
-
var
|
|
1973
|
+
var SCRIPT_FILE_FILTER = /\.(tsx?|jsx?)$/;
|
|
1459
1974
|
var ELYSIA_STUB = `export const t = new Proxy({}, { get: () => (...args) => args[0] ?? {} });
|
|
1460
1975
|
export class NotFoundError extends Error { constructor(m) { super(m); this.name = "NotFoundError"; } }
|
|
1461
1976
|
export class ValidationError extends Error { constructor(m) { super(m); this.name = "ValidationError"; } }
|
|
@@ -1465,32 +1980,27 @@ var plugin = {
|
|
|
1465
1980
|
name: "furin-strip-server",
|
|
1466
1981
|
setup(build) {
|
|
1467
1982
|
build.onResolve({ filter: ELYSIA_FILTER }, () => ({
|
|
1468
|
-
|
|
1469
|
-
|
|
1983
|
+
namespace: "furin-stubs",
|
|
1984
|
+
path: "elysia-stub"
|
|
1470
1985
|
}));
|
|
1471
1986
|
build.onResolve({ filter: BUN_BUILTIN_FILTER }, () => ({
|
|
1472
|
-
|
|
1473
|
-
|
|
1987
|
+
namespace: "furin-stubs",
|
|
1988
|
+
path: "bun-builtin-stub"
|
|
1474
1989
|
}));
|
|
1475
|
-
build.onLoad({ namespace: "furin-stubs"
|
|
1990
|
+
build.onLoad({ filter: ANY_FILTER, namespace: "furin-stubs" }, (args) => ({
|
|
1476
1991
|
contents: args.path === "elysia-stub" ? ELYSIA_STUB : "",
|
|
1477
1992
|
loader: "js"
|
|
1478
1993
|
}));
|
|
1479
|
-
build.onLoad({ filter:
|
|
1994
|
+
build.onLoad({ filter: SCRIPT_FILE_FILTER }, async (args) => {
|
|
1480
1995
|
if (args.path.includes("node_modules")) {
|
|
1481
1996
|
return;
|
|
1482
1997
|
}
|
|
1483
1998
|
const source = await Bun.file(args.path).text();
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
};
|
|
1490
|
-
} catch (err) {
|
|
1491
|
-
console.error(`[furin] strip-plugin transform error for ${args.path}:`, err);
|
|
1492
|
-
return;
|
|
1493
|
-
}
|
|
1999
|
+
const result = transformForClient(source, args.path);
|
|
2000
|
+
return {
|
|
2001
|
+
contents: result.code,
|
|
2002
|
+
loader: detectLoaderFromPath(args.path)
|
|
2003
|
+
};
|
|
1494
2004
|
});
|
|
1495
2005
|
}
|
|
1496
2006
|
};
|