dismantle 0.2.2 → 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.
Files changed (62) hide show
  1. package/dist/cjs/development/index.cjs +917 -452
  2. package/dist/cjs/development/index.cjs.map +4 -4
  3. package/dist/cjs/development/runtime.cjs +917 -452
  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 +917 -452
  8. package/dist/esm/development/index.mjs.map +4 -4
  9. package/dist/esm/development/runtime.mjs +917 -452
  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/index.d.ts +1 -1
  14. package/dist/types/index.d.ts.map +1 -1
  15. package/dist/types/plugin.d.ts.map +1 -1
  16. package/dist/types/split-block-directive.d.ts +5 -0
  17. package/dist/types/split-block-directive.d.ts.map +1 -0
  18. package/dist/types/split-call.d.ts +6 -0
  19. package/dist/types/split-call.d.ts.map +1 -0
  20. package/dist/types/split-function-directive.d.ts +5 -0
  21. package/dist/types/split-function-directive.d.ts.map +1 -0
  22. package/dist/types/split.d.ts +23 -4
  23. package/dist/types/split.d.ts.map +1 -1
  24. package/dist/types/transform-block-directive.d.ts +5 -0
  25. package/dist/types/transform-block-directive.d.ts.map +1 -0
  26. package/dist/types/transform-call.d.ts.map +1 -1
  27. package/dist/types/transform-function-directive.d.ts +5 -0
  28. package/dist/types/transform-function-directive.d.ts.map +1 -0
  29. package/dist/types/types.d.ts +21 -9
  30. package/dist/types/types.d.ts.map +1 -1
  31. package/dist/types/utils/bubble-function-declaration.d.ts +5 -0
  32. package/dist/types/utils/bubble-function-declaration.d.ts.map +1 -0
  33. package/dist/types/utils/directive-check.d.ts +7 -0
  34. package/dist/types/utils/directive-check.d.ts.map +1 -0
  35. package/dist/types/utils/generate-unique-name.d.ts +4 -0
  36. package/dist/types/utils/generate-unique-name.d.ts.map +1 -0
  37. package/dist/types/utils/get-import-identifier.d.ts.map +1 -1
  38. package/dist/types/utils/get-import-specifier-name.d.ts +1 -1
  39. package/dist/types/utils/get-import-specifier-name.d.ts.map +1 -1
  40. package/dist/types/utils/is-statement-top-level.d.ts +4 -0
  41. package/dist/types/utils/is-statement-top-level.d.ts.map +1 -0
  42. package/dist/types/utils/register-import-specifiers.d.ts.map +1 -1
  43. package/package.json +2 -2
  44. package/src/index.ts +16 -2
  45. package/src/plugin.ts +17 -4
  46. package/src/split-block-directive.ts +429 -0
  47. package/src/split-call.ts +395 -0
  48. package/src/split-function-directive.ts +335 -0
  49. package/src/split.ts +21 -727
  50. package/src/transform-block-directive.ts +22 -0
  51. package/src/transform-call.ts +14 -16
  52. package/src/transform-function-directive.ts +32 -0
  53. package/src/types.ts +29 -9
  54. package/src/utils/bubble-function-declaration.ts +50 -0
  55. package/src/{transform-block.ts → utils/directive-check.ts} +21 -29
  56. package/src/utils/generate-unique-name.ts +25 -0
  57. package/src/utils/get-import-identifier.ts +14 -16
  58. package/src/utils/get-import-specifier-name.ts +4 -6
  59. package/src/utils/is-statement-top-level.ts +15 -0
  60. package/src/utils/register-import-specifiers.ts +9 -6
  61. package/dist/types/transform-block.d.ts +0 -5
  62. package/dist/types/transform-block.d.ts.map +0 -1
@@ -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(
@@ -449,7 +432,7 @@ function createEntryFile(ctx, path2, rootFile, imported) {
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,33 @@ 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")
812
+ t9.callExpression(getImportIdentifier(ctx, path2, HIDDEN_GENERATOR), [
813
+ t9.memberExpression(
814
+ t9.awaitExpression(t9.importExpression(t9.stringLiteral(entryFile))),
815
+ t9.identifier("default")
805
816
  ),
806
- bindings.mutations.length ? t6.arrowFunctionExpression(
817
+ bindings.mutations.length ? t9.arrowFunctionExpression(
807
818
  [returnMutations],
808
- t6.assignmentExpression(
819
+ t9.assignmentExpression(
809
820
  "=",
810
- t6.objectPattern(
821
+ t9.objectPattern(
811
822
  bindings.mutations.map(
812
- (item) => t6.objectProperty(item, item, false, true)
823
+ (item) => t9.objectProperty(item, item, false, true)
813
824
  )
814
825
  ),
815
826
  returnMutations
816
827
  )
817
- ) : t6.nullLiteral()
828
+ ) : t9.nullLiteral()
818
829
  ])
819
830
  )
820
831
  ])
821
832
  );
