dismantle 0.2.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.
Files changed (108) hide show
  1. package/dist/cjs/development/index.cjs +996 -487
  2. package/dist/cjs/development/index.cjs.map +4 -4
  3. package/dist/cjs/development/runtime.cjs +33 -1464
  4. package/dist/cjs/development/runtime.cjs.map +4 -4
  5. package/dist/cjs/production/index.cjs +1 -1
  6. package/dist/cjs/production/runtime.cjs +1 -1
  7. package/dist/esm/development/index.mjs +996 -487
  8. package/dist/esm/development/index.mjs.map +4 -4
  9. package/dist/esm/development/runtime.mjs +30 -1451
  10. package/dist/esm/development/runtime.mjs.map +4 -4
  11. package/dist/esm/production/index.mjs +1 -1
  12. package/dist/esm/production/runtime.mjs +1 -1
  13. package/dist/types/runtime/index.d.ts +10 -0
  14. package/dist/types/runtime/index.d.ts.map +1 -0
  15. package/dist/types/src/hidden-imports.d.ts.map +1 -0
  16. package/dist/types/{index.d.ts → src/index.d.ts} +1 -1
  17. package/dist/types/src/index.d.ts.map +1 -0
  18. package/dist/types/src/plugin.d.ts.map +1 -0
  19. package/dist/types/src/split-block-directive.d.ts +5 -0
  20. package/dist/types/src/split-block-directive.d.ts.map +1 -0
  21. package/dist/types/src/split-call.d.ts +6 -0
  22. package/dist/types/src/split-call.d.ts.map +1 -0
  23. package/dist/types/src/split-function-directive.d.ts +5 -0
  24. package/dist/types/src/split-function-directive.d.ts.map +1 -0
  25. package/dist/types/src/split.d.ts +26 -0
  26. package/dist/types/src/split.d.ts.map +1 -0
  27. package/dist/types/src/transform-block-directive.d.ts +5 -0
  28. package/dist/types/src/transform-block-directive.d.ts.map +1 -0
  29. package/dist/types/src/transform-call.d.ts.map +1 -0
  30. package/dist/types/src/transform-function-directive.d.ts +5 -0
  31. package/dist/types/src/transform-function-directive.d.ts.map +1 -0
  32. package/dist/types/{types.d.ts → src/types.d.ts} +25 -9
  33. package/dist/types/src/types.d.ts.map +1 -0
  34. package/dist/types/src/utils/assert.d.ts.map +1 -0
  35. package/dist/types/src/utils/bubble-function-declaration.d.ts +5 -0
  36. package/dist/types/src/utils/bubble-function-declaration.d.ts.map +1 -0
  37. package/dist/types/src/utils/directive-check.d.ts +7 -0
  38. package/dist/types/src/utils/directive-check.d.ts.map +1 -0
  39. package/dist/types/src/utils/generate-unique-name.d.ts +4 -0
  40. package/dist/types/src/utils/generate-unique-name.d.ts.map +1 -0
  41. package/dist/types/src/utils/generator-shim.d.ts.map +1 -0
  42. package/dist/types/src/utils/get-descriptive-name.d.ts.map +1 -0
  43. package/dist/types/src/utils/get-foreign-bindings.d.ts.map +1 -0
  44. package/dist/types/src/utils/get-identifiers-from-lval.d.ts.map +1 -0
  45. package/dist/types/src/utils/get-import-identifier.d.ts.map +1 -0
  46. package/dist/types/src/utils/get-import-specifier-name.d.ts +3 -0
  47. package/dist/types/src/utils/get-import-specifier-name.d.ts.map +1 -0
  48. package/dist/types/src/utils/get-module-definition.d.ts.map +1 -0
  49. package/dist/types/src/utils/get-root-statement-path.d.ts.map +1 -0
  50. package/dist/types/src/utils/is-statement-top-level.d.ts +4 -0
  51. package/dist/types/src/utils/is-statement-top-level.d.ts.map +1 -0
  52. package/dist/types/src/utils/register-import-specifiers.d.ts.map +1 -0
  53. package/dist/types/src/utils/unwrap.d.ts.map +1 -0
  54. package/dist/types/src/utils/xxhash32.d.ts.map +1 -0
  55. package/package.json +10 -12
  56. package/src/index.ts +16 -2
  57. package/src/plugin.ts +17 -4
  58. package/src/split-block-directive.ts +444 -0
  59. package/src/split-call.ts +410 -0
  60. package/src/split-function-directive.ts +349 -0
  61. package/src/split.ts +23 -728
  62. package/src/transform-block-directive.ts +22 -0
  63. package/src/transform-call.ts +14 -16
  64. package/src/transform-function-directive.ts +32 -0
  65. package/src/types.ts +33 -9
  66. package/src/utils/bubble-function-declaration.ts +50 -0
  67. package/src/{transform-block.ts → utils/directive-check.ts} +21 -29
  68. package/src/utils/generate-unique-name.ts +25 -0
  69. package/src/utils/get-import-identifier.ts +14 -16
  70. package/src/utils/get-import-specifier-name.ts +4 -6
  71. package/src/utils/is-statement-top-level.ts +15 -0
  72. package/src/utils/register-import-specifiers.ts +9 -6
  73. package/dist/types/hidden-imports.d.ts.map +0 -1
  74. package/dist/types/index.d.ts.map +0 -1
  75. package/dist/types/plugin.d.ts.map +0 -1
  76. package/dist/types/split.d.ts +0 -7
  77. package/dist/types/split.d.ts.map +0 -1
  78. package/dist/types/transform-block.d.ts +0 -5
  79. package/dist/types/transform-block.d.ts.map +0 -1
  80. package/dist/types/transform-call.d.ts.map +0 -1
  81. package/dist/types/types.d.ts.map +0 -1
  82. package/dist/types/utils/assert.d.ts.map +0 -1
  83. package/dist/types/utils/generator-shim.d.ts.map +0 -1
  84. package/dist/types/utils/get-descriptive-name.d.ts.map +0 -1
  85. package/dist/types/utils/get-foreign-bindings.d.ts.map +0 -1
  86. package/dist/types/utils/get-identifiers-from-lval.d.ts.map +0 -1
  87. package/dist/types/utils/get-import-identifier.d.ts.map +0 -1
  88. package/dist/types/utils/get-import-specifier-name.d.ts +0 -3
  89. package/dist/types/utils/get-import-specifier-name.d.ts.map +0 -1
  90. package/dist/types/utils/get-module-definition.d.ts.map +0 -1
  91. package/dist/types/utils/get-root-statement-path.d.ts.map +0 -1
  92. package/dist/types/utils/register-import-specifiers.d.ts.map +0 -1
  93. package/dist/types/utils/unwrap.d.ts.map +0 -1
  94. package/dist/types/utils/xxhash32.d.ts.map +0 -1
  95. /package/dist/types/{hidden-imports.d.ts → src/hidden-imports.d.ts} +0 -0
  96. /package/dist/types/{plugin.d.ts → src/plugin.d.ts} +0 -0
  97. /package/dist/types/{transform-call.d.ts → src/transform-call.d.ts} +0 -0
  98. /package/dist/types/{utils → src/utils}/assert.d.ts +0 -0
  99. /package/dist/types/{utils → src/utils}/generator-shim.d.ts +0 -0
  100. /package/dist/types/{utils → src/utils}/get-descriptive-name.d.ts +0 -0
  101. /package/dist/types/{utils → src/utils}/get-foreign-bindings.d.ts +0 -0
  102. /package/dist/types/{utils → src/utils}/get-identifiers-from-lval.d.ts +0 -0
  103. /package/dist/types/{utils → src/utils}/get-import-identifier.d.ts +0 -0
  104. /package/dist/types/{utils → src/utils}/get-module-definition.d.ts +0 -0
  105. /package/dist/types/{utils → src/utils}/get-root-statement-path.d.ts +0 -0
  106. /package/dist/types/{utils → src/utils}/register-import-specifiers.d.ts +0 -0
  107. /package/dist/types/{utils → src/utils}/unwrap.d.ts +0 -0
  108. /package/dist/types/{utils → src/utils}/xxhash32.d.ts +0 -0
@@ -36,8 +36,8 @@ module.exports = __toCommonJS(src_exports);
36
36
  var babel = __toESM(require("@babel/core"), 1);
37
37
  var import_node_path = __toESM(require("node:path"), 1);
38
38
 
39
- // src/split.ts
40
- var t6 = __toESM(require("@babel/types"), 1);
39
+ // src/split-block-directive.ts
40
+ var t9 = __toESM(require("@babel/types"), 1);
41
41
 
42
42
  // src/hidden-imports.ts
