rolldown-plugin-concurrent-top-level-await 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -44,11 +44,11 @@ export default defineConfig({
44
44
 
45
45
  ## Options
46
46
 
47
- | Option | Type | Default | Description |
48
- | ------------------------- | -------- | --------- | ------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
49
- | `include` | `RegExp | RegExp[]` | `undefined` | A RegExp specifying which files to include. See [below](#which-modules-to-include) to determine which modules to include. |
50
- | `exclude` | `RegExp | RegExp[]` | `[/\/node_modules\//, /\.html$/]` | A RegExp specifying which files to exclude. Must still follow the [same considerations](#which-modules-to-include) as `include`. |
51
- | `generatedVariablePrefix` | `string` | `"__tla"` | Prefix used for internal variables generated by the plugin. Change this if it conflicts with variable names in your code. |
47
+ | Option | Type | Default | Description |
48
+ | ------------------------- | ------------------------------- | --------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
49
+ | `include` | <code>RegExp \| RegExp[]</code> | `undefined` | A RegExp specifying which files to include. See [below](#which-modules-to-include) to determine which modules to include. |
50
+ | `exclude` | <code>RegExp \| RegExp[]</code> | `[/\/node_modules\//, /\.html$/]` | A RegExp specifying which files to exclude. Must still follow the [same considerations](#which-modules-to-include) as `include`. |
51
+ | `generatedVariablePrefix` | `string` | `"__tla"` | Prefix used for internal variables generated by the plugin. Change this if it conflicts with variable names in your code. |
52
52
 
53
53
  ### Which modules to include?
54
54
 
package/dist/index.mjs CHANGED
@@ -83,88 +83,17 @@ export default ${generatedVariablePrefix}_default;
83
83
  }
84
84
 
85
85
  //#endregion
86
- //#region src/registerModulePlugin.ts
87
- const tlaModule = `export default function register(fn, evaluate_accesses) {
88
- let state = "ready";
89
- let whenDones = [];
90
- let whenErrors = [];
91
- let remaining = 1;
92
- let error = undefined;
93
- // evaluate eagerly
94
- evaluate();
95
-
96
- return evaluate;
86
+ //#region ../shared/src/registerSource.ts
87
+ var registerSource_default = {
88
+ "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",
89
+ "helpersUsed": {},
90
+ "errors": [],
91
+ "warnings": [],
92
+ "tsconfigFilePaths": []
93
+ }.code;
97
94
 
98
- function evaluate(whenDone, onError) {
99
- if (state === "done") {
100
- if (whenDone)
101
- whenDone();
102
- return;
103
- }
104
- if (state === "failed") {
105
- if (onError)
106
- onError(error);
107
- return;
108
- }
109
- if (whenDone)
110
- whenDones.push(whenDone);
111
- if (onError)
112
- whenErrors.push(onError);
113
- if (state === "busy") {
114
- return;
115
- }
116
- state = "busy";
117
- let moduleDone = () => {
118
- state = "done";
119
- for (let x of whenDones)
120
- x();
121
- };
122
- let moduleError = (err) => {
123
- state = "failed";
124
- error = err;
125
- for (let x of whenErrors)
126
- x(err);
127
- };
128
- let importDone = () => {
129
- if (state !== "busy")
130
- return;
131
- if (--remaining !== 0)
132
- return;
133
- try {
134
- let result = fn();
135
- if (result) {
136
- result
137
- .then(moduleDone)
138
- .catch(moduleError);
139
- }
140
- else {
141
- moduleDone();
142
- }
143
- }
144
- catch (err) {
145
- moduleError(err);
146
- }
147
- };
148
- for (const access of evaluate_accesses) {
149
- let evaluate;
150
- try {
151
- // Environment-dependent behavior:
152
- // - throws on cyclic dependencies in V8
153
- // - returns undefined in some environments (e.g., vitest)
154
- evaluate = access();
155
- if (evaluate == null)
156
- continue;
157
- }
158
- catch (_a) {
159
- continue;
160
- }
161
- remaining++;
162
- evaluate(importDone, moduleError);
163
- }
164
- importDone();
165
- }
166
- }
167
- `;
95
+ //#endregion
96
+ //#region src/registerModulePlugin.ts
168
97
  function registerModulePlugin(options) {
169
98
  options.generatedVariablePrefix;
170
99
  const registerModuleSource = options.registerModuleSource;
@@ -175,7 +104,7 @@ function registerModulePlugin(options) {
175
104
  if (source === registerModuleSource) return registerModuleSource;
176
105
  },
177
106
  load(id) {
178
- if (id === registerModuleSource) return tlaModule;
107
+ if (id === registerModuleSource) return registerSource_default;
179
108
  }
180
109
  };
181
110
  }
@@ -312,18 +241,24 @@ function transform$1(s, ast, registerModuleSource, asyncImports, hasAwait, varia
312
241
  const declarationsEnd = transformAndMoveDeclarationsToModuleScope(s, ast, asyncImports, variablePrefix);
313
242
  s.appendRight(declarationsEnd, `${hasAwait ? "async " : ""}function ${variablePrefix}_initModuleExports() {\n`);
314
243
  s.append("\n}\n");
315
- s.prepend(`import ${variablePrefix}_register from ${JSON.stringify(registerModuleSource)};\n`);
244
+ s.append(`import ${variablePrefix}_register from ${JSON.stringify(registerModuleSource)};\n`);
316
245
  const asyncDeps = `[${asyncImports.map((_, i) => `() => ${variablePrefix}${i}`).join(", ")}]`;
317
246
  s.append(`export const ${variablePrefix}_access = ${variablePrefix}_register(${variablePrefix}_initModuleExports, ${asyncDeps});\n`);
318
247
  }
319
248
  function transformAndMoveDeclarationsToModuleScope(s, ast, asyncImports, variablePrefix) {
320
249
  let moduleScopeEnd = 0;
321
- let i = 0;
250
+ let importDeclarationIndex = 0;
251
+ let inDirectivePrologue = true;
322
252
  for (const node of ast.body) {
253
+ if (inDirectivePrologue && node.type === "ExpressionStatement" && node.expression.type === "Literal") {
254
+ moduleScopeEnd = node.end;
255
+ continue;
256
+ }
257
+ inDirectivePrologue = false;
323
258
  if (asyncImports.includes(node)) {
324
- const tlaImport = `\nimport { ${variablePrefix}_access as ${variablePrefix}${i}} from '${node.source.value}';`;
259
+ const tlaImport = `\nimport { ${variablePrefix}_access as ${variablePrefix}${importDeclarationIndex}} from '${node.source.value}';`;
325
260
  s.appendLeft(node.end, tlaImport);
326
- i++;
261
+ importDeclarationIndex++;
327
262
  }
328
263
  if (node.type === "ClassDeclaration") {
329
264
  s.appendLeft(moduleScopeEnd, `let ${node.id.name};\n`);
@@ -366,7 +301,8 @@ function isFunctionDeclaration(type) {
366
301
  return type === "FunctionDeclaration";
367
302
  }
368
303
  function getClassDeclarationStart(node) {
369
- return node.decorators?.[0]?.start ?? node.start;
304
+ if (!("decorators" in node)) return node.start;
305
+ return node.decorators[0]?.start ?? node.start;
370
306
  }
371
307
  function moveVariableDeclarationToModuleScope(s, node, declarationsEnd) {
372
308
  const kind = replaceConstWithLet(node.kind);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rolldown-plugin-concurrent-top-level-await",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Rolldown (and Vite) plugin enabling concurrent execution of modules that contain top level await.",
5
5
  "keywords": [
6
6
  "rolldown-plugin",
@@ -55,7 +55,8 @@
55
55
  "@oxc-project/types": "^0.122.0",
56
56
  "@types/estree": "^1.0.8",
57
57
  "prettier": "3.7.4",
58
- "rolldown": "1.0.0-rc.9"
58
+ "rolldown": "1.0.0-rc.9",
59
+ "unplugin-macros": "^0.19.1"
59
60
  },
60
61
  "scripts": {
61
62
  "build": "tsdown --dts"