rolldown-plugin-concurrent-top-level-await 0.0.1 → 0.0.3
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/LICENSE +21 -0
- package/dist/index.d.mts +11 -0
- package/dist/index.mjs +451 -0
- package/package.json +6 -9
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Oliver Zell
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
declare function concurrentTopLevelAwait(options?: {
|
|
3
|
+
/**
|
|
4
|
+
* Prefix used for internal variables generated by the plugin.
|
|
5
|
+
* Change this if it conflicts with variable names in your code.
|
|
6
|
+
* @default "__tla"
|
|
7
|
+
*/
|
|
8
|
+
generatedVariablePrefix?: string;
|
|
9
|
+
}): any[];
|
|
10
|
+
//#endregion
|
|
11
|
+
export { concurrentTopLevelAwait as default };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { RolldownMagicString } from "rolldown";
|
|
3
|
+
import { transform } from "rolldown/utils";
|
|
4
|
+
|
|
5
|
+
//#region src/awaitEntriesPlugin.ts
|
|
6
|
+
function awaitEntrypointsPlugin(options) {
|
|
7
|
+
const generatedVariablePrefix = options.generatedVariablePrefix;
|
|
8
|
+
const proxyPrefix = `\0${generatedVariablePrefix}AwaitEntry:`;
|
|
9
|
+
const awaitedEntriesMap = /* @__PURE__ */ new Map();
|
|
10
|
+
return {
|
|
11
|
+
name: "rolldown-plugin-concurrent-tla-plugin-await-entrypoints",
|
|
12
|
+
apply: "build",
|
|
13
|
+
resolveId: {
|
|
14
|
+
order: "pre",
|
|
15
|
+
async handler(source, importer, options$1) {
|
|
16
|
+
if (importer?.startsWith(proxyPrefix)) {
|
|
17
|
+
const key$1 = importer.slice(proxyPrefix.length);
|
|
18
|
+
const resolved$1 = awaitedEntriesMap.get(key$1);
|
|
19
|
+
if (!resolved$1) throw new Error("Name collision in concurrent-tla plugin, please change the generatedVariablePrefix option");
|
|
20
|
+
return resolved$1;
|
|
21
|
+
}
|
|
22
|
+
if (!options$1.isEntry && options$1.kind !== "dynamic-import") return;
|
|
23
|
+
const resolved = await this.resolve(source, importer, options$1);
|
|
24
|
+
if (resolved == null) return;
|
|
25
|
+
const key = randomUUID();
|
|
26
|
+
awaitedEntriesMap.set(key, resolved);
|
|
27
|
+
return `${proxyPrefix}${key}`;
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
load: {
|
|
31
|
+
order: "pre",
|
|
32
|
+
async handler(id) {
|
|
33
|
+
if (id.startsWith(proxyPrefix)) {
|
|
34
|
+
const resolved = awaitedEntriesMap.get(id.slice(proxyPrefix.length));
|
|
35
|
+
if (!resolved) throw new Error("Name collision in concurrent-tla plugin, please change the generatedVariablePrefix option");
|
|
36
|
+
if ((await this.load(resolved)).meta[generatedVariablePrefix + "_async"]) return `import { ${generatedVariablePrefix}_access } from "${resolved.id}";
|
|
37
|
+
await new Promise(${generatedVariablePrefix}_access);
|
|
38
|
+
export * from "${resolved.id}";
|
|
39
|
+
`;
|
|
40
|
+
return `export * from "${resolved.id}";
|
|
41
|
+
`;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region src/registerModulePlugin.ts
|
|
50
|
+
const tlaModule = `export default function register(fn, evaluate_accesses) {
|
|
51
|
+
let state = "ready";
|
|
52
|
+
let whenDones = [];
|
|
53
|
+
let whenErrors = [];
|
|
54
|
+
let remaining = 1;
|
|
55
|
+
let error = undefined;
|
|
56
|
+
// evaluate eagerly
|
|
57
|
+
evaluate();
|
|
58
|
+
|
|
59
|
+
return evaluate;
|
|
60
|
+
|
|
61
|
+
function evaluate(whenDone, onError) {
|
|
62
|
+
if (state === "done") {
|
|
63
|
+
if (whenDone)
|
|
64
|
+
whenDone();
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (state === "failed") {
|
|
68
|
+
if (onError)
|
|
69
|
+
onError(error);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (whenDone)
|
|
73
|
+
whenDones.push(whenDone);
|
|
74
|
+
if (onError)
|
|
75
|
+
whenErrors.push(onError);
|
|
76
|
+
if (state === "busy") {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
state = "busy";
|
|
80
|
+
let moduleDone = () => {
|
|
81
|
+
state = "done";
|
|
82
|
+
for (let x of whenDones)
|
|
83
|
+
x();
|
|
84
|
+
};
|
|
85
|
+
let moduleError = (err) => {
|
|
86
|
+
state = "failed";
|
|
87
|
+
error = err;
|
|
88
|
+
for (let x of whenErrors)
|
|
89
|
+
x(err);
|
|
90
|
+
};
|
|
91
|
+
let importDone = () => {
|
|
92
|
+
if (state !== "busy")
|
|
93
|
+
return;
|
|
94
|
+
if (--remaining !== 0)
|
|
95
|
+
return;
|
|
96
|
+
try {
|
|
97
|
+
let result = fn();
|
|
98
|
+
if (result) {
|
|
99
|
+
result
|
|
100
|
+
.then(moduleDone)
|
|
101
|
+
.catch(moduleError);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
moduleDone();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
moduleError(err);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
for (const access of evaluate_accesses) {
|
|
112
|
+
let evaluate;
|
|
113
|
+
try {
|
|
114
|
+
// Environment-dependent behavior:
|
|
115
|
+
// - throws on cyclic dependencies in V8
|
|
116
|
+
// - returns undefined in some environments (e.g., vitest)
|
|
117
|
+
evaluate = access();
|
|
118
|
+
if (evaluate == null)
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
catch (_a) {
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
remaining++;
|
|
125
|
+
evaluate(importDone, moduleError);
|
|
126
|
+
}
|
|
127
|
+
importDone();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
`;
|
|
131
|
+
function registerModulePlugin(options) {
|
|
132
|
+
options.generatedVariablePrefix;
|
|
133
|
+
const registerModuleSource = options.registerModuleSource;
|
|
134
|
+
return {
|
|
135
|
+
name: "rolldown-plugin-concurrent-tla-plugin-register-module",
|
|
136
|
+
apply: "build",
|
|
137
|
+
resolveId(source) {
|
|
138
|
+
if (source === registerModuleSource) return registerModuleSource;
|
|
139
|
+
},
|
|
140
|
+
load(id) {
|
|
141
|
+
if (id === registerModuleSource) return tlaModule;
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region ../shared/src/ast.ts
|
|
148
|
+
function isFunctionNode(node) {
|
|
149
|
+
return node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression" || node.type === "MethodDefinition" || node.type === "Property" && node.value.type === "FunctionExpression";
|
|
150
|
+
}
|
|
151
|
+
function visitScope(node, cb) {
|
|
152
|
+
if (node?.type == null) return false;
|
|
153
|
+
if (cb(node)) return true;
|
|
154
|
+
if (isFunctionNode(node)) return false;
|
|
155
|
+
return Object.values(node).flat().some((n) => visitScope(n, cb));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
//#endregion
|
|
159
|
+
//#region ../shared/src/hasTopLevelAwait.ts
|
|
160
|
+
function isAwaitNode(node) {
|
|
161
|
+
return node.type === "AwaitExpression" || node.type === "ForOfStatement" && node.await || node.type === "VariableDeclaration" && node.kind === "await using";
|
|
162
|
+
}
|
|
163
|
+
function hasTopLevelAwait(node) {
|
|
164
|
+
return visitScope(node, isAwaitNode);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region ../shared/src/polyfills/promise.ts
|
|
169
|
+
function withResolvers() {
|
|
170
|
+
let resolve;
|
|
171
|
+
let reject;
|
|
172
|
+
return {
|
|
173
|
+
promise: new Promise((res, rej) => {
|
|
174
|
+
resolve = res;
|
|
175
|
+
reject = rej;
|
|
176
|
+
}),
|
|
177
|
+
resolve,
|
|
178
|
+
reject
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
//#endregion
|
|
183
|
+
//#region ../shared/src/AsyncModuleTracker.ts
|
|
184
|
+
var AwaitableCache = class {
|
|
185
|
+
#store = /* @__PURE__ */ new Map();
|
|
186
|
+
#getPromise(key) {
|
|
187
|
+
const entry = this.#store.get(key);
|
|
188
|
+
if (entry) return entry;
|
|
189
|
+
const { promise, resolve, reject } = withResolvers();
|
|
190
|
+
this.#store.set(key, [
|
|
191
|
+
promise,
|
|
192
|
+
resolve,
|
|
193
|
+
reject
|
|
194
|
+
]);
|
|
195
|
+
return [
|
|
196
|
+
promise,
|
|
197
|
+
resolve,
|
|
198
|
+
reject
|
|
199
|
+
];
|
|
200
|
+
}
|
|
201
|
+
resolve(key, value) {
|
|
202
|
+
const [_, resolve] = this.#getPromise(key);
|
|
203
|
+
resolve(value);
|
|
204
|
+
}
|
|
205
|
+
reject(key) {
|
|
206
|
+
const [_, __, reject] = this.#getPromise(key);
|
|
207
|
+
reject();
|
|
208
|
+
}
|
|
209
|
+
get(key) {
|
|
210
|
+
const [promise] = this.#getPromise(key);
|
|
211
|
+
return promise;
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
/**
|
|
215
|
+
* AsyncModuleTracker tracks whether modules must be treated as async due to
|
|
216
|
+
* containing top-level await or being an ancestor of an async module.
|
|
217
|
+
*/
|
|
218
|
+
var AsyncModuleTracker = class {
|
|
219
|
+
#entryCache = new AwaitableCache();
|
|
220
|
+
#subtreeCache = new AwaitableCache();
|
|
221
|
+
#resultCache = /* @__PURE__ */ new Map();
|
|
222
|
+
#seen = /* @__PURE__ */ new Set();
|
|
223
|
+
#resolved = /* @__PURE__ */ new Set();
|
|
224
|
+
#childrenSeen = /* @__PURE__ */ new Set();
|
|
225
|
+
#cycleResolver;
|
|
226
|
+
#resolveCycles;
|
|
227
|
+
constructor() {
|
|
228
|
+
this.#createCycleResolver();
|
|
229
|
+
}
|
|
230
|
+
#createCycleResolver() {
|
|
231
|
+
const { promise, resolve } = withResolvers();
|
|
232
|
+
this.#cycleResolver = promise;
|
|
233
|
+
this.#resolveCycles = () => resolve(false);
|
|
234
|
+
}
|
|
235
|
+
#checkForCycles() {
|
|
236
|
+
if (this.#childrenSeen.size === this.#seen.size && this.#resolved.size === this.#seen.size) {
|
|
237
|
+
const resolveCycles = this.#resolveCycles;
|
|
238
|
+
this.#createCycleResolver();
|
|
239
|
+
setTimeout(() => {
|
|
240
|
+
resolveCycles();
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
#bindResultCache(key) {
|
|
245
|
+
if (!this.#resultCache.has(key)) this.#resultCache.set(key, this.#subtreeCache.get(key).then(() => false).catch(() => true));
|
|
246
|
+
}
|
|
247
|
+
isAsync(key) {
|
|
248
|
+
this.#bindResultCache(key);
|
|
249
|
+
return this.#resultCache.get(key);
|
|
250
|
+
}
|
|
251
|
+
setEntryAsync(key, value) {
|
|
252
|
+
this.#seen.add(key);
|
|
253
|
+
this.#resolved.add(key);
|
|
254
|
+
if (value) {
|
|
255
|
+
this.#entryCache.reject(key);
|
|
256
|
+
this.#entryCache.get(key).catch(() => {});
|
|
257
|
+
} else this.#entryCache.resolve(key, void 0);
|
|
258
|
+
this.#checkForCycles();
|
|
259
|
+
}
|
|
260
|
+
setDependencies(key, children) {
|
|
261
|
+
this.#seen.add(key);
|
|
262
|
+
this.#childrenSeen.add(key);
|
|
263
|
+
children.forEach((child) => {
|
|
264
|
+
this.#seen.add(child);
|
|
265
|
+
});
|
|
266
|
+
Promise.race([this.#cycleResolver, Promise.all([this.#entryCache.get(key), ...children.map((child) => this.#subtreeCache.get(child))])]).then(() => this.#subtreeCache.resolve(key, void 0)).catch(() => this.#subtreeCache.reject(key));
|
|
267
|
+
this.#bindResultCache(key);
|
|
268
|
+
this.#checkForCycles();
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
//#endregion
|
|
273
|
+
//#region ../shared/src/transform.ts
|
|
274
|
+
function transform$1(s, ast, registerModuleSource, asyncImports, hasAwait, variablePrefix) {
|
|
275
|
+
const declarationsEnd = transformAndMoveDeclarationsToModuleScope(s, ast, asyncImports, variablePrefix);
|
|
276
|
+
s.appendRight(declarationsEnd, `${hasAwait ? "async " : ""}function ${variablePrefix}_initModuleExports() {\n`);
|
|
277
|
+
s.append("\n}\n");
|
|
278
|
+
s.prepend(`import ${variablePrefix}_register from ${JSON.stringify(registerModuleSource)};\n`);
|
|
279
|
+
const asyncDeps = `[${asyncImports.map((_, i) => `() => ${variablePrefix}${i}`).join(", ")}]`;
|
|
280
|
+
s.append(`export const ${variablePrefix}_access = ${variablePrefix}_register(${variablePrefix}_initModuleExports, ${asyncDeps});\n`);
|
|
281
|
+
}
|
|
282
|
+
function transformAndMoveDeclarationsToModuleScope(s, ast, asyncImports, variablePrefix) {
|
|
283
|
+
let moduleScopeEnd = 0;
|
|
284
|
+
let i = 0;
|
|
285
|
+
for (const node of ast.body) {
|
|
286
|
+
if (asyncImports.includes(node)) {
|
|
287
|
+
const tlaImport = `\nimport { ${variablePrefix}_access as ${variablePrefix}${i}} from '${node.source.value}';`;
|
|
288
|
+
s.appendLeft(node.end, tlaImport);
|
|
289
|
+
i++;
|
|
290
|
+
}
|
|
291
|
+
if (node.type === "ClassDeclaration") {
|
|
292
|
+
s.appendLeft(moduleScopeEnd, `let ${node.id.name};\n`);
|
|
293
|
+
s.appendRight(node.start, `${node.id.name} = `);
|
|
294
|
+
}
|
|
295
|
+
if (node.type === "ExportNamedDeclaration") {
|
|
296
|
+
if (node.declaration?.type === "VariableDeclaration") {
|
|
297
|
+
s.appendLeft(moduleScopeEnd, "export ");
|
|
298
|
+
s.remove(node.start, node.declaration.start);
|
|
299
|
+
moveVariableDeclarationToModuleScope(s, node.declaration, moduleScopeEnd);
|
|
300
|
+
} else if (node.declaration?.type === "ClassDeclaration") {
|
|
301
|
+
s.appendLeft(moduleScopeEnd, `export let ${node.declaration.id.name};\n`);
|
|
302
|
+
const declarationStart = getClassDeclarationStart(node.declaration);
|
|
303
|
+
s.remove(node.start, declarationStart);
|
|
304
|
+
s.appendRight(declarationStart, `${node.declaration.id.name} = `);
|
|
305
|
+
}
|
|
306
|
+
} else if (node.type === "VariableDeclaration") if (node.kind.endsWith("using")) moveVariableDeclarationWithUsingToModuleScope(s, node, moduleScopeEnd, variablePrefix);
|
|
307
|
+
else moveVariableDeclarationToModuleScope(s, node, moduleScopeEnd);
|
|
308
|
+
else visitScope(node, (n) => {
|
|
309
|
+
if (n.type === "VariableDeclaration" && n.kind === "var") moveVariableDeclarationToModuleScope(s, n, moduleScopeEnd);
|
|
310
|
+
return false;
|
|
311
|
+
});
|
|
312
|
+
if (node.type === "ExportDefaultDeclaration" && !isFunctionDeclaration(node.declaration.type)) {
|
|
313
|
+
const variableName = node.declaration.type === "ClassDeclaration" ? node.declaration.id?.name ?? `${variablePrefix}_default` : `${variablePrefix}_default`;
|
|
314
|
+
s.appendLeft(moduleScopeEnd, `let ${variableName};\nexport { ${variableName} as default };\n`);
|
|
315
|
+
const declarationStart = node.declaration.type === "ClassDeclaration" ? getClassDeclarationStart(node.declaration) : node.declaration.start;
|
|
316
|
+
s.remove(node.start, declarationStart);
|
|
317
|
+
s.appendRight(declarationStart, `${variableName} = (`);
|
|
318
|
+
s.appendLeft(node.declaration.end, ");");
|
|
319
|
+
}
|
|
320
|
+
if (isFunctionDeclaration(node.type) || node.type === "ImportDeclaration" || node.type === "ExportDefaultDeclaration" && isFunctionDeclaration(node.declaration.type) || node.type === "ExportNamedDeclaration" && isFunctionDeclaration(node.declaration?.type) || node.type === "ExportNamedDeclaration" && node.declaration == null || node.type === "ExportAllDeclaration") if (node.start > moduleScopeEnd) {
|
|
321
|
+
s.appendLeft(node.end, "\n");
|
|
322
|
+
s.move(node.start, node.end, moduleScopeEnd);
|
|
323
|
+
s.appendLeft(node.start, ";");
|
|
324
|
+
} else moduleScopeEnd = node.end;
|
|
325
|
+
}
|
|
326
|
+
return moduleScopeEnd;
|
|
327
|
+
}
|
|
328
|
+
function isFunctionDeclaration(type) {
|
|
329
|
+
return type === "FunctionDeclaration";
|
|
330
|
+
}
|
|
331
|
+
function getClassDeclarationStart(node) {
|
|
332
|
+
return node.decorators[0]?.start ?? node.start;
|
|
333
|
+
}
|
|
334
|
+
function moveVariableDeclarationToModuleScope(s, node, declarationsEnd) {
|
|
335
|
+
const kind = replaceConstWithLet(node.kind);
|
|
336
|
+
const names = node.declarations.flatMap((decl) => getNames(decl.id)).join(", ");
|
|
337
|
+
s.appendRight(node.declarations[0].start, ";(");
|
|
338
|
+
s.appendLeft(node.declarations[node.declarations.length - 1].end, ")");
|
|
339
|
+
s.appendLeft(declarationsEnd, `${kind} ${names};\n`);
|
|
340
|
+
s.remove(node.start, node.declarations[0].start);
|
|
341
|
+
return s;
|
|
342
|
+
}
|
|
343
|
+
function moveVariableDeclarationWithUsingToModuleScope(s, node, declarationsEnd, variablePrefix) {
|
|
344
|
+
node.declarations.forEach((declaration) => {
|
|
345
|
+
const id = declaration.id;
|
|
346
|
+
if (id.type !== "Identifier") throw new Error("'using' declarations may not have binding patterns.");
|
|
347
|
+
const name = id.name;
|
|
348
|
+
s.appendRight(id.start, `${variablePrefix}_using_`);
|
|
349
|
+
s.appendLeft(node.end, `\n${name} = ${variablePrefix}_using_${name};`);
|
|
350
|
+
s.appendLeft(declarationsEnd, `let ${name};\n`);
|
|
351
|
+
});
|
|
352
|
+
return s;
|
|
353
|
+
}
|
|
354
|
+
function replaceConstWithLet(value) {
|
|
355
|
+
if (value === "const") return "let";
|
|
356
|
+
return value;
|
|
357
|
+
}
|
|
358
|
+
function getNames(pattern) {
|
|
359
|
+
switch (pattern.type) {
|
|
360
|
+
case "Identifier": return [pattern.name];
|
|
361
|
+
case "MemberExpression": throw new Error("Unexpected member expression in variable declaration");
|
|
362
|
+
case "ObjectPattern": return pattern.properties.flatMap((p) => {
|
|
363
|
+
switch (p.type) {
|
|
364
|
+
case "Property": return getNames(p.value);
|
|
365
|
+
case "RestElement": return getNames(p.argument);
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
case "ArrayPattern": return pattern.elements.filter((e) => e != null).flatMap(getNames);
|
|
369
|
+
case "RestElement": return getNames(pattern.argument);
|
|
370
|
+
case "AssignmentPattern": return getNames(pattern.left);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
//#endregion
|
|
375
|
+
//#region src/transformPlugin.ts
|
|
376
|
+
const supportedModuleTypes = new Set([
|
|
377
|
+
"js",
|
|
378
|
+
"jsx",
|
|
379
|
+
"ts",
|
|
380
|
+
"tsx"
|
|
381
|
+
]);
|
|
382
|
+
function resolveDeclarationSource(context, id, importerAttributes = {}, declaration) {
|
|
383
|
+
return context.resolve(declaration.source.value, id, {
|
|
384
|
+
attributes: Object.fromEntries(declaration.attributes.map((attr) => [attr.key.type === "Identifier" ? attr.key.name : attr.key.value, attr.value.value])),
|
|
385
|
+
importerAttributes,
|
|
386
|
+
custom: {}
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
function transformPlugin(options) {
|
|
390
|
+
const generatedVariablePrefix = options.generatedVariablePrefix;
|
|
391
|
+
const asyncTracker = new AsyncModuleTracker();
|
|
392
|
+
return {
|
|
393
|
+
name: "rolldown-plugin-concurrent-tla-plugin-transform",
|
|
394
|
+
apply: "build",
|
|
395
|
+
transform: { async handler(_code, id, transformOptions) {
|
|
396
|
+
if (id.startsWith(`\0${generatedVariablePrefix}AwaitEntry:`)) return;
|
|
397
|
+
if (!supportedModuleTypes.has(transformOptions?.moduleType)) {
|
|
398
|
+
asyncTracker.setEntryAsync(id, false);
|
|
399
|
+
asyncTracker.setDependencies(id, []);
|
|
400
|
+
}
|
|
401
|
+
const code = (await transform(id, _code)).code;
|
|
402
|
+
const ast = this.parse(code);
|
|
403
|
+
const importDeclarations = ast.body.filter((a) => a.type === "ImportDeclaration");
|
|
404
|
+
const hasAwait = hasTopLevelAwait(ast);
|
|
405
|
+
asyncTracker.setEntryAsync(id, hasAwait);
|
|
406
|
+
if (hasAwait) asyncTracker.setDependencies(id, []);
|
|
407
|
+
let imports = (await Promise.all(importDeclarations.map(async (declaration) => {
|
|
408
|
+
const importId = await resolveDeclarationSource(this, id, transformOptions?.attributes, declaration);
|
|
409
|
+
if (!importId) return null;
|
|
410
|
+
return {
|
|
411
|
+
declaration,
|
|
412
|
+
id: importId.id
|
|
413
|
+
};
|
|
414
|
+
}))).filter(Boolean);
|
|
415
|
+
if (!hasAwait) asyncTracker.setDependencies(id, imports.map((x) => x.id));
|
|
416
|
+
const asyncImports = (await Promise.all(imports.map(async ({ declaration, id: id$1 }) => {
|
|
417
|
+
this.load({ id: id$1 });
|
|
418
|
+
if (!await asyncTracker.isAsync(id$1)) return null;
|
|
419
|
+
return declaration;
|
|
420
|
+
}))).filter(Boolean);
|
|
421
|
+
if (!(asyncImports.length > 0 || hasAwait)) return;
|
|
422
|
+
const { magicString: _magicString } = transformOptions;
|
|
423
|
+
const magicString = new RolldownMagicString(code);
|
|
424
|
+
if (!_magicString) throw new Error("experimental.nativeMagicString must be enabled in the Rolldown options to use the concurrent-tla plugin");
|
|
425
|
+
transform$1(magicString, ast, options.registerModuleSource, asyncImports, hasAwait, generatedVariablePrefix);
|
|
426
|
+
return {
|
|
427
|
+
code: magicString,
|
|
428
|
+
meta: { [generatedVariablePrefix + "_async"]: true }
|
|
429
|
+
};
|
|
430
|
+
} }
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
//#endregion
|
|
435
|
+
//#region src/index.ts
|
|
436
|
+
function concurrentTopLevelAwait(options = {}) {
|
|
437
|
+
const generatedVariablePrefix = options.generatedVariablePrefix ?? "__tla";
|
|
438
|
+
const enrichedOptions = {
|
|
439
|
+
...options,
|
|
440
|
+
generatedVariablePrefix,
|
|
441
|
+
registerModuleSource: `\0${generatedVariablePrefix}Register`
|
|
442
|
+
};
|
|
443
|
+
return [
|
|
444
|
+
awaitEntrypointsPlugin(enrichedOptions),
|
|
445
|
+
registerModulePlugin(enrichedOptions),
|
|
446
|
+
transformPlugin(enrichedOptions)
|
|
447
|
+
];
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
//#endregion
|
|
451
|
+
export { concurrentTopLevelAwait as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rolldown-plugin-concurrent-top-level-await",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "Rolldown (and Vite) plugin enabling concurrent execution of modules that contain top level await.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"rolldown-plugin",
|
|
@@ -22,9 +22,6 @@
|
|
|
22
22
|
"files": [
|
|
23
23
|
"dist"
|
|
24
24
|
],
|
|
25
|
-
"scripts": {
|
|
26
|
-
"build": "tsdown --dts"
|
|
27
|
-
},
|
|
28
25
|
"repository": {
|
|
29
26
|
"type": "git",
|
|
30
27
|
"url": "git+https://github.com/zOadT/concurrent-top-level-await-plugins.git"
|
|
@@ -36,7 +33,7 @@
|
|
|
36
33
|
},
|
|
37
34
|
"homepage": "https://github.com/zOadT/concurrent-top-level-await-plugins/tree/main/packages/rolldown-plugin#readme",
|
|
38
35
|
"peerDependencies": {
|
|
39
|
-
"rolldown": "^1.0.0-rc.0",
|
|
36
|
+
"rolldown": "^1.0.0 || ^1.0.0-rc.0",
|
|
40
37
|
"vite": "^8.0.0"
|
|
41
38
|
},
|
|
42
39
|
"peerDependenciesMeta": {
|
|
@@ -54,12 +51,12 @@
|
|
|
54
51
|
"reason": "Uses Rolldown-specific APIs"
|
|
55
52
|
}
|
|
56
53
|
},
|
|
57
|
-
"dependencies": {
|
|
58
|
-
"@rolldown/pluginutils": "1.0.0-rc.9"
|
|
59
|
-
},
|
|
60
54
|
"devDependencies": {
|
|
61
55
|
"@types/estree": "^1.0.8",
|
|
62
56
|
"prettier": "3.7.4",
|
|
63
57
|
"rolldown": "1.0.0-rc.9"
|
|
58
|
+
},
|
|
59
|
+
"scripts": {
|
|
60
|
+
"build": "tsdown --dts"
|
|
64
61
|
}
|
|
65
|
-
}
|
|
62
|
+
}
|