rollup-plugin-concurrent-top-level-await 0.0.6 → 0.0.7

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 CHANGED
@@ -24,12 +24,46 @@ import concurrentTopLevelAwait from "rollup-plugin-concurrent-top-level-await";
24
24
  export default {
25
25
  plugins: [
26
26
  concurrentTopLevelAwait({
27
- include: "**/*.ts",
27
+ include: "**/*.js",
28
28
  }),
29
29
  ],
30
30
  };
31
31
  ```
32
32
 
33
+ ### Which modules to include
34
+
35
+ The plugin needs to handle not only modules that directly contain a top-level `await`, but also their ancestor modules up to the lowest common ancestor. Ancestor modules must be transformed to handle the asynchronous completion of their children concurrently. As an example, consider the following module structure:
36
+
37
+ ```mermaid
38
+ flowchart LR
39
+ app[app.js]
40
+ moduleA[moduleA.js]
41
+ moduleB[moduleB.js]
42
+ moduleC[moduleC.js]
43
+ tla1[tla1.js]
44
+ tla2[tla2.js]
45
+ tla3[tla3.js]
46
+ other[other.js]
47
+
48
+ app --> moduleA
49
+ moduleA --> moduleB
50
+ moduleA --> moduleC
51
+ moduleB --> tla1
52
+ moduleC --> tla2
53
+ moduleC --> tla3
54
+ app --> other
55
+
56
+ classDef tla fill:#ffe6e6,stroke:#ff0000,color:#660000
57
+ classDef ancestor fill:#fff4cc,stroke:#ffcc00,color:#663300
58
+ classDef unaffected fill:#e6ffe6,stroke:#00cc00,color:#006600
59
+
60
+ class tla1,tla2,tla3 tla
61
+ class moduleA,moduleB,moduleC ancestor
62
+ class app,other unaffected
63
+ ```
64
+
65
+ If the red modules contain top level awaits, these and their yellow ancestors should be included in the plugin's `include` option.
66
+
33
67
  ## Known Limitations
34
68
 
35
69
  ### Execution Order
package/dist/index.mjs CHANGED
@@ -159,10 +159,11 @@ function tansformAndMoveDeclarationsToModuleScope(s, ast, asyncImports) {
159
159
  if (node.declaration?.type === "VariableDeclaration") {
160
160
  s = s.appendLeft(moduleScopeEnd, ";export ");
161
161
  s = s.remove(node.start, node.declaration.start);
162
- s = moveVarDeclarationToModuleScope(s, node.declaration, moduleScopeEnd);
162
+ s = moveVariableDeclarationToModuleScope(s, node.declaration, moduleScopeEnd);
163
163
  }
164
164
  }
165
- if (node.type === "VariableDeclaration") s = moveVarDeclarationToModuleScope(s, node, moduleScopeEnd);
165
+ if (node.type === "VariableDeclaration") if (node.kind.endsWith("using")) s = moveVariableDeclarationWithUsingToModuleScope(s, node, moduleScopeEnd);
166
+ else s = moveVariableDeclarationToModuleScope(s, node, moduleScopeEnd);
166
167
  if (isDeclaration(node.type) || node.type === "ImportDeclaration" || node.type === "ExportNamedDeclaration" && isDeclaration(node.declaration?.type) || node.type === "ExportNamedDeclaration" && node.declaration == null || node.type === "ExportDefaultDeclaration" || node.type === "ExportAllDeclaration") if (node.start > moduleScopeEnd) {
167
168
  s = s.appendRight(node.start, ";\n");
168
169
  s = s.move(node.start, node.end, moduleScopeEnd);
@@ -173,7 +174,7 @@ function tansformAndMoveDeclarationsToModuleScope(s, ast, asyncImports) {
173
174
  function isDeclaration(type) {
174
175
  return type === "ClassDeclaration" || type === "FunctionDeclaration";
175
176
  }
176
- function moveVarDeclarationToModuleScope(s, node, declarationsEnd) {
177
+ function moveVariableDeclarationToModuleScope(s, node, declarationsEnd) {
177
178
  const kind = replaceConstWithLet(node.kind);
178
179
  const names = node.declarations.flatMap((decl) => getNames(decl.id)).join(", ");
179
180
  s = s.appendRight(node.declarations[0].start, ";(");
@@ -182,6 +183,17 @@ function moveVarDeclarationToModuleScope(s, node, declarationsEnd) {
182
183
  s = s.remove(node.start, node.declarations[0].start);
183
184
  return s;
184
185
  }
186
+ function moveVariableDeclarationWithUsingToModuleScope(s, node, declarationsEnd) {
187
+ node.declarations.forEach((declaration) => {
188
+ const id = declaration.id;
189
+ if (id.type !== "Identifier") throw new Error("'using' declarations may not have binding patterns.");
190
+ const name = id.name;
191
+ s = s.appendRight(id.start, `__tla_using_`);
192
+ s = s.appendLeft(node.end, `;\n${name} = __tla_using_${name};`);
193
+ s = s.appendLeft(declarationsEnd, `\nlet ${name};\n`);
194
+ });
195
+ return s;
196
+ }
185
197
  function replaceConstWithLet(value) {
186
198
  if (value === "const") return "let";
187
199
  return value;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rollup-plugin-concurrent-top-level-await",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "Rollup (and Vite) plugin enabling concurrent execution of modules that contain top level await.",
5
5
  "keywords": [
6
6
  "rollup-plugin",