rollup-plugin-concurrent-top-level-await 0.1.0 → 0.2.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
@@ -99,6 +99,14 @@ postponed until the subgraph is analyzed. This may lead to slower builds.
99
99
 
100
100
  If you notice significant performance degradation, please open an issue.
101
101
 
102
+ ### Exposed Module Structure
103
+
104
+ Because the execution of modules gets wrapped in functions, the bundled output will contain more information about the source module structure. This may be a consideration for projects where code obfuscation is important.
105
+
106
+ ### Tree Shaking
107
+
108
+ Wrapping code in functions may reduce tree shaking effectiveness. We mitigate this where possible, such as by not wrapping declarations.
109
+
102
110
  ### Changing Variable Types
103
111
 
104
112
  In the process of transforming the code, top level `const` declarations may get replaced with `let` declarations. This
@@ -106,7 +114,6 @@ can lead to `const` variables being assignable at runtime instead of throwing an
106
114
 
107
115
  Additionally, variable declarations may be hoisted, which removes temporal dead zone (TDZ) checks.
108
116
 
109
- ### Class Decorators
117
+ ### Default export class name
110
118
 
111
- Class declarations still get evaluated before any top level await expressions. This means that if a class decorator
112
- relies on a top level await expression, it may not work as expected.
119
+ When using `export default class {}`, the runtime `.name` of the exported value will be `<generatedVariablePrefix>_default` (e.g. `__tla_default`) instead of `default`.
package/dist/index.mjs CHANGED
@@ -129,7 +129,7 @@ var AsyncModuleTracker = class {
129
129
  //#endregion
130
130
  //#region src/transform.ts
131
131
  function transform(s, ast, asyncImports, hasAwait, variablePrefix) {
132
- const declarationsEnd = tansformAndMoveDeclarationsToModuleScope(s, ast, asyncImports, variablePrefix);
132
+ const declarationsEnd = transformAndMoveDeclarationsToModuleScope(s, ast, asyncImports, variablePrefix);
133
133
  s.appendRight(declarationsEnd, `async function ${variablePrefix}_initModuleExports() {\n`);
134
134
  s.append("\n}\n");
135
135
  const asyncDeps = `[${asyncImports.map((_, i) => `${variablePrefix}${i}`).join()}].flatMap(a => {
@@ -149,7 +149,7 @@ function transform(s, ast, asyncImports, hasAwait, variablePrefix) {
149
149
  s.append(`if (import.meta.useTla) await ${variablePrefix}_initPromise;\n`);
150
150
  s.append(`export function ${variablePrefix}_access() { return ${variablePrefix}; };\n`);
151
151
  }
152
- function tansformAndMoveDeclarationsToModuleScope(s, ast, asyncImports, variablePrefix) {
152
+ function transformAndMoveDeclarationsToModuleScope(s, ast, asyncImports, variablePrefix) {
153
153
  let moduleScopeEnd = 0;
154
154
  let i = 0;
155
155
  for (const node of ast.body) {
@@ -158,11 +158,20 @@ function tansformAndMoveDeclarationsToModuleScope(s, ast, asyncImports, variable
158
158
  s.appendLeft(node.end, tlaImport);
159
159
  i++;
160
160
  }
161
+ if (node.type === "ClassDeclaration") {
162
+ s.appendLeft(moduleScopeEnd, `let ${node.id.name};\n`);
163
+ s.appendRight(node.start, `${node.id.name} = `);
164
+ }
161
165
  if (node.type === "ExportNamedDeclaration") {
162
166
  if (node.declaration?.type === "VariableDeclaration") {
163
167
  s.appendLeft(moduleScopeEnd, "export ");
164
168
  s.remove(node.start, node.declaration.start);
165
169
  moveVariableDeclarationToModuleScope(s, node.declaration, moduleScopeEnd);
170
+ } else if (node.declaration?.type === "ClassDeclaration") {
171
+ s.appendLeft(moduleScopeEnd, `export let ${node.declaration.id.name};\n`);
172
+ const declarationStart = getClassDeclarationStart(node.declaration);
173
+ s.remove(node.start, declarationStart);
174
+ s.appendRight(declarationStart, `${node.declaration.id.name} = `);
166
175
  }
167
176
  } else if (node.type === "VariableDeclaration") if (node.kind.endsWith("using")) moveVariableDeclarationWithUsingToModuleScope(s, node, moduleScopeEnd, variablePrefix);
168
177
  else moveVariableDeclarationToModuleScope(s, node, moduleScopeEnd);
@@ -170,13 +179,15 @@ function tansformAndMoveDeclarationsToModuleScope(s, ast, asyncImports, variable
170
179
  if (n.type === "VariableDeclaration" && n.kind === "var") moveVariableDeclarationToModuleScope(s, n, moduleScopeEnd);
171
180
  return false;
172
181
  });
173
- if (node.type === "ExportDefaultDeclaration" && !isDeclaration(node.declaration.type)) {
174
- s.appendLeft(moduleScopeEnd, `let ${variablePrefix}_default;\nexport { ${variablePrefix}_default as default };\n`);
175
- s.remove(node.start, node.declaration.start);
176
- s.appendRight(node.declaration.start, `${variablePrefix}_default = (`);
182
+ if (node.type === "ExportDefaultDeclaration" && !isFunctionDeclaration(node.declaration.type)) {
183
+ const variableName = node.declaration.type === "ClassDeclaration" ? node.declaration.id?.name ?? `${variablePrefix}_default` : `${variablePrefix}_default`;
184
+ s.appendLeft(moduleScopeEnd, `let ${variableName};\nexport { ${variableName} as default };\n`);
185
+ const declarationStart = node.declaration.type === "ClassDeclaration" ? getClassDeclarationStart(node.declaration) : node.declaration.start;
186
+ s.remove(node.start, declarationStart);
187
+ s.appendRight(declarationStart, `${variableName} = (`);
177
188
  s.appendLeft(node.declaration.end, ");");
178
189
  }
179
- if (isDeclaration(node.type) || node.type === "ImportDeclaration" || node.type === "ExportDefaultDeclaration" && isDeclaration(node.declaration.type) || node.type === "ExportNamedDeclaration" && isDeclaration(node.declaration?.type) || node.type === "ExportNamedDeclaration" && node.declaration == null || node.type === "ExportAllDeclaration") if (node.start > moduleScopeEnd) {
190
+ 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) {
180
191
  s.appendLeft(node.end, "\n");
181
192
  s.move(node.start, node.end, moduleScopeEnd);
182
193
  s.appendLeft(node.start, ";");
@@ -184,8 +195,11 @@ function tansformAndMoveDeclarationsToModuleScope(s, ast, asyncImports, variable
184
195
  }
185
196
  return moduleScopeEnd;
186
197
  }
187
- function isDeclaration(type) {
188
- return type === "ClassDeclaration" || type === "FunctionDeclaration";
198
+ function isFunctionDeclaration(type) {
199
+ return type === "FunctionDeclaration";
200
+ }
201
+ function getClassDeclarationStart(node) {
202
+ return node.decorators[0]?.start ?? node.start;
189
203
  }
190
204
  function moveVariableDeclarationToModuleScope(s, node, declarationsEnd) {
191
205
  const kind = replaceConstWithLet(node.kind);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rollup-plugin-concurrent-top-level-await",
3
- "version": "0.1.0",
3
+ "version": "0.2.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",