rollup-plugin-concurrent-top-level-await 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +23 -87
- package/package.json +3 -2
package/dist/index.d.mts
CHANGED
|
@@ -18,7 +18,7 @@ declare function concurrentTopLevelAwait(options?: {
|
|
|
18
18
|
name: string;
|
|
19
19
|
apply: "build";
|
|
20
20
|
resolveId(this: rollup0.PluginContext, source: string): string | undefined;
|
|
21
|
-
load(this: rollup0.PluginContext, id: string):
|
|
21
|
+
load(this: rollup0.PluginContext, id: string): string | undefined;
|
|
22
22
|
transform: {
|
|
23
23
|
handler(this: TransformPluginContext, code: string, id: string, transformOptions: {
|
|
24
24
|
ssr?: boolean | undefined;
|
package/dist/index.mjs
CHANGED
|
@@ -132,18 +132,24 @@ function transform(s, ast, registerModuleSource, asyncImports, hasAwait, variabl
|
|
|
132
132
|
const declarationsEnd = transformAndMoveDeclarationsToModuleScope(s, ast, asyncImports, variablePrefix);
|
|
133
133
|
s.appendRight(declarationsEnd, `${hasAwait ? "async " : ""}function ${variablePrefix}_initModuleExports() {\n`);
|
|
134
134
|
s.append("\n}\n");
|
|
135
|
-
s.
|
|
135
|
+
s.append(`import ${variablePrefix}_register from ${JSON.stringify(registerModuleSource)};\n`);
|
|
136
136
|
const asyncDeps = `[${asyncImports.map((_, i) => `() => ${variablePrefix}${i}`).join(", ")}]`;
|
|
137
137
|
s.append(`export const ${variablePrefix}_access = ${variablePrefix}_register(${variablePrefix}_initModuleExports, ${asyncDeps});\n`);
|
|
138
138
|
}
|
|
139
139
|
function transformAndMoveDeclarationsToModuleScope(s, ast, asyncImports, variablePrefix) {
|
|
140
140
|
let moduleScopeEnd = 0;
|
|
141
|
-
let
|
|
141
|
+
let importDeclarationIndex = 0;
|
|
142
|
+
let inDirectivePrologue = true;
|
|
142
143
|
for (const node of ast.body) {
|
|
144
|
+
if (inDirectivePrologue && node.type === "ExpressionStatement" && node.expression.type === "Literal") {
|
|
145
|
+
moduleScopeEnd = node.end;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
inDirectivePrologue = false;
|
|
143
149
|
if (asyncImports.includes(node)) {
|
|
144
|
-
const tlaImport = `\nimport { ${variablePrefix}_access as ${variablePrefix}${
|
|
150
|
+
const tlaImport = `\nimport { ${variablePrefix}_access as ${variablePrefix}${importDeclarationIndex}} from '${node.source.value}';`;
|
|
145
151
|
s.appendLeft(node.end, tlaImport);
|
|
146
|
-
|
|
152
|
+
importDeclarationIndex++;
|
|
147
153
|
}
|
|
148
154
|
if (node.type === "ClassDeclaration") {
|
|
149
155
|
s.appendLeft(moduleScopeEnd, `let ${node.id.name};\n`);
|
|
@@ -186,7 +192,8 @@ function isFunctionDeclaration(type) {
|
|
|
186
192
|
return type === "FunctionDeclaration";
|
|
187
193
|
}
|
|
188
194
|
function getClassDeclarationStart(node) {
|
|
189
|
-
|
|
195
|
+
if (!("decorators" in node)) return node.start;
|
|
196
|
+
return node.decorators[0]?.start ?? node.start;
|
|
190
197
|
}
|
|
191
198
|
function moveVariableDeclarationToModuleScope(s, node, declarationsEnd) {
|
|
192
199
|
const kind = replaceConstWithLet(node.kind);
|
|
@@ -228,6 +235,16 @@ function getNames(pattern) {
|
|
|
228
235
|
}
|
|
229
236
|
}
|
|
230
237
|
|
|
238
|
+
//#endregion
|
|
239
|
+
//#region ../shared/src/registerSource.ts
|
|
240
|
+
var registerSource_default = {
|
|
241
|
+
"code": "export default function register(fn, evaluate_accesses) {\n let state = \"ready\";\n let whenDones = [];\n let whenErrors = [];\n let remaining = 1;\n let error = undefined;\n // evaluate eagerly\n evaluate();\n return evaluate;\n function evaluate(whenDone, onError) {\n if (state === \"done\") {\n if (whenDone) whenDone();\n return;\n }\n if (state === \"failed\") {\n if (onError) onError(error);\n return;\n }\n if (whenDone) whenDones.push(whenDone);\n if (onError) whenErrors.push(onError);\n if (state === \"busy\") {\n return;\n }\n state = \"busy\";\n let moduleDone = () => {\n state = \"done\";\n for (let x of whenDones) x();\n };\n let moduleError = (err) => {\n state = \"failed\";\n error = err;\n for (let x of whenErrors) x(err);\n };\n let importDone = () => {\n if (state !== \"busy\") return;\n if (--remaining !== 0) return;\n try {\n let result = fn();\n if (result) {\n result.then(moduleDone).catch(moduleError);\n } else {\n moduleDone();\n }\n } catch (err) {\n moduleError(err);\n }\n };\n for (const access of evaluate_accesses) {\n let evaluate;\n try {\n // Environment-dependent behavior:\n // - throws on cyclic dependencies in V8\n // - returns undefined in some environments (e.g., vitest)\n evaluate = access();\n if (evaluate == null) continue;\n } catch {\n continue;\n }\n remaining++;\n evaluate(importDone, moduleError);\n }\n importDone();\n }\n}\n",
|
|
242
|
+
"helpersUsed": {},
|
|
243
|
+
"errors": [],
|
|
244
|
+
"warnings": [],
|
|
245
|
+
"tsconfigFilePaths": []
|
|
246
|
+
}.code;
|
|
247
|
+
|
|
231
248
|
//#endregion
|
|
232
249
|
//#region src/index.ts
|
|
233
250
|
function resolveDeclarationSource(context, id, importerAttributes = {}, declaration) {
|
|
@@ -237,87 +254,6 @@ function resolveDeclarationSource(context, id, importerAttributes = {}, declarat
|
|
|
237
254
|
custom: {}
|
|
238
255
|
});
|
|
239
256
|
}
|
|
240
|
-
const tlaModule = `export default function register(fn, evaluate_accesses) {
|
|
241
|
-
let state = "ready";
|
|
242
|
-
let whenDones = [];
|
|
243
|
-
let whenErrors = [];
|
|
244
|
-
let remaining = 1;
|
|
245
|
-
let error = undefined;
|
|
246
|
-
// evaluate eagerly
|
|
247
|
-
evaluate();
|
|
248
|
-
|
|
249
|
-
return evaluate;
|
|
250
|
-
|
|
251
|
-
function evaluate(whenDone, onError) {
|
|
252
|
-
if (state === "done") {
|
|
253
|
-
if (whenDone)
|
|
254
|
-
whenDone();
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
if (state === "failed") {
|
|
258
|
-
if (onError)
|
|
259
|
-
onError(error);
|
|
260
|
-
return;
|
|
261
|
-
}
|
|
262
|
-
if (whenDone)
|
|
263
|
-
whenDones.push(whenDone);
|
|
264
|
-
if (onError)
|
|
265
|
-
whenErrors.push(onError);
|
|
266
|
-
if (state === "busy") {
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
state = "busy";
|
|
270
|
-
let moduleDone = () => {
|
|
271
|
-
state = "done";
|
|
272
|
-
for (let x of whenDones)
|
|
273
|
-
x();
|
|
274
|
-
};
|
|
275
|
-
let moduleError = (err) => {
|
|
276
|
-
state = "failed";
|
|
277
|
-
error = err;
|
|
278
|
-
for (let x of whenErrors)
|
|
279
|
-
x(err);
|
|
280
|
-
};
|
|
281
|
-
let importDone = () => {
|
|
282
|
-
if (state !== "busy")
|
|
283
|
-
return;
|
|
284
|
-
if (--remaining !== 0)
|
|
285
|
-
return;
|
|
286
|
-
try {
|
|
287
|
-
let result = fn();
|
|
288
|
-
if (result) {
|
|
289
|
-
result
|
|
290
|
-
.then(moduleDone)
|
|
291
|
-
.catch(moduleError);
|
|
292
|
-
}
|
|
293
|
-
else {
|
|
294
|
-
moduleDone();
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
catch (err) {
|
|
298
|
-
moduleError(err);
|
|
299
|
-
}
|
|
300
|
-
};
|
|
301
|
-
for (const access of evaluate_accesses) {
|
|
302
|
-
let evaluate;
|
|
303
|
-
try {
|
|
304
|
-
// Environment-dependent behavior:
|
|
305
|
-
// - throws on cyclic dependencies in V8
|
|
306
|
-
// - returns undefined in some environments (e.g., vitest)
|
|
307
|
-
evaluate = access();
|
|
308
|
-
if (evaluate == null)
|
|
309
|
-
continue;
|
|
310
|
-
}
|
|
311
|
-
catch (_a) {
|
|
312
|
-
continue;
|
|
313
|
-
}
|
|
314
|
-
remaining++;
|
|
315
|
-
evaluate(importDone, moduleError);
|
|
316
|
-
}
|
|
317
|
-
importDone();
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
`;
|
|
321
257
|
function concurrentTopLevelAwait(options = {}) {
|
|
322
258
|
const filter = createFilter(options.include, options.exclude);
|
|
323
259
|
const generatedVariablePrefix = options.generatedVariablePrefix ?? "__tla";
|
|
@@ -330,7 +266,7 @@ function concurrentTopLevelAwait(options = {}) {
|
|
|
330
266
|
if (source === registerModuleSource) return registerModuleSource;
|
|
331
267
|
},
|
|
332
268
|
load(id) {
|
|
333
|
-
if (id === registerModuleSource) return
|
|
269
|
+
if (id === registerModuleSource) return registerSource_default;
|
|
334
270
|
},
|
|
335
271
|
transform: { async handler(code, id, transformOptions) {
|
|
336
272
|
if (!filter(id)) return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rollup-plugin-concurrent-top-level-await",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Rollup (and Vite) plugin enabling concurrent execution of modules that contain top level await.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"rollup-plugin",
|
|
@@ -58,7 +58,8 @@
|
|
|
58
58
|
"devDependencies": {
|
|
59
59
|
"@types/estree": "^1.0.8",
|
|
60
60
|
"prettier": "3.7.4",
|
|
61
|
-
"rollup": "^4.57.1"
|
|
61
|
+
"rollup": "^4.57.1",
|
|
62
|
+
"unplugin-macros": "^0.19.1"
|
|
62
63
|
},
|
|
63
64
|
"scripts": {
|
|
64
65
|
"build": "tsdown --dts"
|