43
43
  var HIDDEN_FUNC = {
@@ -51,11 +51,22 @@ var HIDDEN_GENERATOR = {
51
51
  name: "$$gen"
52
52
  };
53
53
 
54
- // src/utils/assert.ts
55
- function assert(cond, error) {
56
- if (!cond) {
57
- throw new Error(error);
58
- }
54
+ // src/split.ts
55
+ var t7 = __toESM(require("@babel/types"), 1);
56
+
57
+ // src/utils/generate-unique-name.ts
58
+ var t = __toESM(require("@babel/types"), 1);
59
+ function generateUniqueName(path2, name) {
60
+ let uid;
61
+ let i = 1;
62
+ do {
63
+ uid = name + "_" + i;
64
+ i++;
65
+ } while (path2.scope.hasLabel(uid) || path2.scope.hasBinding(uid) || path2.scope.hasGlobal(uid) || path2.scope.hasReference(uid));
66
+ const program2 = path2.scope.getProgramParent();
67
+ program2.references[uid] = true;
68
+ program2.uids[uid] = true;
69
+ return t.identifier(uid);
59
70
  }
60
71
 
61
72
  // src/utils/generator-shim.ts
@@ -112,7 +123,7 @@ function getDescriptiveName(path2, defaultName) {
112
123
  }
113
124
 
114
125
  // src/utils/get-foreign-bindings.ts
115
- var t = __toESM(require("@babel/types"), 1);
126
+ var t2 = __toESM(require("@babel/types"), 1);
116
127
  function isForeignBinding(source, current, name, mode) {
117
128
  if (current.scope.hasGlobal(name)) {
118
129
  return false;
@@ -135,7 +146,7 @@ function isForeignBinding(source, current, name, mode) {
135
146
  function isInTypescript(path2) {
136
147
  let parent = path2.parentPath;
137
148
  while (parent) {
138
- if (t.isTypeScript(parent.node) && !t.isExpression(parent.node)) {
149
+ if (t2.isTypeScript(parent.node) && !t2.isExpression(parent.node)) {
139
150
  return true;
140
151
  }
141
152
  parent = parent.parentPath;
@@ -155,7 +166,7 @@ function getForeignBindings(path2, mode) {
155
166
  }
156
167
 
157
168
  // src/utils/get-identifiers-from-lval.ts
158
- var t2 = __toESM(require("@babel/types"), 1);
169
+ var t3 = __toESM(require("@babel/types"), 1);
159
170
  function getIdentifiersFromArrayPattern(node) {
160
171
  const ids = [];
161
172
  for (let i = 0, len = node.elements.length; i < len; i++) {
@@ -173,7 +184,7 @@ function getIdentifiersFromObjectPattern(node) {
173
184
  if (el) {
174
185
  if (el.type === "RestElement") {
175
186
  ids.push(...getIdentifiersFromLVal(el));
176
- } else if (t2.isLVal(el.value)) {
187
+ } else if (t3.isLVal(el.value)) {
177
188
  ids.push(...getIdentifiersFromLVal(el.value));
178
189
  }
179
190
  }
@@ -197,44 +208,16 @@ function getIdentifiersFromLVal(node) {
197
208
  }
198
209
  }
199
210
 
200
- // src/utils/get-import-identifier.ts
201
- var t3 = __toESM(require("@babel/types"), 1);
202
- function getImportIdentifier(state, path2, registration) {
203
- const name = registration.kind === "named" ? registration.name : "default";
204
- const target = `${registration.source}[${name}]`;
205
- const current = state.imports.get(target);
206
- if (current) {
207
- return current;
208
- }
209
- const programParent = path2.scope.getProgramParent();
210
- const uid = programParent.generateUidIdentifier(
211
- registration.kind === "named" ? registration.name : "default"
212
- );
213
- const newPath = programParent.path.unshiftContainer(
214
- "body",
215
- t3.importDeclaration(
216
- [
217
- registration.kind === "named" ? t3.importSpecifier(uid, t3.identifier(registration.name)) : t3.importDefaultSpecifier(uid)
218
- ],
219
- t3.stringLiteral(registration.source)
220
- )
221
- )[0];
222
- programParent.registerDeclaration(newPath);
223
- state.imports.set(target, uid);
224
- return uid;
225
- }
226
-
227
211
  // src/utils/get-module-definition.ts
228
- var t4 = __toESM(require("@babel/types"), 1);
212
+ var t5 = __toESM(require("@babel/types"), 1);
229
213
 
230
214
  // src/utils/get-import-specifier-name.ts
215
+ var t4 = __toESM(require("@babel/types"), 1);
231
216
  function getImportSpecifierName(node) {
232
- switch (node.imported.type) {
233
- case "Identifier":
234
- return node.imported.name;
235
- case "StringLiteral":
236
- return node.imported.value;
217
+ if (t4.isIdentifier(node.imported)) {
218
+ return node.imported.name;
237
219
  }
220
+ return node.imported.value;
238
221
  }
239
222
 
240
223
  // src/utils/unwrap.ts
@@ -276,7 +259,7 @@ function unwrapPath(path2, key) {
276
259
 
277
260
  // src/utils/get-module-definition.ts
278
261
  function getModuleDefinition(path2) {
279
- if (!(isPathValid(path2, t4.isImportSpecifier) || isPathValid(path2, t4.isImportDefaultSpecifier) || isPathValid(path2, t4.isImportNamespaceSpecifier))) {
262
+ if (!(isPathValid(path2, t5.isImportSpecifier) || isPathValid(path2, t5.isImportDefaultSpecifier) || isPathValid(path2, t5.isImportNamespaceSpecifier))) {
280
263
  return void 0;
281
264
  }
282
265
  const parent = path2.getStatementParent();
@@ -315,12 +298,12 @@ function getModuleDefinition(path2) {
315
298
  }
316
299
 
317
300
  // src/utils/get-root-statement-path.ts
318
- var t5 = __toESM(require("@babel/types"), 1);
301
+ var t6 = __toESM(require("@babel/types"), 1);
319
302
  function getRootStatementPath(path2) {
320
303
  let current = path2.parentPath;
321
304
  while (current) {
322
305
  const next = current.parentPath;
323
- if (next && t5.isProgram(next.node)) {
306
+ if (next && t6.isProgram(next.node)) {
324
307
  return current;
325
308
  }
326
309
  current = next;
@@ -332,20 +315,20 @@ function getRootStatementPath(path2) {
332
315
  function moduleDefinitionToImportSpecifier(definition) {
333
316
  switch (definition.kind) {
334
317
  case "default":
335
- return t6.importDefaultSpecifier(t6.identifier(definition.local));
318
+ return t7.importDefaultSpecifier(t7.identifier(definition.local));
336
319
  case "named":
337
- return t6.importSpecifier(
338
- t6.identifier(definition.local),
339
- definition.imported ? t6.identifier(definition.imported) : t6.identifier(definition.local)
320
+ return t7.importSpecifier(
321
+ t7.identifier(definition.local),
322
+ definition.imported ? t7.identifier(definition.imported) : t7.identifier(definition.local)
340
323
  );
341
324
  case "namespace":
342
- return t6.importNamespaceSpecifier(t6.identifier(definition.local));
325
+ return t7.importNamespaceSpecifier(t7.identifier(definition.local));
343
326
  }
344
327
  }
345
328
  function moduleDefinitionToImportDeclaration(definition) {
346
- return t6.importDeclaration(
329
+ return t7.importDeclaration(
347
330
  [moduleDefinitionToImportSpecifier(definition)],
348
- t6.stringLiteral(definition.source)
331
+ t7.stringLiteral(definition.source)
349
332
  );
350
333
  }
351
334
  function moduleDefinitionsToImportDeclarations(definitions) {
@@ -373,13 +356,13 @@ function registerModuleLevelBinding(ctx, modules, binding, target) {
373
356
  const current = ctx.bindings.get(binding);
374
357
  if (current) {
375
358
  modules.push(current);
376
- } else if (isPathValid(target.path, t6.isVariableDeclarator)) {
359
+ } else if (isPathValid(target.path, t7.isVariableDeclarator)) {
377
360
  const definitions = splitVariableDeclarator(ctx, target.path);
378
361
  for (let k = 0, klen = definitions.length; k < klen; k++) {
379
362
  modules.push(definitions[k]);
380
363
  ctx.bindings.set(definitions[k].local, definitions[k]);
381
364
  }
382
- } else if (isPathValid(target.path, t6.isFunctionDeclaration)) {
365
+ } else if (isPathValid(target.path, t7.isFunctionDeclaration)) {
383
366
  const definition = splitFunctionDeclaration(ctx, target.path);
384
367
  modules.push(definition);
385
368
  ctx.bindings.set(definition.local, definition);
@@ -432,9 +415,9 @@ function createRootFile(ctx, bindings, replacement) {
432
415
  const rootFile = createVirtualFileName(ctx);
433
416
  const rootContent = generateCode(
434
417
  ctx.id,
435
- t6.program([
418
+ t7.program([
436
419
  ...ctx.options.mode === "server" ? moduleDefinitionsToImportDeclarations(bindings.modules) : [],
437
- t6.exportDefaultDeclaration(replacement)
420
+ t7.exportDefaultDeclaration(replacement)
438
421
  ])
439
422
  );
440
423
  ctx.onVirtualFile(
@@ -444,12 +427,12 @@ function createRootFile(ctx, bindings, replacement) {
444
427
  );
445
428
  return rootFile;
446
429
  }
447
- function createEntryFile(ctx, path2, rootFile, imported) {
448
- let id = `${ctx.blocks.hash}-${ctx.blocks.count++}`;
430
+ function createEntryFile(ctx, path2, rootFile, imported, idPrefix) {
431
+ let id = `${idPrefix || ""}${ctx.blocks.hash}-${ctx.blocks.count++}`;
449
432
  if (ctx.options.env !== "production") {
450
433
  id += `-${getDescriptiveName(path2, "anonymous")}`;
451
434
  }
452
- const entryID = path2.scope.generateUidIdentifier("entry");
435
+ const entryID = generateUniqueName(path2, "entry");
453
436
  const entryImports = [
454
437
  {
455
438
  kind: imported.kind,
@@ -458,9 +441,9 @@ function createEntryFile(ctx, path2, rootFile, imported) {
458
441
  imported: imported.kind === "named" ? imported.name : void 0
459
442
  }
460
443
  ];
461
- const args = [t6.stringLiteral(id)];
462
- if (ctx.options.mode === "server") {
463
- const rootID = path2.scope.generateUidIdentifier("root");
444
+ const args = [t7.stringLiteral(id)];
445
+ if (rootFile) {
446
+ const rootID = generateUniqueName(path2, "root");
464
447
  entryImports.push({
465
448
  kind: "default",
466
449
  source: rootFile,
@@ -471,9 +454,9 @@ function createEntryFile(ctx, path2, rootFile, imported) {
471
454
  const entryFile = createVirtualFileName(ctx);
472
455
  const entryContent = generateCode(
473
456
  ctx.id,
474
- t6.program([
457
+ t7.program([
475
458
  ...moduleDefinitionsToImportDeclarations(entryImports),
476
- t6.exportDefaultDeclaration(t6.callExpression(entryID, args))
459
+ t7.exportDefaultDeclaration(t7.callExpression(entryID, args))
477
460
  ])
478
461
  );
479
462
  ctx.onVirtualFile(
@@ -489,17 +472,17 @@ function splitFunctionDeclaration(ctx, path2) {
489
472
  const file = createVirtualFileName(ctx);
490
473
  const compiled = generateCode(
491
474
  ctx.id,
492
- t6.program([
475
+ t7.program([
493
476
  ...moduleDefinitionsToImportDeclarations(modules),
494
- t6.exportNamedDeclaration(path2.node)
477
+ t7.exportNamedDeclaration(path2.node)
495
478
  ])
496
479
  );
497
480
  ctx.onVirtualFile(file, { code: compiled.code, map: compiled.map }, "none");
498
481
  const statement = getRootStatementPath(path2);
499
- const identifier3 = path2.node.id || path2.scope.generateUidIdentifier("func");
482
+ const identifier7 = path2.node.id || generateUniqueName(path2, "func");
500
483
  const definition = {
501
484
  kind: "named",
502
- local: identifier3.name,
485
+ local: identifier7.name,
503
486
  source: file
504
487
  };
505
488
  statement.insertBefore(moduleDefinitionToImportDeclaration(definition));
@@ -507,22 +490,22 @@ function splitFunctionDeclaration(ctx, path2) {
507
490
  return definition;
508
491
  }
509
492
  function splitVariableDeclarator(ctx, path2) {
510
- const init = unwrapPath(path2.get("init"), t6.isExpression);
493
+ const init = unwrapPath(path2.get("init"), t7.isExpression);
511
494
  const modules = init ? extractBindings(
512
495
  ctx,
513
496
  path2,
514
497
  getForeignBindings(
515
498
  path2,
516
- isPathValid(init, t6.isArrowFunctionExpression) || isPathValid(init, t6.isFunctionExpression) ? "function" : "expression"
499
+ isPathValid(init, t7.isArrowFunctionExpression) || isPathValid(init, t7.isFunctionExpression) ? "function" : "expression"
517
500
  )
518
501
  ).modules : [];
519
502
  const file = createVirtualFileName(ctx);
520
503
  const parent = path2.parentPath.node;
521
504
  const compiled = generateCode(
522
505
  ctx.id,
523
- t6.program([
506
+ t7.program([
524
507
  ...moduleDefinitionsToImportDeclarations(modules),
525
- t6.exportNamedDeclaration(t6.variableDeclaration(parent.kind, [path2.node]))
508
+ t7.exportNamedDeclaration(t7.variableDeclaration(parent.kind, [path2.node]))
526
509
  ])
527
510
  );
528
511
  ctx.onVirtualFile(file, { code: compiled.code, map: compiled.map }, "none");
@@ -540,12 +523,91 @@ function splitVariableDeclarator(ctx, path2) {
540
523
  path2.remove();
541
524
  return definitions;
542
525
  }
543
- var BREAK_KEY = t6.numericLiteral(0);
544
- var CONTINUE_KEY = t6.numericLiteral(1);
545
- var RETURN_KEY = t6.numericLiteral(2);
546
- var NO_HALT_KEY = t6.numericLiteral(3);
547
- var THROW_KEY = t6.numericLiteral(4);
548
- var YIELD_KEY = t6.numericLiteral(5);
526
+ var BREAK_KEY = t7.numericLiteral(0);
527
+ var CONTINUE_KEY = t7.numericLiteral(1);
528
+ var RETURN_KEY = t7.numericLiteral(2);
529
+ var NO_HALT_KEY = t7.numericLiteral(3);
530
+ var THROW_KEY = t7.numericLiteral(4);
531
+ var YIELD_KEY = t7.numericLiteral(5);
532
+ function getGeneratorReplacementForBlock(path2, registerID, args) {
533
+ const iterator = generateUniqueName(path2, "iterator");
534
+ const step = generateUniqueName(path2, "step");
535
+ const replacement = [
536
+ t7.variableDeclaration("let", [
537
+ t7.variableDeclarator(step),
538
+ // First, get the iterator by calling the generator
539
+ t7.variableDeclarator(
540
+ iterator,
541
+ t7.awaitExpression(t7.callExpression(registerID, args))
542
+ )
543
+ ]),
544
+ // Create a while statement, the intent is to
545
+ // repeatedly iterate the generator
546
+ t7.whileStatement(
547
+ t7.booleanLiteral(true),
548
+ t7.blockStatement([
549
+ // Get the next value
550
+ t7.expressionStatement(
551
+ t7.assignmentExpression(
552
+ "=",
553
+ step,
554
+ t7.awaitExpression(
555
+ t7.callExpression(
556
+ t7.memberExpression(iterator, t7.identifier("next")),
557
+ []
558
+ )
559
+ )
560
+ )
561
+ ),
562
+ // Check if the step is done
563
+ t7.ifStatement(
564
+ t7.memberExpression(step, t7.identifier("done")),
565
+ t7.blockStatement([
566
+ // exit the loop
567
+ t7.breakStatement()
568
+ ]),
569
+ // Otherwise, yield the value
570
+ t7.blockStatement([
571
+ t7.expressionStatement(
572
+ t7.yieldExpression(
573
+ t7.memberExpression(step, t7.identifier("value"))
574
+ )
575
+ )
576
+ ])
577
+ )
578
+ ])
579
+ )
580
+ ];
581
+ return [replacement, step];
582
+ }
583
+
584
+ // src/utils/get-import-identifier.ts
585
+ var t8 = __toESM(require("@babel/types"), 1);
586
+ function getImportIdentifier(state, path2, registration) {
587
+ const name = registration.kind === "named" ? registration.name : "default";
588
+ const target = `${registration.source}[${name}]`;
589
+ const current = state.imports.get(target);
590
+ if (current) {
591
+ return current;
592
+ }
593
+ const programParent = path2.scope.getProgramParent();
594
+ const uid = generateUniqueName(programParent.path, name);
595
+ programParent.registerDeclaration(
596
+ programParent.path.unshiftContainer(
597
+ "body",
598
+ t8.importDeclaration(
599
+ [
600
+ registration.kind === "named" ? t8.importSpecifier(uid, t8.identifier(registration.name)) : t8.importDefaultSpecifier(uid)
601
+ ],
602
+ t8.stringLiteral(registration.source)
603
+ )
604
+ )[0]
605
+ );
606
+ state.imports.set(target, uid);
607
+ return uid;
608
+ }
609
+
610
+ // src/split-block-directive.ts
549
611
  function transformBlockContent(path2, mutations) {
550
612
  const target = path2.scope.getFunctionParent() || path2.scope.getProgramParent();
551
613
  const breaks = [];
@@ -554,7 +616,7 @@ function transformBlockContent(path2, mutations) {
554
616
  let continueCount = 0;
555
617
  let hasReturn = false;
556
618
  let hasYield = false;
557
- const applyMutations = mutations.length ? path2.scope.generateUidIdentifier("mutate") : void 0;
619
+ const applyMutations = mutations.length ? generateUniqueName(path2, "mutate") : void 0;
558
620
  path2.traverse({
559
621
  BreakStatement(child) {
560
622
  const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
@@ -564,14 +626,14 @@ function transformBlockContent(path2, mutations) {
564
626
  if (child.node.label) {
565
627
  const targetName = child.node.label.name;
566
628
  breaks.push(targetName);
567
- replacement.push(t6.stringLiteral(targetName));
629
+ replacement.push(t9.stringLiteral(targetName));
568
630
  } else {
569
- replacement.push(t6.nullLiteral());
631
+ replacement.push(t9.nullLiteral());
570
632
  }
571
633
  if (applyMutations) {
572
- replacement.push(t6.callExpression(applyMutations, []));
634
+ replacement.push(t9.callExpression(applyMutations, []));
573
635
  }
574
- child.replaceWith(t6.returnStatement(t6.arrayExpression(replacement)));
636
+ child.replaceWith(t9.returnStatement(t9.arrayExpression(replacement)));
575
637
  child.skip();
576
638
  }
577
639
  },
@@ -583,14 +645,14 @@ function transformBlockContent(path2, mutations) {
583
645
  if (child.node.label) {
584
646
  const targetName = child.node.label.name;
585
647
  continues.push(targetName);
586
- replacement.push(t6.stringLiteral(targetName));
648
+ replacement.push(t9.stringLiteral(targetName));
587
649
  } else {
588
- replacement.push(t6.nullLiteral());
650
+ replacement.push(t9.nullLiteral());
589
651
  }
590
652
  if (applyMutations) {
591
- replacement.push(t6.callExpression(applyMutations, []));
653
+ replacement.push(t9.callExpression(applyMutations, []));
592
654
  }
593
- child.replaceWith(t6.returnStatement(t6.arrayExpression(replacement)));
655
+ child.replaceWith(t9.returnStatement(t9.arrayExpression(replacement)));
594
656
  child.skip();
595
657
  }
596
658
  },
@@ -600,10 +662,10 @@ function transformBlockContent(path2, mutations) {
600
662
  hasReturn = true;
601
663
  const arg = child.get("argument");
602
664
  arg.replaceWith(
603
- t6.arrayExpression([
665
+ t9.arrayExpression([
604
666
  RETURN_KEY,
605
- arg.node ? arg.node : t6.nullLiteral(),
606
- applyMutations ? t6.callExpression(applyMutations, []) : t6.nullLiteral()
667
+ arg.node ? arg.node : t9.nullLiteral(),
668
+ applyMutations ? t9.callExpression(applyMutations, []) : t9.nullLiteral()
607
669
  ])
608
670
  );
609
671
  }
@@ -616,43 +678,43 @@ function transformBlockContent(path2, mutations) {
616
678
  } else {
617
679
  const arg = child.get("argument");
618
680
  arg.replaceWith(
619
- t6.arrayExpression([
681
+ t9.arrayExpression([
620
682
  YIELD_KEY,
621
- arg.node ? arg.node : t6.nullLiteral(),
622
- applyMutations ? t6.callExpression(applyMutations, []) : t6.nullLiteral()
683
+ arg.node ? arg.node : t9.nullLiteral(),
684
+ applyMutations ? t9.callExpression(applyMutations, []) : t9.nullLiteral()
623
685
  ])
624
686
  );
625
687
  }
626
688
  }
627
689
  }
628
690
  });
629
- const error = path2.scope.generateUidIdentifier("error");
691
+ const error = generateUniqueName(path2, "error");
630
692
  const throwResult = [THROW_KEY, error];
631
693
  const haltResult = [NO_HALT_KEY];
632
694
  if (applyMutations) {
633
- throwResult.push(t6.callExpression(applyMutations, []));
634
- haltResult.push(t6.nullLiteral());
635
- haltResult.push(t6.callExpression(applyMutations, []));
695
+ throwResult.push(t9.callExpression(applyMutations, []));
696
+ haltResult.push(t9.nullLiteral());
697
+ haltResult.push(t9.callExpression(applyMutations, []));
636
698
  }
637
699
  const statements = [
638
- t6.tryStatement(
639
- t6.blockStatement(path2.node.body),
640
- t6.catchClause(
700
+ t9.tryStatement(
701
+ t9.blockStatement(path2.node.body),
702
+ t9.catchClause(
641
703
  error,
642
- t6.blockStatement([t6.returnStatement(t6.arrayExpression(throwResult))])
704
+ t9.blockStatement([t9.returnStatement(t9.arrayExpression(throwResult))])
643
705
  )
644
706
  ),
645
- t6.returnStatement(t6.arrayExpression(haltResult))
707
+ t9.returnStatement(t9.arrayExpression(haltResult))
646
708
  ];
647
709
  if (applyMutations) {
648
710
  statements.unshift(
649
- t6.variableDeclaration("const", [
650
- t6.variableDeclarator(
711
+ t9.variableDeclaration("const", [
712
+ t9.variableDeclarator(
651
713
  applyMutations,
652
- t6.arrowFunctionExpression(
714
+ t9.arrowFunctionExpression(
653
715
  [],
654
- t6.objectExpression(
655
- mutations.map((item) => t6.objectProperty(item, item, false, true))
716
+ t9.objectExpression(
717
+ mutations.map((item) => t9.objectProperty(item, item, false, true))
656
718
  )
657
719
  )
658
720
  )
@@ -665,21 +727,21 @@ function transformBlockContent(path2, mutations) {
665
727
  function getBreakCheck(returnType, returnResult, breakCount, breaks, check) {
666
728
  let current;
667
729
  if (breakCount !== breaks.length) {
668
- current = t6.blockStatement([t6.breakStatement()]);
730
+ current = t9.blockStatement([t9.breakStatement()]);
669
731
  }
670
732
  for (let i = 0, len = breaks.length; i < len; i++) {
671
733
  const target = breaks[i];
672
- current = t6.blockStatement([
673
- t6.ifStatement(
674
- t6.binaryExpression("===", returnResult, t6.stringLiteral(target)),
675
- t6.blockStatement([t6.breakStatement(t6.identifier(target))]),
734
+ current = t9.blockStatement([
735
+ t9.ifStatement(
736
+ t9.binaryExpression("===", returnResult, t9.stringLiteral(target)),
737
+ t9.blockStatement([t9.breakStatement(t9.identifier(target))]),
676
738
  current
677
739
  )
678
740
  ]);
679
741
  }
680
742
  if (current) {
681
- return t6.ifStatement(
682
- t6.binaryExpression("===", returnType, BREAK_KEY),
743
+ return t9.ifStatement(
744
+ t9.binaryExpression("===", returnType, BREAK_KEY),
683
745
  current,
684
746
  check
685
747
  );
@@ -689,87 +751,36 @@ function getBreakCheck(returnType, returnResult, breakCount, breaks, check) {
689
751
  function getContinueCheck(returnType, returnResult, continueCount, continues, check) {
690
752
  let current;
691
753
  if (continueCount !== continues.length) {
692
- current = t6.blockStatement([t6.continueStatement()]);
754
+ current = t9.blockStatement([t9.continueStatement()]);
693
755
  }
694
756
  for (let i = 0, len = continues.length; i < len; i++) {
695
757
  const target = continues[i];
696
- current = t6.blockStatement([
697
- t6.ifStatement(
698
- t6.binaryExpression("===", returnResult, t6.stringLiteral(target)),
699
- t6.blockStatement([t6.continueStatement(t6.identifier(target))]),
758
+ current = t9.blockStatement([
759
+ t9.ifStatement(
760
+ t9.binaryExpression("===", returnResult, t9.stringLiteral(target)),
761
+ t9.blockStatement([t9.continueStatement(t9.identifier(target))]),
700
762
  current
701
763
  )
702
764
  ]);
703
765
  }
704
766
  if (current) {
705
- return t6.ifStatement(
706
- t6.binaryExpression("===", returnType, CONTINUE_KEY),
767
+ return t9.ifStatement(
768
+ t9.binaryExpression("===", returnType, CONTINUE_KEY),
707
769
  current,
708
770
  check
709
771
  );
710
772
  }
711
773
  return check;
712
774
  }
713
- function getGeneratorReplacementForServerBlock(path2, registerID, args) {
714
- const iterator = path2.scope.generateUidIdentifier("iterator");
715
- const step = path2.scope.generateUidIdentifier("step");
716
- const replacement = [
717
- t6.variableDeclaration("let", [
718
- t6.variableDeclarator(step),
719
- // First, get the iterator by calling the generator
720
- t6.variableDeclarator(
721
- iterator,
722
- t6.awaitExpression(t6.callExpression(registerID, args))
723
- )
724
- ]),
725
- // Create a while statement, the intent is to
726
- // repeatedly iterate the generator
727
- t6.whileStatement(
728
- t6.booleanLiteral(true),
729
- t6.blockStatement([
730
- // Get the next value
731
- t6.expressionStatement(
732
- t6.assignmentExpression(
733
- "=",
734
- step,
735
- t6.awaitExpression(
736
- t6.callExpression(
737
- t6.memberExpression(iterator, t6.identifier("next")),
738
- []
739
- )
740
- )
741
- )
742
- ),
743
- // Check if the step is done
744
- t6.ifStatement(
745
- t6.memberExpression(step, t6.identifier("done")),
746
- t6.blockStatement([
747
- // exit the loop
748
- t6.breakStatement()
749
- ]),
750
- // Otherwise, yield the value
751
- t6.blockStatement([
752
- t6.expressionStatement(
753
- t6.yieldExpression(
754
- t6.memberExpression(step, t6.identifier("value"))
755
- )
756
- )
757
- ])
758
- )
759
- ])
760
- )
761
- ];
762
- return [replacement, step];
763
- }
764
- function getBlockReplacement(ctx, path2, entryFile, bindings, halting) {
765
- const returnType = path2.scope.generateUidIdentifier("type");
766
- const returnResult = path2.scope.generateUidIdentifier("result");
767
- const returnMutations = path2.scope.generateUidIdentifier("mutations");
775
+ function getBlockDirectiveReplacement(ctx, path2, entryFile, bindings, halting) {
776
+ const returnType = generateUniqueName(path2, "type");
777
+ const returnResult = generateUniqueName(path2, "result");
778
+ const returnMutations = generateUniqueName(path2, "mutations");
768
779
  let check;
769
780
  if (halting.hasReturn) {
770
- check = t6.ifStatement(
771
- t6.binaryExpression("===", returnType, RETURN_KEY),
772
- t6.blockStatement([t6.returnStatement(returnResult)]),
781
+ check = t9.ifStatement(
782
+ t9.binaryExpression("===", returnType, RETURN_KEY),
783
+ t9.blockStatement([t9.returnStatement(returnResult)]),
773
784
  check
774
785
  );
775
786
  }
@@ -793,33 +804,41 @@ function getBlockReplacement(ctx, path2, entryFile, bindings, halting) {
793
804
  }
794
805
  const replacement = [];
795
806
  if (halting.hasYield) {
796
- const blockID = path2.scope.generateUidIdentifier("block");
807
+ const blockID = generateUniqueName(path2, "block");
797
808
  replacement.push(
798
- t6.variableDeclaration("const", [
799
- t6.variableDeclarator(
809
+ t9.variableDeclaration("const", [
810
+ t9.variableDeclarator(
800
811
  blockID,
801
- t6.callExpression(getImportIdentifier(ctx, path2, HIDDEN_GENERATOR), [
802
- t6.memberExpression(
803
- t6.awaitExpression(t6.importExpression(t6.stringLiteral(entryFile))),
804
- t6.identifier("default")
805
- ),
806
- bindings.mutations.length ? t6.arrowFunctionExpression(
807
- [returnMutations],
808
- t6.assignmentExpression(
809
- "=",
810
- t6.objectPattern(
811
- bindings.mutations.map(
812
- (item) => t6.objectProperty(item, item, false, true)
813
- )
812
+ t9.callExpression(
813
+ getImportIdentifier(ctx, path2, {
814
+ ...HIDDEN_GENERATOR,
815
+ source: ctx.options.runtime
816
+ }),
817
+ [
818
+ t9.memberExpression(
819
+ t9.awaitExpression(
820
+ t9.importExpression(t9.stringLiteral(entryFile))
814
821
  ),
815
- returnMutations
816
- )
817
- ) : t6.nullLiteral()
818
- ])
822
+ t9.identifier("default")
823
+ ),
824
+ bindings.mutations.length ? t9.arrowFunctionExpression(
825
+ [returnMutations],
826
+ t9.assignmentExpression(
827
+ "=",
828
+ t9.objectPattern(
829
+ bindings.mutations.map(
830
+ (item) => t9.objectProperty(item, item, false, true)
831
+ )
832
+ ),
833
+ returnMutations
834
+ )
835
+ ) : t9.nullLiteral()
836
+ ]
837
+ )
819
838
  )
820
839
  ])
821
840
  );
822
- const [reps, step] = getGeneratorReplacementForServerBlock(
841
+ const [reps, step] = getGeneratorReplacementForBlock(
823
842
  path2,
824
843
  blockID,
825
844
  bindings.locals
@@ -828,40 +847,46 @@ function getBlockReplacement(ctx, path2, entryFile, bindings, halting) {
828
847
  replacement.push(reps[i]);
829
848
  }
830
849
  replacement.push(
831
- t6.variableDeclaration("const", [
832
- t6.variableDeclarator(
833
- t6.arrayPattern([returnType, returnResult]),
834
- t6.memberExpression(step, t6.identifier("value"))
850
+ t9.variableDeclaration("const", [
851
+ t9.variableDeclarator(
852
+ t9.arrayPattern([returnType, returnResult]),
853
+ t9.memberExpression(step, t9.identifier("value"))
835
854
  )
836
855
  ])
837
856
  );
838
857
  } else {
839
858
  replacement.push(
840
- t6.variableDeclaration("const", [
841
- t6.variableDeclarator(
842
- t6.arrayPattern([returnType, returnResult]),
843
- t6.awaitExpression(
844
- t6.callExpression(
845
- t6.callExpression(getImportIdentifier(ctx, path2, HIDDEN_FUNC), [
846
- t6.memberExpression(
847
- t6.awaitExpression(
848
- t6.importExpression(t6.stringLiteral(entryFile))
849
- ),
850
- t6.identifier("default")
851
- ),
852
- bindings.mutations.length ? t6.arrowFunctionExpression(
853
- [returnMutations],
854
- t6.assignmentExpression(
855
- "=",
856
- t6.objectPattern(
857
- bindings.mutations.map(
858
- (item) => t6.objectProperty(item, item, false, true)
859
- )
859
+ t9.variableDeclaration("const", [
860
+ t9.variableDeclarator(
861
+ t9.arrayPattern([returnType, returnResult]),
862
+ t9.awaitExpression(
863
+ t9.callExpression(
864
+ t9.callExpression(
865
+ getImportIdentifier(ctx, path2, {
866
+ ...HIDDEN_FUNC,
867
+ source: ctx.options.runtime
868
+ }),
869
+ [
870
+ t9.memberExpression(
871
+ t9.awaitExpression(
872
+ t9.importExpression(t9.stringLiteral(entryFile))
860
873
  ),
861
- returnMutations
862
- )
863
- ) : t6.nullLiteral()
864
- ]),
874
+ t9.identifier("default")
875
+ ),
876
+ bindings.mutations.length ? t9.arrowFunctionExpression(
877
+ [returnMutations],
878
+ t9.assignmentExpression(
879
+ "=",
880
+ t9.objectPattern(
881
+ bindings.mutations.map(
882
+ (item) => t9.objectProperty(item, item, false, true)
883
+ )
884
+ ),
885
+ returnMutations
886
+ )
887
+ ) : t9.nullLiteral()
888
+ ]
889
+ ),
865
890
  bindings.locals
866
891
  )
867
892
  )
@@ -872,28 +897,33 @@ function getBlockReplacement(ctx, path2, entryFile, bindings, halting) {
872
897
  if (check) {
873
898
  replacement.push(check);
874
899
  }
875
- return t6.blockStatement(replacement);
900
+ return t9.blockStatement(replacement);
876
901
  }
877
- function replaceBlock(ctx, path2, directive, bindings) {
902
+ function replaceBlockDirective(ctx, path2, directive, bindings) {
878
903
  const halting = transformBlockContent(path2, bindings.mutations);
879
- const rootFile = createRootFile(
904
+ const entryFile = createEntryFile(
880
905
  ctx,
881
- bindings,
882
- t6.functionExpression(
883
- void 0,
884
- bindings.locals,
885
- t6.blockStatement(path2.node.body),
886
- halting.hasYield,
887
- true
888
- )
906
+ path2,
907
+ ctx.options.mode === "server" || directive.isomorphic ? createRootFile(
908
+ ctx,
909
+ bindings,
910
+ t9.functionExpression(
911
+ void 0,
912
+ bindings.locals,
913
+ t9.blockStatement(path2.node.body),
914
+ halting.hasYield,
915
+ true
916
+ )
917
+ ) : void 0,
918
+ directive.target,
919
+ directive.idPrefix
889
920
  );
890
- const entryFile = createEntryFile(ctx, path2, rootFile, directive.target);
891
921
  path2.replaceWith(
892
- getBlockReplacement(ctx, path2, entryFile, bindings, halting)
922
+ getBlockDirectiveReplacement(ctx, path2, entryFile, bindings, halting)
893
923
  );
894
924
  }
895
- function splitBlock(ctx, path2, directive) {
896
- replaceBlock(
925
+ function splitBlockDirective(ctx, path2, directive) {
926
+ replaceBlockDirective(
897
927
  ctx,
898
928
  path2,
899
929
  directive,
@@ -905,63 +935,154 @@ function splitBlock(ctx, path2, directive) {
905
935
  )
906
936
  );
907
937
  }
938
+
939
+ // src/utils/directive-check.ts
940
+ var t10 = __toESM(require("@babel/types"), 1);
941
+ function getValidDirectiveFromString(ctx, string) {
942
+ for (let i = 0, len = ctx.options.definitions.length; i < len; i++) {
943
+ const current = ctx.options.definitions[i];
944
+ switch (current.type) {
945
+ case "block-directive":
946
+ case "function-directive": {
947
+ if (current.directive === string) {
948
+ return current;
949
+ }
950
+ break;
951
+ }
952
+ }
953
+ }
954
+ return void 0;
955
+ }
956
+ function getDefinitionFromDirectives(ctx, path2) {
957
+ for (let i = 0, len = path2.node.directives.length; i < len; i++) {
958
+ const statement = path2.node.directives[i].value.value;
959
+ const directive = getValidDirectiveFromString(ctx, statement);
960
+ if (directive) {
961
+ return directive;
962
+ }
963
+ }
964
+ return void 0;
965
+ }
966
+ function getDefinitionFromFauxDirectives(ctx, path2) {
967
+ for (let i = 0, len = path2.node.body.length; i < len; i++) {
968
+ const statement = path2.node.body[i];
969
+ if (t10.isExpressionStatement(statement) && t10.isStringLiteral(statement.expression)) {
970
+ const directive = getValidDirectiveFromString(
971
+ ctx,
972
+ statement.expression.value
973
+ );
974
+ if (directive) {
975
+ return directive;
976
+ }
977
+ } else {
978
+ break;
979
+ }
980
+ }
981
+ return void 0;
982
+ }
983
+ function getDirectiveDefinitionFromBlock(ctx, path2) {
984
+ const parent = path2.getFunctionParent();
985
+ if (parent && !parent.node.async) {
986
+ return void 0;
987
+ }
988
+ return getDefinitionFromDirectives(ctx, path2) || getDefinitionFromFauxDirectives(ctx, path2);
989
+ }
990
+ function cleanBlockForDirectives(path2, definition) {
991
+ const newDirectives = [];
992
+ for (let i = 0, len = path2.node.directives.length; i < len; i++) {
993
+ const current = path2.node.directives[i];
994
+ if (current.value.value !== definition.directive) {
995
+ newDirectives.push(current);
996
+ }
997
+ }
998
+ path2.node.directives = newDirectives;
999
+ }
1000
+ function cleanBlockForFauxDirectives(path2, definition) {
1001
+ const body = path2.get("body");
1002
+ for (let i = 0, len = body.length; i < len; i++) {
1003
+ const statement = body[i];
1004
+ if (t10.isExpressionStatement(statement.node) && t10.isStringLiteral(statement.node.expression)) {
1005
+ if (statement.node.expression.value === definition.directive) {
1006
+ statement.remove();
1007
+ return;
1008
+ }
1009
+ }
1010
+ }
1011
+ }
1012
+
1013
+ // src/transform-block-directive.ts
1014
+ function transformBlockDirective(ctx, path2) {
1015
+ const definition = getDirectiveDefinitionFromBlock(ctx, path2);
1016
+ if (!definition || definition.type !== "block-directive") {
1017
+ return;
1018
+ }
1019
+ cleanBlockForDirectives(path2, definition);
1020
+ cleanBlockForFauxDirectives(path2, definition);
1021
+ splitBlockDirective(ctx, path2, definition);
1022
+ }
1023
+
1024
+ // src/transform-call.ts
1025
+ var t12 = __toESM(require("@babel/types"), 1);
1026
+
1027
+ // src/split-call.ts
1028
+ var t11 = __toESM(require("@babel/types"), 1);
1029
+
1030
+ // src/utils/assert.ts
1031
+ function assert(cond, error) {
1032
+ if (!cond) {
1033
+ throw new Error(error);
1034
+ }
1035
+ }
1036
+
1037
+ // src/split-call.ts
908
1038
  function transformFunctionContent(path2, mutations) {
909
1039
  const target = path2.scope.getFunctionParent() || path2.scope.getProgramParent();
910
- const applyMutations = mutations.length ? path2.scope.generateUidIdentifier("mutate") : void 0;
911
- let hasReturn = false;
912
- let hasYield = false;
1040
+ const applyMutations = mutations.length ? generateUniqueName(path2, "mutate") : void 0;
913
1041
  path2.traverse({
914
1042
  ReturnStatement(child) {
915
1043
  const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
916
1044
  if (parent === target) {
917
- hasReturn = true;
918
1045
  const replacement = [RETURN_KEY];
919
1046
  if (child.node.argument) {
920
1047
  replacement.push(child.node.argument);
921
1048
  } else {
922
- replacement.push(t6.nullLiteral());
1049
+ replacement.push(t11.nullLiteral());
923
1050
  }
924
1051
  if (applyMutations) {
925
- replacement.push(t6.callExpression(applyMutations, []));
1052
+ replacement.push(t11.callExpression(applyMutations, []));
926
1053
  }
927
- child.replaceWith(t6.returnStatement(t6.arrayExpression(replacement)));
1054
+ child.replaceWith(t11.returnStatement(t11.arrayExpression(replacement)));
928
1055
  child.skip();
929
1056
  }
930
- },
931
- YieldExpression(child) {
932
- const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
933
- if (parent === target) {
934
- hasYield = true;
935
- }
936
1057
  }
937
1058
  });
938
- const error = path2.scope.generateUidIdentifier("error");
1059
+ const error = generateUniqueName(path2, "error");
939
1060
  const throwResult = [THROW_KEY, error];
940
1061
  const haltResult = [NO_HALT_KEY];
941
1062
  if (applyMutations) {
942
- throwResult.push(t6.callExpression(applyMutations, []));
943
- haltResult.push(t6.nullLiteral());
944
- haltResult.push(t6.callExpression(applyMutations, []));
1063
+ throwResult.push(t11.callExpression(applyMutations, []));
1064
+ haltResult.push(t11.nullLiteral());
1065
+ haltResult.push(t11.callExpression(applyMutations, []));
945
1066
  }
946
1067
  const statements = [
947
- t6.tryStatement(
948
- t6.blockStatement(path2.node.body),
949
- t6.catchClause(
1068
+ t11.tryStatement(
1069
+ t11.blockStatement(path2.node.body),
1070
+ t11.catchClause(
950
1071
  error,
951
- t6.blockStatement([t6.returnStatement(t6.arrayExpression(throwResult))])
1072
+ t11.blockStatement([t11.returnStatement(t11.arrayExpression(throwResult))])
952
1073
  )
953
1074
  ),
954
- t6.returnStatement(t6.arrayExpression(haltResult))
1075
+ t11.returnStatement(t11.arrayExpression(haltResult))
955
1076
  ];
956
1077
  if (applyMutations) {
957
1078
  statements.unshift(
958
- t6.variableDeclaration("const", [
959
- t6.variableDeclarator(
1079
+ t11.variableDeclaration("const", [
1080
+ t11.variableDeclarator(
960
1081
  applyMutations,
961
- t6.arrowFunctionExpression(
1082
+ t11.arrowFunctionExpression(
962
1083
  [],
963
- t6.objectExpression(
964
- mutations.map((item) => t6.objectProperty(item, item, false, true))
1084
+ t11.objectExpression(
1085
+ mutations.map((item) => t11.objectProperty(item, item, false, true))
965
1086
  )
966
1087
  )
967
1088
  )
@@ -969,160 +1090,256 @@ function transformFunctionContent(path2, mutations) {
969
1090
  );
970
1091
  }
971
1092
  path2.node.body = statements;
972
- return {
973
- hasReturn,
974
- hasYield
975
- };
976
1093
  }
977
- function getFunctionReplacement(ctx, path2, entryFile, bindings, halting) {
978
- const rest = path2.scope.generateUidIdentifier("rest");
979
- const returnType = path2.scope.generateUidIdentifier("type");
980
- const returnResult = path2.scope.generateUidIdentifier("result");
981
- const returnMutations = path2.scope.generateUidIdentifier("mutations");
1094
+ function getFunctionReplacement(ctx, path2, entryFile, bindings) {
1095
+ const rest = generateUniqueName(path2, "rest");
1096
+ const returnType = generateUniqueName(path2, "type");
1097
+ const returnResult = generateUniqueName(path2, "result");
1098
+ const returnMutations = generateUniqueName(path2, "mutations");
1099
+ const source = generateUniqueName(path2, "source");
982
1100
  const replacement = [];
983
- if (halting.hasYield) {
984
- const funcID = path2.scope.generateUidIdentifier("fn");
1101
+ if (path2.node.generator) {
1102
+ const funcID = generateUniqueName(path2, "fn");
985
1103
  replacement.push(
986
- t6.variableDeclaration("const", [
987
- t6.variableDeclarator(
1104
+ t11.variableDeclaration("const", [
1105
+ t11.variableDeclarator(
988
1106
  funcID,
989
- t6.callExpression(getImportIdentifier(ctx, path2, HIDDEN_GENERATOR), [
990
- t6.memberExpression(
991
- t6.awaitExpression(t6.importExpression(t6.stringLiteral(entryFile))),
992
- t6.identifier("default")
993
- ),
994
- bindings.mutations.length ? t6.arrowFunctionExpression(
995
- [returnMutations],
996
- t6.assignmentExpression(
997
- "=",
998
- t6.objectPattern(
999
- bindings.mutations.map(
1000
- (item) => t6.objectProperty(item, item, false, true)
1001
- )
1002
- ),
1003
- returnMutations
1004
- )
1005
- ) : t6.nullLiteral()
1006
- ])
1107
+ t11.callExpression(
1108
+ getImportIdentifier(ctx, path2, {
1109
+ ...HIDDEN_GENERATOR,
1110
+ source: ctx.options.runtime
1111
+ }),
1112
+ [
1113
+ source,
1114
+ bindings.mutations.length ? t11.arrowFunctionExpression(
1115
+ [returnMutations],
1116
+ t11.assignmentExpression(
1117
+ "=",
1118
+ t11.objectPattern(
1119
+ bindings.mutations.map(
1120
+ (item) => t11.objectProperty(item, item, false, true)
1121
+ )
1122
+ ),
1123
+ returnMutations
1124
+ )
1125
+ ) : t11.nullLiteral()
1126
+ ]
1127
+ )
1007
1128
  )
1008
1129
  ])
1009
1130
  );
1010
- const [reps, step] = getGeneratorReplacementForServerBlock(path2, funcID, [
1011
- t6.arrayExpression(bindings.locals),
1012
- t6.spreadElement(rest)
1131
+ const [reps, step] = getGeneratorReplacementForBlock(path2, funcID, [
1132
+ t11.arrayExpression(bindings.locals),
1133
+ t11.spreadElement(rest)
1013
1134
  ]);
1014
1135
  for (let i = 0, len = reps.length; i < len; i++) {
1015
1136
  replacement.push(reps[i]);
1016
1137
  }
1017
1138
  replacement.push(
1018
- t6.variableDeclaration("const", [
1019
- t6.variableDeclarator(
1020
- t6.arrayPattern([returnType, returnResult]),
1021
- t6.memberExpression(step, t6.identifier("value"))
1139
+ t11.variableDeclaration("const", [
1140
+ t11.variableDeclarator(
1141
+ t11.arrayPattern([returnType, returnResult]),
1142
+ t11.memberExpression(step, t11.identifier("value"))
1022
1143
  )
1023
1144
  ])
1024
1145
  );
1025
1146
  } else {
1026
1147
  replacement.push(
1027
- t6.variableDeclaration("const", [
1028
- t6.variableDeclarator(
1029
- t6.arrayPattern([returnType, returnResult]),
1030
- t6.awaitExpression(
1031
- t6.callExpression(
1032
- t6.callExpression(getImportIdentifier(ctx, path2, HIDDEN_FUNC), [
1033
- t6.memberExpression(
1034
- t6.awaitExpression(
1035
- t6.importExpression(t6.stringLiteral(entryFile))
1036
- ),
1037
- t6.identifier("default")
1038
- ),
1039
- bindings.mutations.length ? t6.arrowFunctionExpression(
1040
- [returnMutations],
1041
- t6.assignmentExpression(
1042
- "=",
1043
- t6.objectPattern(
1044
- bindings.mutations.map(
1045
- (item) => t6.objectProperty(item, item, false, true)
1046
- )
1047
- ),
1048
- returnMutations
1049
- )
1050
- ) : t6.nullLiteral()
1051
- ]),
1052
- [t6.arrayExpression(bindings.locals), t6.spreadElement(rest)]
1148
+ t11.variableDeclaration("const", [
1149
+ t11.variableDeclarator(
1150
+ t11.arrayPattern([returnType, returnResult]),
1151
+ t11.awaitExpression(
1152
+ t11.callExpression(
1153
+ t11.callExpression(
1154
+ getImportIdentifier(ctx, path2, {
1155
+ ...HIDDEN_FUNC,
1156
+ source: ctx.options.runtime
1157
+ }),
1158
+ [
1159
+ source,
1160
+ bindings.mutations.length ? t11.arrowFunctionExpression(
1161
+ [returnMutations],
1162
+ t11.assignmentExpression(
1163
+ "=",
1164
+ t11.objectPattern(
1165
+ bindings.mutations.map(
1166
+ (item) => t11.objectProperty(item, item, false, true)
1167
+ )
1168
+ ),
1169
+ returnMutations
1170
+ )
1171
+ ) : t11.nullLiteral()
1172
+ ]
1173
+ ),
1174
+ [t11.arrayExpression(bindings.locals), t11.spreadElement(rest)]
1053
1175
  )
1054
1176
  )
1055
1177
  )
1056
1178
  ])
1057
1179
  );
1058
1180
  }
1059
- replacement.push(t6.returnStatement(returnResult));
1060
- if (isPathValid(path2, t6.isFunctionExpression)) {
1061
- return t6.functionExpression(
1062
- path2.node.id,
1063
- [t6.restElement(rest)],
1064
- t6.blockStatement(replacement),
1065
- halting.hasYield,
1066
- true
1067
- );
1181
+ replacement.push(t11.returnStatement(returnResult));
1182
+ return t11.arrowFunctionExpression(
1183
+ [],
1184
+ t11.blockStatement([
1185
+ t11.variableDeclaration("const", [
1186
+ t11.variableDeclarator(
1187
+ source,
1188
+ t11.memberExpression(
1189
+ t11.awaitExpression(t11.importExpression(t11.stringLiteral(entryFile))),
1190
+ t11.identifier("default")
1191
+ )
1192
+ )
1193
+ ]),
1194
+ t11.returnStatement(
1195
+ isPathValid(path2, t11.isFunctionExpression) ? t11.functionExpression(
1196
+ path2.node.id,
1197
+ [t11.restElement(rest)],
1198
+ t11.blockStatement(replacement),
1199
+ path2.node.generator,
1200
+ true
1201
+ ) : t11.arrowFunctionExpression(
1202
+ [t11.restElement(rest)],
1203
+ t11.blockStatement(replacement),
1204
+ true
1205
+ )
1206
+ )
1207
+ ]),
1208
+ true
1209
+ );
1210
+ }
1211
+ function replaceIsomorphicFunction(ctx, path2, func, bindings) {
1212
+ const body = path2.get("body");
1213
+ if (isPathValid(body, t11.isExpression)) {
1214
+ body.replaceWith(t11.blockStatement([t11.returnStatement(body.node)]));
1068
1215
  }
1069
- return t6.arrowFunctionExpression(
1070
- [t6.restElement(rest)],
1071
- t6.blockStatement(replacement),
1216
+ assert(isPathValid(body, t11.isBlockStatement), "invariant");
1217
+ const entryFile = createEntryFile(
1218
+ ctx,
1219
+ path2,
1220
+ createRootFile(
1221
+ ctx,
1222
+ bindings,
1223
+ t11.isFunctionExpression(path2.node) ? t11.functionExpression(
1224
+ path2.node.id,
1225
+ [t11.arrayPattern(bindings.locals), ...path2.node.params],
1226
+ path2.node.body,
1227
+ path2.node.generator,
1228
+ path2.node.async
1229
+ ) : t11.arrowFunctionExpression(
1230
+ [t11.arrayPattern(bindings.locals), ...path2.node.params],
1231
+ path2.node.body,
1232
+ path2.node.async
1233
+ )
1234
+ ),
1235
+ func.target,
1236
+ func.idPrefix
1237
+ );
1238
+ const source = generateUniqueName(path2, "source");
1239
+ const rest = generateUniqueName(path2, "rest");
1240
+ return t11.arrowFunctionExpression(
1241
+ [],
1242
+ t11.blockStatement([
1243
+ t11.variableDeclaration("const", [
1244
+ t11.variableDeclarator(
1245
+ source,
1246
+ t11.memberExpression(
1247
+ t11.awaitExpression(t11.importExpression(t11.stringLiteral(entryFile))),
1248
+ t11.identifier("default")
1249
+ )
1250
+ )
1251
+ ]),
1252
+ t11.returnStatement(
1253
+ t11.arrowFunctionExpression(
1254
+ [t11.restElement(rest)],
1255
+ t11.callExpression(source, [
1256
+ t11.arrayExpression(bindings.locals),
1257
+ t11.spreadElement(rest)
1258
+ ]),
1259
+ path2.node.async
1260
+ )
1261
+ )
1262
+ ]),
1072
1263
  true
1073
1264
  );
1074
1265
  }
1075
- function replaceFunction(ctx, path2, func, bindings) {
1266
+ function replaceFunctionFromCall(ctx, path2, func, bindings) {
1076
1267
  const body = path2.get("body");
1077
- if (isPathValid(body, t6.isExpression)) {
1078
- body.replaceWith(t6.blockStatement([t6.returnStatement(body.node)]));
1268
+ if (isPathValid(body, t11.isExpression)) {
1269
+ body.replaceWith(t11.blockStatement([t11.returnStatement(body.node)]));
1079
1270
  }
1080
- assert(isPathValid(body, t6.isBlockStatement), "invariant");
1081
- const halting = transformFunctionContent(body, bindings.mutations);
1082
- const rootFile = createRootFile(
1271
+ assert(isPathValid(body, t11.isBlockStatement), "invariant");
1272
+ transformFunctionContent(body, bindings.mutations);
1273
+ const entryFile = createEntryFile(
1083
1274
  ctx,
1084
- bindings,
1085
- t6.isFunctionExpression(path2.node) ? t6.functionExpression(
1086
- path2.node.id,
1087
- [t6.arrayPattern(bindings.locals), ...path2.node.params],
1088
- path2.node.body,
1089
- path2.node.async,
1090
- path2.node.generator
1091
- ) : t6.arrowFunctionExpression(
1092
- [t6.arrayPattern(bindings.locals), ...path2.node.params],
1093
- path2.node.body,
1094
- path2.node.async
1095
- )
1275
+ path2,
1276
+ ctx.options.mode === "server" || func.isomorphic ? createRootFile(
1277
+ ctx,
1278
+ bindings,
1279
+ t11.isFunctionExpression(path2.node) ? t11.functionExpression(
1280
+ path2.node.id,
1281
+ [t11.arrayPattern(bindings.locals), ...path2.node.params],
1282
+ path2.node.body,
1283
+ path2.node.generator,
1284
+ path2.node.async
1285
+ ) : t11.arrowFunctionExpression(
1286
+ [t11.arrayPattern(bindings.locals), ...path2.node.params],
1287
+ path2.node.body,
1288
+ path2.node.async
1289
+ )
1290
+ ) : void 0,
1291
+ func.target,
1292
+ func.idPrefix
1096
1293
  );
1097
- const entryFile = createEntryFile(ctx, path2, rootFile, func.target);
1098
- return getFunctionReplacement(ctx, path2, entryFile, bindings, halting);
1294
+ return getFunctionReplacement(ctx, path2, entryFile, bindings);
1099
1295
  }
1100
- function splitFunction(ctx, path2, func) {
1101
- return replaceFunction(
1296
+ function splitFunctionFromCall(ctx, path2, func) {
1297
+ const bindings = extractBindings(
1102
1298
  ctx,
1103
1299
  path2,
1104
- func,
1105
- extractBindings(ctx, path2, getForeignBindings(path2, "function"), func.pure)
1300
+ getForeignBindings(path2, "function"),
1301
+ func.pure
1106
1302
  );
1303
+ if (func.isomorphic) {
1304
+ return replaceIsomorphicFunction(ctx, path2, func, bindings);
1305
+ }
1306
+ return replaceFunctionFromCall(ctx, path2, func, bindings);
1107
1307
  }
1108
- function replaceExpression(ctx, path2, func, bindings) {
1109
- const rootFile = createRootFile(ctx, bindings, path2.node);
1110
- const entryFile = createEntryFile(ctx, path2, rootFile, func.target);
1111
- const rest = path2.scope.generateUidIdentifier("rest");
1112
- return t6.arrowFunctionExpression(
1113
- [t6.restElement(rest)],
1114
- t6.callExpression(
1115
- t6.memberExpression(
1116
- t6.awaitExpression(t6.importExpression(t6.stringLiteral(entryFile))),
1117
- t6.identifier("default")
1118
- ),
1119
- [t6.spreadElement(rest)]
1120
- ),
1308
+ function replaceExpressionFromCall(ctx, path2, func, bindings) {
1309
+ const entryFile = createEntryFile(
1310
+ ctx,
1311
+ path2,
1312
+ ctx.options.mode === "server" || func.isomorphic ? createRootFile(ctx, bindings, path2.node) : void 0,
1313
+ func.target,
1314
+ func.idPrefix
1315
+ );
1316
+ const rest = generateUniqueName(path2, "rest");
1317
+ const source = generateUniqueName(path2, "source");
1318
+ return t11.arrowFunctionExpression(
1319
+ [],
1320
+ t11.blockStatement([
1321
+ t11.variableDeclaration("const", [
1322
+ t11.variableDeclarator(
1323
+ source,
1324
+ t11.memberExpression(
1325
+ t11.awaitExpression(t11.importExpression(t11.stringLiteral(entryFile))),
1326
+ t11.identifier("default")
1327
+ )
1328
+ )
1329
+ ]),
1330
+ t11.returnStatement(
1331
+ t11.arrowFunctionExpression(
1332
+ [t11.restElement(rest)],
1333
+ t11.callExpression(source, [t11.spreadElement(rest)]),
1334
+ true
1335
+ )
1336
+ )
1337
+ ]),
1121
1338
  true
1122
1339
  );
1123
1340
  }
1124
- function splitExpression(ctx, path2, func) {
1125
- return replaceExpression(
1341
+ function splitExpressionFromCall(ctx, path2, func) {
1342
+ return replaceExpressionFromCall(
1126
1343
  ctx,
1127
1344
  path2,
1128
1345
  func,
@@ -1135,84 +1352,7 @@ function splitExpression(ctx, path2, func) {
1135
1352
  );
1136
1353
  }
1137
1354
 
1138
- // src/transform-block.ts
1139
- function getValidDirectiveFromString(ctx, string) {
1140
- for (let i = 0, len = ctx.options.directives.length; i < len; i++) {
1141
- const current = ctx.options.directives[i];
1142
- if (current.value === string) {
1143
- return current;
1144
- }
1145
- }
1146
- return void 0;
1147
- }
1148
- function getDefinitionFromDirectives(ctx, path2) {
1149
- for (let i = 0, len = path2.node.directives.length; i < len; i++) {
1150
- const statement = path2.node.directives[i].value.value;
1151
- const directive = getValidDirectiveFromString(ctx, statement);
1152
- if (directive) {
1153
- return directive;
1154
- }
1155
- }
1156
- return void 0;
1157
- }
1158
- function getDefinitionFromFauxDirectives(ctx, path2) {
1159
- for (let i = 0, len = path2.node.body.length; i < len; i++) {
1160
- const statement = path2.node.body[i];
1161
- if (statement.type === "ExpressionStatement" && statement.expression.type === "StringLiteral") {
1162
- const directive = getValidDirectiveFromString(
1163
- ctx,
1164
- statement.expression.value
1165
- );
1166
- if (directive) {
1167
- return directive;
1168
- }
1169
- } else {
1170
- break;
1171
- }
1172
- }
1173
- return void 0;
1174
- }
1175
- function getDirectiveDefinitionFromBlock(ctx, path2) {
1176
- const parent = path2.getFunctionParent();
1177
- if (parent && !parent.node.async) {
1178
- return void 0;
1179
- }
1180
- return getDefinitionFromDirectives(ctx, path2) || getDefinitionFromFauxDirectives(ctx, path2);
1181
- }
1182
- function cleanBlockForDirectives(path2, definition) {
1183
- const newDirectives = [];
1184
- for (let i = 0, len = path2.node.directives.length; i < len; i++) {
1185
- const current = path2.node.directives[i];
1186
- if (current.value.value !== definition.value) {
1187
- newDirectives.push(current);
1188
- }
1189
- }
1190
- path2.node.directives = newDirectives;
1191
- }
1192
- function cleanBlockForFauxDirectives(path2, definition) {
1193
- const body = path2.get("body");
1194
- for (let i = 0, len = body.length; i < len; i++) {
1195
- const statement = body[i];
1196
- if (statement.node.type === "ExpressionStatement" && statement.node.expression.type === "StringLiteral") {
1197
- if (statement.node.expression.value === definition.value) {
1198
- statement.remove();
1199
- return;
1200
- }
1201
- }
1202
- }
1203
- }
1204
- function transformBlock(ctx, path2) {
1205
- const definition = getDirectiveDefinitionFromBlock(ctx, path2);
1206
- if (!definition) {
1207
- return;
1208
- }
1209
- cleanBlockForDirectives(path2, definition);
1210
- cleanBlockForFauxDirectives(path2, definition);
1211
- splitBlock(ctx, path2, definition);
1212
- }
1213
-
1214
1355
  // src/transform-call.ts
1215
- var t7 = __toESM(require("@babel/types"), 1);
1216
1356
  function getFunctionDefinitionFromPropName(definitions, propName) {
1217
1357
  for (let i = 0, len = definitions.length; i < len; i++) {
1218
1358
  const def = definitions[i];
@@ -1227,7 +1367,7 @@ function getFunctionDefinitionFromPropName(definitions, propName) {
1227
1367
  }
1228
1368
  function getFunctionDefinitionFromCallee(ctx, path2) {
1229
1369
  const callee = path2.node.callee;
1230
- const id = unwrapNode(callee, t7.isIdentifier);
1370
+ const id = unwrapNode(callee, t12.isIdentifier);
1231
1371
  if (id) {
1232
1372
  const binding = path2.scope.getBindingIdentifier(id.name);
1233
1373
  if (binding) {
@@ -1235,9 +1375,9 @@ function getFunctionDefinitionFromCallee(ctx, path2) {
1235
1375
  }
1236
1376
  return void 0;
1237
1377
  }
1238
- const memberExpr = unwrapNode(callee, t7.isMemberExpression);
1239
- if (memberExpr && !memberExpr.computed && t7.isIdentifier(memberExpr.property)) {
1240
- const object = unwrapNode(memberExpr.object, t7.isIdentifier);
1378
+ const memberExpr = unwrapNode(callee, t12.isMemberExpression);
1379
+ if (memberExpr && !memberExpr.computed && t12.isIdentifier(memberExpr.property)) {
1380
+ const object = unwrapNode(memberExpr.object, t12.isIdentifier);
1241
1381
  if (object) {
1242
1382
  const binding = path2.scope.getBindingIdentifier(object.name);
1243
1383
  if (binding) {
@@ -1254,7 +1394,7 @@ function getFunctionDefinitionFromCallee(ctx, path2) {
1254
1394
  return void 0;
1255
1395
  }
1256
1396
  function isValidFunction(node) {
1257
- return t7.isArrowFunctionExpression(node) || t7.isFunctionExpression(node);
1397
+ return t12.isArrowFunctionExpression(node) || t12.isFunctionExpression(node);
1258
1398
  }
1259
1399
  function isSkippableFunction(node) {
1260
1400
  if (node.leadingComments) {
@@ -1273,23 +1413,369 @@ function transformCall(ctx, path2) {
1273
1413
  }
1274
1414
  const args = path2.get("arguments");
1275
1415
  const expr = args[0];
1276
- if (isPathValid(expr, t7.isExpression)) {
1277
- if (isSkippableFunction(expr.node)) {
1416
+ if (isPathValid(expr, t12.isExpression) && !isSkippableFunction(expr.node)) {
1417
+ const replacement = isPathValid(expr, isValidFunction) ? splitFunctionFromCall(ctx, expr, definition) : splitExpressionFromCall(ctx, expr, definition);
1418
+ path2.replaceWith(
1419
+ t12.callExpression(getImportIdentifier(ctx, path2, definition.handle), [
1420
+ t12.addComment(replacement, "leading", "@dismantle skip")
1421
+ ])
1422
+ );
1423
+ }
1424
+ }
1425
+
1426
+ // src/transform-function-directive.ts
1427
+ var t14 = __toESM(require("@babel/types"), 1);
1428
+
1429
+ // src/split-function-directive.ts
1430
+ var t13 = __toESM(require("@babel/types"), 1);
1431
+ function transformFunctionContent2(path2, mutations) {
1432
+ const target = path2.scope.getFunctionParent() || path2.scope.getProgramParent();
1433
+ const applyMutations = mutations.length ? generateUniqueName(path2, "mutate") : void 0;
1434
+ path2.traverse({
1435
+ ReturnStatement(child) {
1436
+ const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
1437
+ if (parent === target) {
1438
+ const replacement = [RETURN_KEY];
1439
+ if (child.node.argument) {
1440
+ replacement.push(child.node.argument);
1441
+ } else {
1442
+ replacement.push(t13.nullLiteral());
1443
+ }
1444
+ if (applyMutations) {
1445
+ replacement.push(t13.callExpression(applyMutations, []));
1446
+ }
1447
+ child.replaceWith(t13.returnStatement(t13.arrayExpression(replacement)));
1448
+ child.skip();
1449
+ }
1450
+ }
1451
+ });
1452
+ const error = generateUniqueName(path2, "error");
1453
+ const throwResult = [THROW_KEY, error];
1454
+ const haltResult = [NO_HALT_KEY];
1455
+ if (applyMutations) {
1456
+ throwResult.push(t13.callExpression(applyMutations, []));
1457
+ haltResult.push(t13.nullLiteral());
1458
+ haltResult.push(t13.callExpression(applyMutations, []));
1459
+ }
1460
+ const statements = [
1461
+ t13.tryStatement(
1462
+ t13.blockStatement(path2.node.body),
1463
+ t13.catchClause(
1464
+ error,
1465
+ t13.blockStatement([t13.returnStatement(t13.arrayExpression(throwResult))])
1466
+ )
1467
+ ),
1468
+ t13.returnStatement(t13.arrayExpression(haltResult))
1469
+ ];
1470
+ if (applyMutations) {
1471
+ statements.unshift(
1472
+ t13.variableDeclaration("const", [
1473
+ t13.variableDeclarator(
1474
+ applyMutations,
1475
+ t13.arrowFunctionExpression(
1476
+ [],
1477
+ t13.objectExpression(
1478
+ mutations.map((item) => t13.objectProperty(item, item, false, true))
1479
+ )
1480
+ )
1481
+ )
1482
+ ])
1483
+ );
1484
+ }
1485
+ path2.node.body = statements;
1486
+ }
1487
+ function getFunctionReplacement2(ctx, path2, entryFile, bindings) {
1488
+ const rest = generateUniqueName(path2, "rest");
1489
+ const returnType = generateUniqueName(path2, "type");
1490
+ const returnResult = generateUniqueName(path2, "result");
1491
+ const returnMutations = generateUniqueName(path2, "mutations");
1492
+ const source = generateUniqueName(path2, "source");
1493
+ const replacement = [];
1494
+ if (path2.node.generator) {
1495
+ const funcID = generateUniqueName(path2, "fn");
1496
+ replacement.push(
1497
+ t13.variableDeclaration("const", [
1498
+ t13.variableDeclarator(
1499
+ funcID,
1500
+ t13.callExpression(
1501
+ getImportIdentifier(ctx, path2, {
1502
+ ...HIDDEN_GENERATOR,
1503
+ source: ctx.options.runtime
1504
+ }),
1505
+ [
1506
+ source,
1507
+ bindings.mutations.length ? t13.arrowFunctionExpression(
1508
+ [returnMutations],
1509
+ t13.assignmentExpression(
1510
+ "=",
1511
+ t13.objectPattern(
1512
+ bindings.mutations.map(
1513
+ (item) => t13.objectProperty(item, item, false, true)
1514
+ )
1515
+ ),
1516
+ returnMutations
1517
+ )
1518
+ ) : t13.nullLiteral()
1519
+ ]
1520
+ )
1521
+ )
1522
+ ])
1523
+ );
1524
+ const [reps, step] = getGeneratorReplacementForBlock(path2, funcID, [
1525
+ t13.arrayExpression(bindings.locals),
1526
+ t13.spreadElement(rest)
1527
+ ]);
1528
+ for (let i = 0, len = reps.length; i < len; i++) {
1529
+ replacement.push(reps[i]);
1530
+ }
1531
+ replacement.push(
1532
+ t13.variableDeclaration("const", [
1533
+ t13.variableDeclarator(
1534
+ t13.arrayPattern([returnType, returnResult]),
1535
+ t13.memberExpression(step, t13.identifier("value"))
1536
+ )
1537
+ ])
1538
+ );
1539
+ } else {
1540
+ replacement.push(
1541
+ t13.variableDeclaration("const", [
1542
+ t13.variableDeclarator(
1543
+ t13.arrayPattern([returnType, returnResult]),
1544
+ t13.awaitExpression(
1545
+ t13.callExpression(
1546
+ t13.callExpression(
1547
+ getImportIdentifier(ctx, path2, {
1548
+ ...HIDDEN_FUNC,
1549
+ source: ctx.options.runtime
1550
+ }),
1551
+ [
1552
+ source,
1553
+ bindings.mutations.length ? t13.arrowFunctionExpression(
1554
+ [returnMutations],
1555
+ t13.assignmentExpression(
1556
+ "=",
1557
+ t13.objectPattern(
1558
+ bindings.mutations.map(
1559
+ (item) => t13.objectProperty(item, item, false, true)
1560
+ )
1561
+ ),
1562
+ returnMutations
1563
+ )
1564
+ ) : t13.nullLiteral()
1565
+ ]
1566
+ ),
1567
+ [t13.arrayExpression(bindings.locals), t13.spreadElement(rest)]
1568
+ )
1569
+ )
1570
+ )
1571
+ ])
1572
+ );
1573
+ }
1574
+ replacement.push(t13.returnStatement(returnResult));
1575
+ return t13.arrowFunctionExpression(
1576
+ [],
1577
+ t13.blockStatement([
1578
+ t13.variableDeclaration("const", [
1579
+ t13.variableDeclarator(
1580
+ source,
1581
+ t13.memberExpression(
1582
+ t13.awaitExpression(t13.importExpression(t13.stringLiteral(entryFile))),
1583
+ t13.identifier("default")
1584
+ )
1585
+ )
1586
+ ]),
1587
+ t13.returnStatement(
1588
+ isPathValid(path2, t13.isFunctionExpression) ? t13.functionExpression(
1589
+ path2.node.id,
1590
+ [t13.restElement(rest)],
1591
+ t13.blockStatement(replacement),
1592
+ path2.node.generator,
1593
+ true
1594
+ ) : t13.arrowFunctionExpression(
1595
+ [t13.restElement(rest)],
1596
+ t13.blockStatement(replacement),
1597
+ true
1598
+ )
1599
+ )
1600
+ ]),
1601
+ true
1602
+ );
1603
+ }
1604
+ function replaceIsomorphicFunction2(ctx, path2, directive, bindings) {
1605
+ const body = path2.get("body");
1606
+ if (isPathValid(body, t13.isExpression)) {
1607
+ body.replaceWith(t13.blockStatement([t13.returnStatement(body.node)]));
1608
+ }
1609
+ assert(isPathValid(body, t13.isBlockStatement), "invariant");
1610
+ const entryFile = createEntryFile(
1611
+ ctx,
1612
+ path2,
1613
+ createRootFile(
1614
+ ctx,
1615
+ bindings,
1616
+ t13.isFunctionExpression(path2.node) ? t13.functionExpression(
1617
+ path2.node.id,
1618
+ [t13.arrayPattern(bindings.locals), ...path2.node.params],
1619
+ path2.node.body,
1620
+ path2.node.generator,
1621
+ path2.node.async
1622
+ ) : t13.arrowFunctionExpression(
1623
+ [t13.arrayPattern(bindings.locals), ...path2.node.params],
1624
+ path2.node.body,
1625
+ path2.node.async
1626
+ )
1627
+ ),
1628
+ directive.target,
1629
+ directive.idPrefix
1630
+ );
1631
+ const source = generateUniqueName(path2, "source");
1632
+ const rest = generateUniqueName(path2, "rest");
1633
+ return t13.arrowFunctionExpression(
1634
+ [],
1635
+ t13.blockStatement([
1636
+ t13.variableDeclaration("const", [
1637
+ t13.variableDeclarator(
1638
+ source,
1639
+ t13.memberExpression(
1640
+ t13.awaitExpression(t13.importExpression(t13.stringLiteral(entryFile))),
1641
+ t13.identifier("default")
1642
+ )
1643
+ )
1644
+ ]),
1645
+ t13.returnStatement(
1646
+ t13.arrowFunctionExpression(
1647
+ [t13.restElement(rest)],
1648
+ t13.callExpression(source, [
1649
+ t13.arrayExpression(bindings.locals),
1650
+ t13.spreadElement(rest)
1651
+ ]),
1652
+ path2.node.async
1653
+ )
1654
+ )
1655
+ ]),
1656
+ true
1657
+ );
1658
+ }
1659
+ function replaceFunctionDirective(ctx, path2, directive, bindings) {
1660
+ const body = path2.get("body");
1661
+ if (isPathValid(body, t13.isExpression)) {
1662
+ body.replaceWith(t13.blockStatement([t13.returnStatement(body.node)]));
1663
+ }
1664
+ assert(isPathValid(body, t13.isBlockStatement), "invariant");
1665
+ transformFunctionContent2(body, bindings.mutations);
1666
+ const entryFile = createEntryFile(
1667
+ ctx,
1668
+ path2,
1669
+ ctx.options.mode === "server" || directive.isomorphic ? createRootFile(
1670
+ ctx,
1671
+ bindings,
1672
+ t13.isFunctionExpression(path2.node) ? t13.functionExpression(
1673
+ path2.node.id,
1674
+ [t13.arrayPattern(bindings.locals), ...path2.node.params],
1675
+ path2.node.body,
1676
+ path2.node.generator,
1677
+ path2.node.async
1678
+ ) : t13.arrowFunctionExpression(
1679
+ [t13.arrayPattern(bindings.locals), ...path2.node.params],
1680
+ path2.node.body,
1681
+ path2.node.async
1682
+ )
1683
+ ) : void 0,
1684
+ directive.target,
1685
+ directive.idPrefix
1686
+ );
1687
+ return getFunctionReplacement2(ctx, path2, entryFile, bindings);
1688
+ }
1689
+ function splitFunctionDirective(ctx, path2, directive) {
1690
+ const bindings = extractBindings(
1691
+ ctx,
1692
+ path2,
1693
+ getForeignBindings(path2, "function"),
1694
+ directive.pure
1695
+ );
1696
+ if (directive.isomorphic) {
1697
+ return replaceIsomorphicFunction2(ctx, path2, directive, bindings);
1698
+ }
1699
+ return replaceFunctionDirective(ctx, path2, directive, bindings);
1700
+ }
1701
+
1702
+ // src/transform-function-directive.ts
1703
+ function transformFunctionDirective(ctx, path2) {
1704
+ const body = path2.get("body");
1705
+ if (isPathValid(body, t14.isBlockStatement)) {
1706
+ const definition = getDirectiveDefinitionFromBlock(ctx, body);
1707
+ if (!definition || definition.type !== "function-directive") {
1278
1708
  return;
1279
1709
  }
1280
- const replacement = isPathValid(expr, isValidFunction) ? splitFunction(ctx, expr, definition) : splitExpression(ctx, expr, definition);
1281
- if (definition.preserve) {
1282
- expr.replaceWith(t7.addComment(replacement, "leading", "@dismantle skip"));
1283
- } else {
1284
- path2.replaceWith(replacement);
1710
+ cleanBlockForDirectives(body, definition);
1711
+ cleanBlockForFauxDirectives(body, definition);
1712
+ const replacement = splitFunctionDirective(ctx, path2, definition);
1713
+ path2.replaceWith(
1714
+ t14.callExpression(getImportIdentifier(ctx, path2, definition.handle), [
1715
+ replacement
1716
+ ])
1717
+ );
1718
+ }
1719
+ }
1720
+
1721
+ // src/utils/bubble-function-declaration.ts
1722
+ var t15 = __toESM(require("@babel/types"), 1);
1723
+
1724
+ // src/utils/is-statement-top-level.ts
1725
+ function isStatementTopLevel(path2) {
1726
+ let blockParent = path2.scope.getBlockParent();
1727
+ const programParent = path2.scope.getProgramParent();
1728
+ if (blockParent.path === path2) {
1729
+ blockParent = blockParent.parent;
1730
+ }
1731
+ return programParent === blockParent;
1732
+ }
1733
+
1734
+ // src/utils/bubble-function-declaration.ts
1735
+ function bubbleFunctionDeclaration(ctx, program2, path2) {
1736
+ if (isStatementTopLevel(path2)) {
1737
+ const decl = path2.node;
1738
+ if (decl.id) {
1739
+ const definition = getDirectiveDefinitionFromBlock(ctx, path2.get("body"));
1740
+ if (!definition || definition.type !== "function-directive") {
1741
+ return;
1742
+ }
1743
+ const [tmp] = program2.unshiftContainer(
1744
+ "body",
1745
+ t15.variableDeclaration("const", [
1746
+ t15.variableDeclarator(
1747
+ decl.id,
1748
+ t15.functionExpression(
1749
+ decl.id,
1750
+ decl.params,
1751
+ decl.body,
1752
+ decl.generator,
1753
+ decl.async
1754
+ )
1755
+ )
1756
+ ])
1757
+ );
1758
+ program2.scope.registerDeclaration(tmp);
1759
+ tmp.skip();
1760
+ if (path2.parentPath.isExportNamedDeclaration()) {
1761
+ path2.parentPath.replaceWith(
1762
+ t15.exportNamedDeclaration(void 0, [
1763
+ t15.exportSpecifier(decl.id, decl.id)
1764
+ ])
1765
+ );
1766
+ } else if (path2.parentPath.isExportDefaultDeclaration()) {
1767
+ path2.replaceWith(decl.id);
1768
+ } else {
1769
+ path2.remove();
1770
+ }
1285
1771
  }
1286
1772
  }
1287
1773
  }
1288
1774
 
1289
1775
  // src/utils/register-import-specifiers.ts
1290
- var t8 = __toESM(require("@babel/types"), 1);
1776
+ var t16 = __toESM(require("@babel/types"), 1);
1291
1777
  function registerImportSpecifier(ctx, node, definition) {
1292
- if (t8.isImportSpecifier(node)) {
1778
+ if (t16.isImportSpecifier(node)) {
1293
1779
  if (node.importKind === "type" || node.importKind === "typeof") {
1294
1780
  return;
1295
1781
  }
@@ -1298,10 +1784,10 @@ function registerImportSpecifier(ctx, node, definition) {
1298
1784
  ctx.registrations.identifiers.set(node.local, definition);
1299
1785
  }
1300
1786
  }
1301
- if (t8.isImportDefaultSpecifier(node) && definition.source.kind === "default") {
1787
+ if (t16.isImportDefaultSpecifier(node) && definition.source.kind === "default") {
1302
1788
  ctx.registrations.identifiers.set(node.local, definition);
1303
1789
  }
1304
- if (t8.isImportNamespaceSpecifier(node)) {
1790
+ if (t16.isImportNamespaceSpecifier(node)) {
1305
1791
  let current = ctx.registrations.namespaces.get(node.local);
1306
1792
  if (!current) {
1307
1793
  current = [];
@@ -1317,7 +1803,7 @@ function registerImportDeclarationByDefinition(ctx, path2, definition) {
1317
1803
  }
1318
1804
  }
1319
1805
  function registerImportSpecifiers(ctx, programPath) {
1320
- const len = ctx.options.functions.length;
1806
+ const len = ctx.options.definitions.length;
1321
1807
  if (!len) {
1322
1808
  return;
1323
1809
  }
@@ -1327,8 +1813,8 @@ function registerImportSpecifiers(ctx, programPath) {
1327
1813
  return;
1328
1814
  }
1329
1815
  for (let i = 0; i < len; i++) {
1330
- const func = ctx.options.functions[i];
1331
- if (func.source.source === path2.node.source.value) {
1816
+ const func = ctx.options.definitions[i];
1817
+ if (func.type === "function-call" && func.source.source === path2.node.source.value) {
1332
1818
  registerImportDeclarationByDefinition(ctx, path2, func);
1333
1819
  }
1334
1820
  }
@@ -1341,11 +1827,22 @@ function plugin() {
1341
1827
  return {
1342
1828
  name: "dismantle",
1343
1829
  visitor: {
1344
- Program(path2, ctx) {
1345
- registerImportSpecifiers(ctx.opts, path2);
1830
+ Program(programPath, ctx) {
1831
+ registerImportSpecifiers(ctx.opts, programPath);
1832
+ programPath.traverse({
1833
+ FunctionDeclaration(path2) {
1834
+ bubbleFunctionDeclaration(ctx.opts, programPath, path2);
1835
+ }
1836
+ });
1837
+ },
1838
+ ArrowFunctionExpression(path2, ctx) {
1839
+ transformFunctionDirective(ctx.opts, path2);
1840
+ },
1841
+ FunctionExpression(path2, ctx) {
1842
+ transformFunctionDirective(ctx.opts, path2);
1346
1843
  },
1347
1844
  BlockStatement(path2, ctx) {
1348
- transformBlock(ctx.opts, path2);
1845
+ transformBlockDirective(ctx.opts, path2);
1349
1846
  },
1350
1847
  CallExpression(path2, ctx) {
1351
1848
  transformCall(ctx.opts, path2);
@@ -1469,7 +1966,19 @@ async function compile(id, code, options) {
1469
1966
  namespaces: /* @__PURE__ */ new Map()
1470
1967
  }
1471
1968
  };
1472
- const plugins = ["jsx"];
1969
+ const plugins = [
1970
+ "jsx",
1971
+ // import { example } from 'example' with { example: true };
1972
+ "importAttributes",
1973
+ // () => throw example
1974
+ "throwExpressions",
1975
+ // You know what this is
1976
+ "decorators",
1977
+ // const { #example: example } = this;
1978
+ "destructuringPrivate",
1979
+ // using example = myExample()
1980
+ "explicitResourceManagement"
1981
+ ];
1473
1982
  if (/\.[mc]?tsx?$/i.test(id)) {
1474
1983
  plugins.push("typescript");
1475
1984
  }