822
- const [reps, step] = getGeneratorReplacementForServerBlock(
833
+ const [reps, step] = getGeneratorReplacementForBlock(
823
834
  path2,
824
835
  blockID,
825
836
  bindings.locals
@@ -828,39 +839,39 @@ function getBlockReplacement(ctx, path2, entryFile, bindings, halting) {
828
839
  replacement.push(reps[i]);
829
840
  }
830
841
  replacement.push(
831
- t6.variableDeclaration("const", [
832
- t6.variableDeclarator(
833
- t6.arrayPattern([returnType, returnResult]),
834
- t6.memberExpression(step, t6.identifier("value"))
842
+ t9.variableDeclaration("const", [
843
+ t9.variableDeclarator(
844
+ t9.arrayPattern([returnType, returnResult]),
845
+ t9.memberExpression(step, t9.identifier("value"))
835
846
  )
836
847
  ])
837
848
  );
838
849
  } else {
839
850
  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))
851
+ t9.variableDeclaration("const", [
852
+ t9.variableDeclarator(
853
+ t9.arrayPattern([returnType, returnResult]),
854
+ t9.awaitExpression(
855
+ t9.callExpression(
856
+ t9.callExpression(getImportIdentifier(ctx, path2, HIDDEN_FUNC), [
857
+ t9.memberExpression(
858
+ t9.awaitExpression(
859
+ t9.importExpression(t9.stringLiteral(entryFile))
849
860
  ),
850
- t6.identifier("default")
861
+ t9.identifier("default")
851
862
  ),
852
- bindings.mutations.length ? t6.arrowFunctionExpression(
863
+ bindings.mutations.length ? t9.arrowFunctionExpression(
853
864
  [returnMutations],
854
- t6.assignmentExpression(
865
+ t9.assignmentExpression(
855
866
  "=",
856
- t6.objectPattern(
867
+ t9.objectPattern(
857
868
  bindings.mutations.map(
858
- (item) => t6.objectProperty(item, item, false, true)
869
+ (item) => t9.objectProperty(item, item, false, true)
859
870
  )
860
871
  ),
861
872
  returnMutations
862
873
  )
863
- ) : t6.nullLiteral()
874
+ ) : t9.nullLiteral()
864
875
  ]),
865
876
  bindings.locals
866
877
  )
@@ -872,28 +883,32 @@ function getBlockReplacement(ctx, path2, entryFile, bindings, halting) {
872
883
  if (check) {
873
884
  replacement.push(check);
874
885
  }
875
- return t6.blockStatement(replacement);
886
+ return t9.blockStatement(replacement);
876
887
  }
877
- function replaceBlock(ctx, path2, directive, bindings) {
888
+ function replaceBlockDirective(ctx, path2, directive, bindings) {
878
889
  const halting = transformBlockContent(path2, bindings.mutations);
879
- const rootFile = createRootFile(
890
+ const entryFile = createEntryFile(
880
891
  ctx,
881
- bindings,
882
- t6.functionExpression(
883
- void 0,
884
- bindings.locals,
885
- t6.blockStatement(path2.node.body),
886
- halting.hasYield,
887
- true
888
- )
892
+ path2,
893
+ ctx.options.mode === "server" || directive.isomorphic ? createRootFile(
894
+ ctx,
895
+ bindings,
896
+ t9.functionExpression(
897
+ void 0,
898
+ bindings.locals,
899
+ t9.blockStatement(path2.node.body),
900
+ halting.hasYield,
901
+ true
902
+ )
903
+ ) : void 0,
904
+ directive.target
889
905
  );
890
- const entryFile = createEntryFile(ctx, path2, rootFile, directive.target);
891
906
  path2.replaceWith(
892
- getBlockReplacement(ctx, path2, entryFile, bindings, halting)
907
+ getBlockDirectiveReplacement(ctx, path2, entryFile, bindings, halting)
893
908
  );
894
909
  }
895
- function splitBlock(ctx, path2, directive) {
896
- replaceBlock(
910
+ function splitBlockDirective(ctx, path2, directive) {
911
+ replaceBlockDirective(
897
912
  ctx,
898
913
  path2,
899
914
  directive,
@@ -905,63 +920,154 @@ function splitBlock(ctx, path2, directive) {
905
920
  )
906
921
  );
907
922
  }
923
+
924
+ // src/utils/directive-check.ts
925
+ var t10 = __toESM(require("@babel/types"), 1);
926
+ function getValidDirectiveFromString(ctx, string) {
927
+ for (let i = 0, len = ctx.options.definitions.length; i < len; i++) {
928
+ const current = ctx.options.definitions[i];
929
+ switch (current.type) {
930
+ case "block-directive":
931
+ case "function-directive": {
932
+ if (current.directive === string) {
933
+ return current;
934
+ }
935
+ break;
936
+ }
937
+ }
938
+ }
939
+ return void 0;
940
+ }
941
+ function getDefinitionFromDirectives(ctx, path2) {
942
+ for (let i = 0, len = path2.node.directives.length; i < len; i++) {
943
+ const statement = path2.node.directives[i].value.value;
944
+ const directive = getValidDirectiveFromString(ctx, statement);
945
+ if (directive) {
946
+ return directive;
947
+ }
948
+ }
949
+ return void 0;
950
+ }
951
+ function getDefinitionFromFauxDirectives(ctx, path2) {
952
+ for (let i = 0, len = path2.node.body.length; i < len; i++) {
953
+ const statement = path2.node.body[i];
954
+ if (t10.isExpressionStatement(statement) && t10.isStringLiteral(statement.expression)) {
955
+ const directive = getValidDirectiveFromString(
956
+ ctx,
957
+ statement.expression.value
958
+ );
959
+ if (directive) {
960
+ return directive;
961
+ }
962
+ } else {
963
+ break;
964
+ }
965
+ }
966
+ return void 0;
967
+ }
968
+ function getDirectiveDefinitionFromBlock(ctx, path2) {
969
+ const parent = path2.getFunctionParent();
970
+ if (parent && !parent.node.async) {
971
+ return void 0;
972
+ }
973
+ return getDefinitionFromDirectives(ctx, path2) || getDefinitionFromFauxDirectives(ctx, path2);
974
+ }
975
+ function cleanBlockForDirectives(path2, definition) {
976
+ const newDirectives = [];
977
+ for (let i = 0, len = path2.node.directives.length; i < len; i++) {
978
+ const current = path2.node.directives[i];
979
+ if (current.value.value !== definition.directive) {
980
+ newDirectives.push(current);
981
+ }
982
+ }
983
+ path2.node.directives = newDirectives;
984
+ }
985
+ function cleanBlockForFauxDirectives(path2, definition) {
986
+ const body = path2.get("body");
987
+ for (let i = 0, len = body.length; i < len; i++) {
988
+ const statement = body[i];
989
+ if (t10.isExpressionStatement(statement.node) && t10.isStringLiteral(statement.node.expression)) {
990
+ if (statement.node.expression.value === definition.directive) {
991
+ statement.remove();
992
+ return;
993
+ }
994
+ }
995
+ }
996
+ }
997
+
998
+ // src/transform-block-directive.ts
999
+ function transformBlockDirective(ctx, path2) {
1000
+ const definition = getDirectiveDefinitionFromBlock(ctx, path2);
1001
+ if (!definition || definition.type !== "block-directive") {
1002
+ return;
1003
+ }
1004
+ cleanBlockForDirectives(path2, definition);
1005
+ cleanBlockForFauxDirectives(path2, definition);
1006
+ splitBlockDirective(ctx, path2, definition);
1007
+ }
1008
+
1009
+ // src/transform-call.ts
1010
+ var t12 = __toESM(require("@babel/types"), 1);
1011
+
1012
+ // src/split-call.ts
1013
+ var t11 = __toESM(require("@babel/types"), 1);
1014
+
1015
+ // src/utils/assert.ts
1016
+ function assert(cond, error) {
1017
+ if (!cond) {
1018
+ throw new Error(error);
1019
+ }
1020
+ }
1021
+
1022
+ // src/split-call.ts
908
1023
  function transformFunctionContent(path2, mutations) {
909
1024
  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;
1025
+ const applyMutations = mutations.length ? generateUniqueName(path2, "mutate") : void 0;
913
1026
  path2.traverse({
914
1027
  ReturnStatement(child) {
915
1028
  const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
916
1029
  if (parent === target) {
917
- hasReturn = true;
918
1030
  const replacement = [RETURN_KEY];
919
1031
  if (child.node.argument) {
920
1032
  replacement.push(child.node.argument);
921
1033
  } else {
922
- replacement.push(t6.nullLiteral());
1034
+ replacement.push(t11.nullLiteral());
923
1035
  }
924
1036
  if (applyMutations) {
925
- replacement.push(t6.callExpression(applyMutations, []));
1037
+ replacement.push(t11.callExpression(applyMutations, []));
926
1038
  }
927
- child.replaceWith(t6.returnStatement(t6.arrayExpression(replacement)));
1039
+ child.replaceWith(t11.returnStatement(t11.arrayExpression(replacement)));
928
1040
  child.skip();
929
1041
  }
930
- },
931
- YieldExpression(child) {
932
- const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
933
- if (parent === target) {
934
- hasYield = true;
935
- }
936
1042
  }
937
1043
  });
938
- const error = path2.scope.generateUidIdentifier("error");
1044
+ const error = generateUniqueName(path2, "error");
939
1045
  const throwResult = [THROW_KEY, error];
940
1046
  const haltResult = [NO_HALT_KEY];
941
1047
  if (applyMutations) {
942
- throwResult.push(t6.callExpression(applyMutations, []));
943
- haltResult.push(t6.nullLiteral());
944
- haltResult.push(t6.callExpression(applyMutations, []));
1048
+ throwResult.push(t11.callExpression(applyMutations, []));
1049
+ haltResult.push(t11.nullLiteral());
1050
+ haltResult.push(t11.callExpression(applyMutations, []));
945
1051
  }
946
1052
  const statements = [
947
- t6.tryStatement(
948
- t6.blockStatement(path2.node.body),
949
- t6.catchClause(
1053
+ t11.tryStatement(
1054
+ t11.blockStatement(path2.node.body),
1055
+ t11.catchClause(
950
1056
  error,
951
- t6.blockStatement([t6.returnStatement(t6.arrayExpression(throwResult))])
1057
+ t11.blockStatement([t11.returnStatement(t11.arrayExpression(throwResult))])
952
1058
  )
953
1059
  ),
954
- t6.returnStatement(t6.arrayExpression(haltResult))
1060
+ t11.returnStatement(t11.arrayExpression(haltResult))
955
1061
  ];
956
1062
  if (applyMutations) {
957
1063
  statements.unshift(
958
- t6.variableDeclaration("const", [
959
- t6.variableDeclarator(
1064
+ t11.variableDeclaration("const", [
1065
+ t11.variableDeclarator(
960
1066
  applyMutations,
961
- t6.arrowFunctionExpression(
1067
+ t11.arrowFunctionExpression(
962
1068
  [],
963
- t6.objectExpression(
964
- mutations.map((item) => t6.objectProperty(item, item, false, true))
1069
+ t11.objectExpression(
1070
+ mutations.map((item) => t11.objectProperty(item, item, false, true))
965
1071
  )
966
1072
  )
967
1073
  )
@@ -969,160 +1075,241 @@ function transformFunctionContent(path2, mutations) {
969
1075
  );
970
1076
  }
971
1077
  path2.node.body = statements;
972
- return {
973
- hasReturn,
974
- hasYield
975
- };
976
1078
  }
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");
1079
+ function getFunctionReplacement(ctx, path2, entryFile, bindings) {
1080
+ const rest = generateUniqueName(path2, "rest");
1081
+ const returnType = generateUniqueName(path2, "type");
1082
+ const returnResult = generateUniqueName(path2, "result");
1083
+ const returnMutations = generateUniqueName(path2, "mutations");
1084
+ const source = generateUniqueName(path2, "source");
982
1085
  const replacement = [];
983
- if (halting.hasYield) {
984
- const funcID = path2.scope.generateUidIdentifier("fn");
1086
+ if (path2.node.generator) {
1087
+ const funcID = generateUniqueName(path2, "fn");
985
1088
  replacement.push(
986
- t6.variableDeclaration("const", [
987
- t6.variableDeclarator(
1089
+ t11.variableDeclaration("const", [
1090
+ t11.variableDeclarator(
988
1091
  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(
1092
+ t11.callExpression(getImportIdentifier(ctx, path2, HIDDEN_GENERATOR), [
1093
+ source,
1094
+ bindings.mutations.length ? t11.arrowFunctionExpression(
995
1095
  [returnMutations],
996
- t6.assignmentExpression(
1096
+ t11.assignmentExpression(
997
1097
  "=",
998
- t6.objectPattern(
1098
+ t11.objectPattern(
999
1099
  bindings.mutations.map(
1000
- (item) => t6.objectProperty(item, item, false, true)
1100
+ (item) => t11.objectProperty(item, item, false, true)
1001
1101
  )
1002
1102
  ),
1003
1103
  returnMutations
1004
1104
  )
1005
- ) : t6.nullLiteral()
1105
+ ) : t11.nullLiteral()
1006
1106
  ])
1007
1107
  )
1008
1108
  ])
1009
1109
  );
1010
- const [reps, step] = getGeneratorReplacementForServerBlock(path2, funcID, [
1011
- t6.arrayExpression(bindings.locals),
1012
- t6.spreadElement(rest)
1110
+ const [reps, step] = getGeneratorReplacementForBlock(path2, funcID, [
1111
+ t11.arrayExpression(bindings.locals),
1112
+ t11.spreadElement(rest)
1013
1113
  ]);
1014
1114
  for (let i = 0, len = reps.length; i < len; i++) {
1015
1115
  replacement.push(reps[i]);
1016
1116
  }
1017
1117
  replacement.push(
1018
- t6.variableDeclaration("const", [
1019
- t6.variableDeclarator(
1020
- t6.arrayPattern([returnType, returnResult]),
1021
- t6.memberExpression(step, t6.identifier("value"))
1118
+ t11.variableDeclaration("const", [
1119
+ t11.variableDeclarator(
1120
+ t11.arrayPattern([returnType, returnResult]),
1121
+ t11.memberExpression(step, t11.identifier("value"))
1022
1122
  )
1023
1123
  ])
1024
1124
  );
1025
1125
  } else {
1026
1126
  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(
1127
+ t11.variableDeclaration("const", [
1128
+ t11.variableDeclarator(
1129
+ t11.arrayPattern([returnType, returnResult]),
1130
+ t11.awaitExpression(
1131
+ t11.callExpression(
1132
+ t11.callExpression(getImportIdentifier(ctx, path2, HIDDEN_FUNC), [
1133
+ source,
1134
+ bindings.mutations.length ? t11.arrowFunctionExpression(
1040
1135
  [returnMutations],
1041
- t6.assignmentExpression(
1136
+ t11.assignmentExpression(
1042
1137
  "=",
1043
- t6.objectPattern(
1138
+ t11.objectPattern(
1044
1139
  bindings.mutations.map(
1045
- (item) => t6.objectProperty(item, item, false, true)
1140
+ (item) => t11.objectProperty(item, item, false, true)
1046
1141
  )
1047
1142
  ),
1048
1143
  returnMutations
1049
1144
  )
1050
- ) : t6.nullLiteral()
1145
+ ) : t11.nullLiteral()
1051
1146
  ]),
1052
- [t6.arrayExpression(bindings.locals), t6.spreadElement(rest)]
1147
+ [t11.arrayExpression(bindings.locals), t11.spreadElement(rest)]
1053
1148
  )
1054
1149
  )
1055
1150
  )
1056
1151
  ])
1057
1152
  );
1058
1153
  }
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
- );
1154
+ replacement.push(t11.returnStatement(returnResult));
1155
+ return t11.arrowFunctionExpression(
1156
+ [],
1157
+ t11.blockStatement([
1158
+ t11.variableDeclaration("const", [
1159
+ t11.variableDeclarator(
1160
+ source,
1161
+ t11.memberExpression(
1162
+ t11.awaitExpression(t11.importExpression(t11.stringLiteral(entryFile))),
1163
+ t11.identifier("default")
1164
+ )
1165
+ )
1166
+ ]),
1167
+ t11.returnStatement(
1168
+ isPathValid(path2, t11.isFunctionExpression) ? t11.functionExpression(
1169
+ path2.node.id,
1170
+ [t11.restElement(rest)],
1171
+ t11.blockStatement(replacement),
1172
+ path2.node.generator,
1173
+ true
1174
+ ) : t11.arrowFunctionExpression(
1175
+ [t11.restElement(rest)],
1176
+ t11.blockStatement(replacement),
1177
+ true
1178
+ )
1179
+ )
1180
+ ]),
1181
+ true
1182
+ );
1183
+ }
1184
+ function replaceIsomorphicFunction(ctx, path2, func, bindings) {
1185
+ const body = path2.get("body");
1186
+ if (isPathValid(body, t11.isExpression)) {
1187
+ body.replaceWith(t11.blockStatement([t11.returnStatement(body.node)]));
1068
1188
  }
1069
- return t6.arrowFunctionExpression(
1070
- [t6.restElement(rest)],
1071
- t6.blockStatement(replacement),
1189
+ assert(isPathValid(body, t11.isBlockStatement), "invariant");
1190
+ const entryFile = createEntryFile(
1191
+ ctx,
1192
+ path2,
1193
+ createRootFile(
1194
+ ctx,
1195
+ bindings,
1196
+ t11.isFunctionExpression(path2.node) ? t11.functionExpression(
1197
+ path2.node.id,
1198
+ [t11.arrayPattern(bindings.locals), ...path2.node.params],
1199
+ path2.node.body,
1200
+ path2.node.generator,
1201
+ path2.node.async
1202
+ ) : t11.arrowFunctionExpression(
1203
+ [t11.arrayPattern(bindings.locals), ...path2.node.params],
1204
+ path2.node.body,
1205
+ path2.node.async
1206
+ )
1207
+ ),
1208
+ func.target
1209
+ );
1210
+ const source = generateUniqueName(path2, "source");
1211
+ const rest = generateUniqueName(path2, "rest");
1212
+ return t11.arrowFunctionExpression(
1213
+ [],
1214
+ t11.blockStatement([
1215
+ t11.variableDeclaration("const", [
1216
+ t11.variableDeclarator(
1217
+ source,
1218
+ t11.memberExpression(
1219
+ t11.awaitExpression(t11.importExpression(t11.stringLiteral(entryFile))),
1220
+ t11.identifier("default")
1221
+ )
1222
+ )
1223
+ ]),
1224
+ t11.returnStatement(
1225
+ t11.arrowFunctionExpression(
1226
+ [t11.restElement(rest)],
1227
+ t11.callExpression(source, [
1228
+ t11.arrayExpression(bindings.locals),
1229
+ t11.spreadElement(rest)
1230
+ ]),
1231
+ path2.node.async
1232
+ )
1233
+ )
1234
+ ]),
1072
1235
  true
1073
1236
  );
1074
1237
  }
1075
- function replaceFunction(ctx, path2, func, bindings) {
1238
+ function replaceFunctionFromCall(ctx, path2, func, bindings) {
1076
1239
  const body = path2.get("body");
1077
- if (isPathValid(body, t6.isExpression)) {
1078
- body.replaceWith(t6.blockStatement([t6.returnStatement(body.node)]));
1240
+ if (isPathValid(body, t11.isExpression)) {
1241
+ body.replaceWith(t11.blockStatement([t11.returnStatement(body.node)]));
1079
1242
  }
1080
- assert(isPathValid(body, t6.isBlockStatement), "invariant");
1081
- const halting = transformFunctionContent(body, bindings.mutations);
1082
- const rootFile = createRootFile(
1243
+ assert(isPathValid(body, t11.isBlockStatement), "invariant");
1244
+ transformFunctionContent(body, bindings.mutations);
1245
+ const entryFile = createEntryFile(
1083
1246
  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
- )
1247
+ path2,
1248
+ ctx.options.mode === "server" || func.isomorphic ? createRootFile(
1249
+ ctx,
1250
+ bindings,
1251
+ t11.isFunctionExpression(path2.node) ? t11.functionExpression(
1252
+ path2.node.id,
1253
+ [t11.arrayPattern(bindings.locals), ...path2.node.params],
1254
+ path2.node.body,
1255
+ path2.node.generator,
1256
+ path2.node.async
1257
+ ) : t11.arrowFunctionExpression(
1258
+ [t11.arrayPattern(bindings.locals), ...path2.node.params],
1259
+ path2.node.body,
1260
+ path2.node.async
1261
+ )
1262
+ ) : void 0,
1263
+ func.target
1096
1264
  );
1097
- const entryFile = createEntryFile(ctx, path2, rootFile, func.target);
1098
- return getFunctionReplacement(ctx, path2, entryFile, bindings, halting);
1265
+ return getFunctionReplacement(ctx, path2, entryFile, bindings);
1099
1266
  }
1100
- function splitFunction(ctx, path2, func) {
1101
- return replaceFunction(
1267
+ function splitFunctionFromCall(ctx, path2, func) {
1268
+ const bindings = extractBindings(
1102
1269
  ctx,
1103
1270
  path2,
1104
- func,
1105
- extractBindings(ctx, path2, getForeignBindings(path2, "function"), func.pure)
1271
+ getForeignBindings(path2, "function"),
1272
+ func.pure
1106
1273
  );
1274
+ if (func.isomorphic) {
1275
+ return replaceIsomorphicFunction(ctx, path2, func, bindings);
1276
+ }
1277
+ return replaceFunctionFromCall(ctx, path2, func, bindings);
1107
1278
  }
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
- ),
1279
+ function replaceExpressionFromCall(ctx, path2, func, bindings) {
1280
+ const entryFile = createEntryFile(
1281
+ ctx,
1282
+ path2,
1283
+ ctx.options.mode === "server" || func.isomorphic ? createRootFile(ctx, bindings, path2.node) : void 0,
1284
+ func.target
1285
+ );
1286
+ const rest = generateUniqueName(path2, "rest");
1287
+ const source = generateUniqueName(path2, "source");
1288
+ return t11.arrowFunctionExpression(
1289
+ [],
1290
+ t11.blockStatement([
1291
+ t11.variableDeclaration("const", [
1292
+ t11.variableDeclarator(
1293
+ source,
1294
+ t11.memberExpression(
1295
+ t11.awaitExpression(t11.importExpression(t11.stringLiteral(entryFile))),
1296
+ t11.identifier("default")
1297
+ )
1298
+ )
1299
+ ]),
1300
+ t11.returnStatement(
1301
+ t11.arrowFunctionExpression(
1302
+ [t11.restElement(rest)],
1303
+ t11.callExpression(source, [t11.spreadElement(rest)]),
1304
+ true
1305
+ )
1306
+ )
1307
+ ]),
1121
1308
  true
1122
1309
  );
1123
1310
  }
1124
- function splitExpression(ctx, path2, func) {
1125
- return replaceExpression(
1311
+ function splitExpressionFromCall(ctx, path2, func) {
1312
+ return replaceExpressionFromCall(
1126
1313
  ctx,
1127
1314
  path2,
1128
1315
  func,
@@ -1135,84 +1322,7 @@ function splitExpression(ctx, path2, func) {
1135
1322
  );
1136
1323
  }
1137
1324
 
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
1325
  // src/transform-call.ts
1215
- var t7 = __toESM(require("@babel/types"), 1);
1216
1326
  function getFunctionDefinitionFromPropName(definitions, propName) {
1217
1327
  for (let i = 0, len = definitions.length; i < len; i++) {
1218
1328
  const def = definitions[i];
@@ -1227,7 +1337,7 @@ function getFunctionDefinitionFromPropName(definitions, propName) {
1227
1337
  }
1228
1338
  function getFunctionDefinitionFromCallee(ctx, path2) {
1229
1339
  const callee = path2.node.callee;
1230
- const id = unwrapNode(callee, t7.isIdentifier);
1340
+ const id = unwrapNode(callee, t12.isIdentifier);
1231
1341
  if (id) {
1232
1342
  const binding = path2.scope.getBindingIdentifier(id.name);
1233
1343
  if (binding) {
@@ -1235,9 +1345,9 @@ function getFunctionDefinitionFromCallee(ctx, path2) {
1235
1345
  }
1236
1346
  return void 0;
1237
1347
  }
1238
- const memberExpr = unwrapNode(callee, t7.isMemberExpression);
1239
- if (memberExpr && !memberExpr.computed && t7.isIdentifier(memberExpr.property)) {
1240
- const object = unwrapNode(memberExpr.object, t7.isIdentifier);
1348
+ const memberExpr = unwrapNode(callee, t12.isMemberExpression);
1349
+ if (memberExpr && !memberExpr.computed && t12.isIdentifier(memberExpr.property)) {
1350
+ const object = unwrapNode(memberExpr.object, t12.isIdentifier);
1241
1351
  if (object) {
1242
1352
  const binding = path2.scope.getBindingIdentifier(object.name);
1243
1353
  if (binding) {
@@ -1254,7 +1364,7 @@ function getFunctionDefinitionFromCallee(ctx, path2) {
1254
1364
  return void 0;
1255
1365
  }
1256
1366
  function isValidFunction(node) {
1257
- return t7.isArrowFunctionExpression(node) || t7.isFunctionExpression(node);
1367
+ return t12.isArrowFunctionExpression(node) || t12.isFunctionExpression(node);
1258
1368
  }
1259
1369
  function isSkippableFunction(node) {
1260
1370
  if (node.leadingComments) {
@@ -1273,23 +1383,355 @@ function transformCall(ctx, path2) {
1273
1383
  }
1274
1384
  const args = path2.get("arguments");
1275
1385
  const expr = args[0];
1276
- if (isPathValid(expr, t7.isExpression)) {
1277
- if (isSkippableFunction(expr.node)) {
1386
+ if (isPathValid(expr, t12.isExpression) && !isSkippableFunction(expr.node)) {
1387
+ const replacement = isPathValid(expr, isValidFunction) ? splitFunctionFromCall(ctx, expr, definition) : splitExpressionFromCall(ctx, expr, definition);
1388
+ path2.replaceWith(
1389
+ t12.callExpression(getImportIdentifier(ctx, path2, definition.handle), [
1390
+ t12.addComment(replacement, "leading", "@dismantle skip")
1391
+ ])
1392
+ );
1393
+ }
1394
+ }
1395
+
1396
+ // src/transform-function-directive.ts
1397
+ var t14 = __toESM(require("@babel/types"), 1);
1398
+
1399
+ // src/split-function-directive.ts
1400
+ var t13 = __toESM(require("@babel/types"), 1);
1401
+ function transformFunctionContent2(path2, mutations) {
1402
+ const target = path2.scope.getFunctionParent() || path2.scope.getProgramParent();
1403
+ const applyMutations = mutations.length ? generateUniqueName(path2, "mutate") : void 0;
1404
+ path2.traverse({
1405
+ ReturnStatement(child) {
1406
+ const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
1407
+ if (parent === target) {
1408
+ const replacement = [RETURN_KEY];
1409
+ if (child.node.argument) {
1410
+ replacement.push(child.node.argument);
1411
+ } else {
1412
+ replacement.push(t13.nullLiteral());
1413
+ }
1414
+ if (applyMutations) {
1415
+ replacement.push(t13.callExpression(applyMutations, []));
1416
+ }
1417
+ child.replaceWith(t13.returnStatement(t13.arrayExpression(replacement)));
1418
+ child.skip();
1419
+ }
1420
+ }
1421
+ });
1422
+ const error = generateUniqueName(path2, "error");
1423
+ const throwResult = [THROW_KEY, error];
1424
+ const haltResult = [NO_HALT_KEY];
1425
+ if (applyMutations) {
1426
+ throwResult.push(t13.callExpression(applyMutations, []));
1427
+ haltResult.push(t13.nullLiteral());
1428
+ haltResult.push(t13.callExpression(applyMutations, []));
1429
+ }
1430
+ const statements = [
1431
+ t13.tryStatement(
1432
+ t13.blockStatement(path2.node.body),
1433
+ t13.catchClause(
1434
+ error,
1435
+ t13.blockStatement([t13.returnStatement(t13.arrayExpression(throwResult))])
1436
+ )
1437
+ ),
1438
+ t13.returnStatement(t13.arrayExpression(haltResult))
1439
+ ];
1440
+ if (applyMutations) {
1441
+ statements.unshift(
1442
+ t13.variableDeclaration("const", [
1443
+ t13.variableDeclarator(
1444
+ applyMutations,
1445
+ t13.arrowFunctionExpression(
1446
+ [],
1447
+ t13.objectExpression(
1448
+ mutations.map((item) => t13.objectProperty(item, item, false, true))
1449
+ )
1450
+ )
1451
+ )
1452
+ ])
1453
+ );
1454
+ }
1455
+ path2.node.body = statements;
1456
+ }
1457
+ function getFunctionReplacement2(ctx, path2, entryFile, bindings) {
1458
+ const rest = generateUniqueName(path2, "rest");
1459
+ const returnType = generateUniqueName(path2, "type");
1460
+ const returnResult = generateUniqueName(path2, "result");
1461
+ const returnMutations = generateUniqueName(path2, "mutations");
1462
+ const source = generateUniqueName(path2, "source");
1463
+ const replacement = [];
1464
+ if (path2.node.generator) {
1465
+ const funcID = generateUniqueName(path2, "fn");
1466
+ replacement.push(
1467
+ t13.variableDeclaration("const", [
1468
+ t13.variableDeclarator(
1469
+ funcID,
1470
+ t13.callExpression(getImportIdentifier(ctx, path2, HIDDEN_GENERATOR), [
1471
+ source,
1472
+ bindings.mutations.length ? t13.arrowFunctionExpression(
1473
+ [returnMutations],
1474
+ t13.assignmentExpression(
1475
+ "=",
1476
+ t13.objectPattern(
1477
+ bindings.mutations.map(
1478
+ (item) => t13.objectProperty(item, item, false, true)
1479
+ )
1480
+ ),
1481
+ returnMutations
1482
+ )
1483
+ ) : t13.nullLiteral()
1484
+ ])
1485
+ )
1486
+ ])
1487
+ );
1488
+ const [reps, step] = getGeneratorReplacementForBlock(path2, funcID, [
1489
+ t13.arrayExpression(bindings.locals),
1490
+ t13.spreadElement(rest)
1491
+ ]);
1492
+ for (let i = 0, len = reps.length; i < len; i++) {
1493
+ replacement.push(reps[i]);
1494
+ }
1495
+ replacement.push(
1496
+ t13.variableDeclaration("const", [
1497
+ t13.variableDeclarator(
1498
+ t13.arrayPattern([returnType, returnResult]),
1499
+ t13.memberExpression(step, t13.identifier("value"))
1500
+ )
1501
+ ])
1502
+ );
1503
+ } else {
1504
+ replacement.push(
1505
+ t13.variableDeclaration("const", [
1506
+ t13.variableDeclarator(
1507
+ t13.arrayPattern([returnType, returnResult]),
1508
+ t13.awaitExpression(
1509
+ t13.callExpression(
1510
+ t13.callExpression(getImportIdentifier(ctx, path2, HIDDEN_FUNC), [
1511
+ source,
1512
+ bindings.mutations.length ? t13.arrowFunctionExpression(
1513
+ [returnMutations],
1514
+ t13.assignmentExpression(
1515
+ "=",
1516
+ t13.objectPattern(
1517
+ bindings.mutations.map(
1518
+ (item) => t13.objectProperty(item, item, false, true)
1519
+ )
1520
+ ),
1521
+ returnMutations
1522
+ )
1523
+ ) : t13.nullLiteral()
1524
+ ]),
1525
+ [t13.arrayExpression(bindings.locals), t13.spreadElement(rest)]
1526
+ )
1527
+ )
1528
+ )
1529
+ ])
1530
+ );
1531
+ }
1532
+ replacement.push(t13.returnStatement(returnResult));
1533
+ return t13.arrowFunctionExpression(
1534
+ [],
1535
+ t13.blockStatement([
1536
+ t13.variableDeclaration("const", [
1537
+ t13.variableDeclarator(
1538
+ source,
1539
+ t13.memberExpression(
1540
+ t13.awaitExpression(t13.importExpression(t13.stringLiteral(entryFile))),
1541
+ t13.identifier("default")
1542
+ )
1543
+ )
1544
+ ]),
1545
+ t13.returnStatement(
1546
+ isPathValid(path2, t13.isFunctionExpression) ? t13.functionExpression(
1547
+ path2.node.id,
1548
+ [t13.restElement(rest)],
1549
+ t13.blockStatement(replacement),
1550
+ path2.node.generator,
1551
+ true
1552
+ ) : t13.arrowFunctionExpression(
1553
+ [t13.restElement(rest)],
1554
+ t13.blockStatement(replacement),
1555
+ true
1556
+ )
1557
+ )
1558
+ ]),
1559
+ true
1560
+ );
1561
+ }
1562
+ function replaceIsomorphicFunction2(ctx, path2, directive, bindings) {
1563
+ const body = path2.get("body");
1564
+ if (isPathValid(body, t13.isExpression)) {
1565
+ body.replaceWith(t13.blockStatement([t13.returnStatement(body.node)]));
1566
+ }
1567
+ assert(isPathValid(body, t13.isBlockStatement), "invariant");
1568
+ const entryFile = createEntryFile(
1569
+ ctx,
1570
+ path2,
1571
+ createRootFile(
1572
+ ctx,
1573
+ bindings,
1574
+ t13.isFunctionExpression(path2.node) ? t13.functionExpression(
1575
+ path2.node.id,
1576
+ [t13.arrayPattern(bindings.locals), ...path2.node.params],
1577
+ path2.node.body,
1578
+ path2.node.generator,
1579
+ path2.node.async
1580
+ ) : t13.arrowFunctionExpression(
1581
+ [t13.arrayPattern(bindings.locals), ...path2.node.params],
1582
+ path2.node.body,
1583
+ path2.node.async
1584
+ )
1585
+ ),
1586
+ directive.target
1587
+ );
1588
+ const source = generateUniqueName(path2, "source");
1589
+ const rest = generateUniqueName(path2, "rest");
1590
+ return t13.arrowFunctionExpression(
1591
+ [],
1592
+ t13.blockStatement([
1593
+ t13.variableDeclaration("const", [
1594
+ t13.variableDeclarator(
1595
+ source,
1596
+ t13.memberExpression(
1597
+ t13.awaitExpression(t13.importExpression(t13.stringLiteral(entryFile))),
1598
+ t13.identifier("default")
1599
+ )
1600
+ )
1601
+ ]),
1602
+ t13.returnStatement(
1603
+ t13.arrowFunctionExpression(
1604
+ [t13.restElement(rest)],
1605
+ t13.callExpression(source, [
1606
+ t13.arrayExpression(bindings.locals),
1607
+ t13.spreadElement(rest)
1608
+ ]),
1609
+ path2.node.async
1610
+ )
1611
+ )
1612
+ ]),
1613
+ true
1614
+ );
1615
+ }
1616
+ function replaceFunctionDirective(ctx, path2, directive, bindings) {
1617
+ const body = path2.get("body");
1618
+ if (isPathValid(body, t13.isExpression)) {
1619
+ body.replaceWith(t13.blockStatement([t13.returnStatement(body.node)]));
1620
+ }
1621
+ assert(isPathValid(body, t13.isBlockStatement), "invariant");
1622
+ transformFunctionContent2(body, bindings.mutations);
1623
+ const entryFile = createEntryFile(
1624
+ ctx,
1625
+ path2,
1626
+ ctx.options.mode === "server" || directive.isomorphic ? createRootFile(
1627
+ ctx,
1628
+ bindings,
1629
+ t13.isFunctionExpression(path2.node) ? t13.functionExpression(
1630
+ path2.node.id,
1631
+ [t13.arrayPattern(bindings.locals), ...path2.node.params],
1632
+ path2.node.body,
1633
+ path2.node.generator,
1634
+ path2.node.async
1635
+ ) : t13.arrowFunctionExpression(
1636
+ [t13.arrayPattern(bindings.locals), ...path2.node.params],
1637
+ path2.node.body,
1638
+ path2.node.async
1639
+ )
1640
+ ) : void 0,
1641
+ directive.target
1642
+ );
1643
+ return getFunctionReplacement2(ctx, path2, entryFile, bindings);
1644
+ }
1645
+ function splitFunctionDirective(ctx, path2, directive) {
1646
+ const bindings = extractBindings(
1647
+ ctx,
1648
+ path2,
1649
+ getForeignBindings(path2, "function"),
1650
+ directive.pure
1651
+ );
1652
+ if (directive.isomorphic) {
1653
+ return replaceIsomorphicFunction2(ctx, path2, directive, bindings);
1654
+ }
1655
+ return replaceFunctionDirective(ctx, path2, directive, bindings);
1656
+ }
1657
+
1658
+ // src/transform-function-directive.ts
1659
+ function transformFunctionDirective(ctx, path2) {
1660
+ const body = path2.get("body");
1661
+ if (isPathValid(body, t14.isBlockStatement)) {
1662
+ const definition = getDirectiveDefinitionFromBlock(ctx, body);
1663
+ if (!definition || definition.type !== "function-directive") {
1278
1664
  return;
1279
1665
  }
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);
1666
+ cleanBlockForDirectives(body, definition);
1667
+ cleanBlockForFauxDirectives(body, definition);
1668
+ const replacement = splitFunctionDirective(ctx, path2, definition);
1669
+ path2.replaceWith(
1670
+ t14.callExpression(getImportIdentifier(ctx, path2, definition.handle), [
1671
+ replacement
1672
+ ])
1673
+ );
1674
+ }
1675
+ }
1676
+
1677
+ // src/utils/bubble-function-declaration.ts
1678
+ var t15 = __toESM(require("@babel/types"), 1);
1679
+
1680
+ // src/utils/is-statement-top-level.ts
1681
+ function isStatementTopLevel(path2) {
1682
+ let blockParent = path2.scope.getBlockParent();
1683
+ const programParent = path2.scope.getProgramParent();
1684
+ if (blockParent.path === path2) {
1685
+ blockParent = blockParent.parent;
1686
+ }
1687
+ return programParent === blockParent;
1688
+ }
1689
+
1690
+ // src/utils/bubble-function-declaration.ts
1691
+ function bubbleFunctionDeclaration(ctx, program2, path2) {
1692
+ if (isStatementTopLevel(path2)) {
1693
+ const decl = path2.node;
1694
+ if (decl.id) {
1695
+ const definition = getDirectiveDefinitionFromBlock(ctx, path2.get("body"));
1696
+ if (!definition || definition.type !== "function-directive") {
1697
+ return;
1698
+ }
1699
+ const [tmp] = program2.unshiftContainer(
1700
+ "body",
1701
+ t15.variableDeclaration("const", [
1702
+ t15.variableDeclarator(
1703
+ decl.id,
1704
+ t15.functionExpression(
1705
+ decl.id,
1706
+ decl.params,
1707
+ decl.body,
1708
+ decl.generator,
1709
+ decl.async
1710
+ )
1711
+ )
1712
+ ])
1713
+ );
1714
+ program2.scope.registerDeclaration(tmp);
1715
+ tmp.skip();
1716
+ if (path2.parentPath.isExportNamedDeclaration()) {
1717
+ path2.parentPath.replaceWith(
1718
+ t15.exportNamedDeclaration(void 0, [
1719
+ t15.exportSpecifier(decl.id, decl.id)
1720
+ ])
1721
+ );
1722
+ } else if (path2.parentPath.isExportDefaultDeclaration()) {
1723
+ path2.replaceWith(decl.id);
1724
+ } else {
1725
+ path2.remove();
1726
+ }
1285
1727
  }
1286
1728
  }
1287
1729
  }
1288
1730
 
1289
1731
  // src/utils/register-import-specifiers.ts
1290
- var t8 = __toESM(require("@babel/types"), 1);
1732
+ var t16 = __toESM(require("@babel/types"), 1);
1291
1733
  function registerImportSpecifier(ctx, node, definition) {
1292
- if (t8.isImportSpecifier(node)) {
1734
+ if (t16.isImportSpecifier(node)) {
1293
1735
  if (node.importKind === "type" || node.importKind === "typeof") {
1294
1736
  return;
1295
1737
  }
@@ -1298,10 +1740,10 @@ function registerImportSpecifier(ctx, node, definition) {
1298
1740
  ctx.registrations.identifiers.set(node.local, definition);
1299
1741
  }
1300
1742
  }
1301
- if (t8.isImportDefaultSpecifier(node) && definition.source.kind === "default") {
1743
+ if (t16.isImportDefaultSpecifier(node) && definition.source.kind === "default") {
1302
1744
  ctx.registrations.identifiers.set(node.local, definition);
1303
1745
  }
1304
- if (t8.isImportNamespaceSpecifier(node)) {
1746
+ if (t16.isImportNamespaceSpecifier(node)) {
1305
1747
  let current = ctx.registrations.namespaces.get(node.local);
1306
1748
  if (!current) {
1307
1749
  current = [];
@@ -1317,7 +1759,7 @@ function registerImportDeclarationByDefinition(ctx, path2, definition) {
1317
1759
  }
1318
1760
  }
1319
1761
  function registerImportSpecifiers(ctx, programPath) {
1320
- const len = ctx.options.functions.length;
1762
+ const len = ctx.options.definitions.length;
1321
1763
  if (!len) {
1322
1764
  return;
1323
1765
  }
@@ -1327,8 +1769,8 @@ function registerImportSpecifiers(ctx, programPath) {
1327
1769
  return;
1328
1770
  }
1329
1771
  for (let i = 0; i < len; i++) {
1330
- const func = ctx.options.functions[i];
1331
- if (func.source.source === path2.node.source.value) {
1772
+ const func = ctx.options.definitions[i];
1773
+ if (func.type === "function-call" && func.source.source === path2.node.source.value) {
1332
1774
  registerImportDeclarationByDefinition(ctx, path2, func);
1333
1775
  }
1334
1776
  }
@@ -1341,11 +1783,22 @@ function plugin() {
1341
1783
  return {
1342
1784
  name: "dismantle",
1343
1785
  visitor: {
1344
- Program(path2, ctx) {
1345
- registerImportSpecifiers(ctx.opts, path2);
1786
+ Program(programPath, ctx) {
1787
+ registerImportSpecifiers(ctx.opts, programPath);
1788
+ programPath.traverse({
1789
+ FunctionDeclaration(path2) {
1790
+ bubbleFunctionDeclaration(ctx.opts, programPath, path2);
1791
+ }
1792
+ });
1793
+ },
1794
+ ArrowFunctionExpression(path2, ctx) {
1795
+ transformFunctionDirective(ctx.opts, path2);
1796
+ },
1797
+ FunctionExpression(path2, ctx) {
1798
+ transformFunctionDirective(ctx.opts, path2);
1346
1799
  },
1347
1800
  BlockStatement(path2, ctx) {
1348
- transformBlock(ctx.opts, path2);
1801
+ transformBlockDirective(ctx.opts, path2);
1349
1802
  },
1350
1803
  CallExpression(path2, ctx) {
1351
1804
  transformCall(ctx.opts, path2);
@@ -1469,7 +1922,19 @@ async function compile(id, code, options) {
1469
1922
  namespaces: /* @__PURE__ */ new Map()
1470
1923
  }
1471
1924
  };
1472
- const plugins = ["jsx"];
1925
+ const plugins = [
1926
+ "jsx",
1927
+ // import { example } from 'example' with { example: true };
1928
+ "importAttributes",
1929
+ // () => throw example
1930
+ "throwExpressions",
1931
+ // You know what this is
1932
+ "decorators",
1933
+ // const { #example: example } = this;
1934
+ "destructuringPrivate",
1935
+ // using example = myExample()
1936
+ "explicitResourceManagement"
1937
+ ];
1473
1938
  if (/\.[mc]?tsx?$/i.test(id)) {
1474
1939
  plugins.push("typescript");
1475
1940
  }