async-browser-context 0.2.3 → 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 (140) hide show
  1. package/CHANGELOG.md +102 -0
  2. package/README.md +45 -30
  3. package/dist/babel/async-generator-helpers.d.ts +6 -6
  4. package/dist/babel/async-generator-helpers.d.ts.map +1 -1
  5. package/dist/babel/async-generator-helpers.js +33 -22
  6. package/dist/babel/async-generator-helpers.js.map +1 -1
  7. package/dist/babel/async-generator-resources.d.ts +14 -0
  8. package/dist/babel/async-generator-resources.d.ts.map +1 -0
  9. package/dist/babel/async-generator-resources.js +38 -0
  10. package/dist/babel/async-generator-resources.js.map +1 -0
  11. package/dist/babel/bind-generators.d.ts +1 -1
  12. package/dist/babel/bind-generators.d.ts.map +1 -1
  13. package/dist/babel/bind-generators.js +88 -44
  14. package/dist/babel/bind-generators.js.map +1 -1
  15. package/dist/babel/name-coroutines.d.ts +14 -8
  16. package/dist/babel/name-coroutines.d.ts.map +1 -1
  17. package/dist/babel/name-coroutines.js +160 -39
  18. package/dist/babel/name-coroutines.js.map +1 -1
  19. package/dist/babel/preset.d.ts +24 -13
  20. package/dist/babel/preset.d.ts.map +1 -1
  21. package/dist/babel/preset.js +37 -9
  22. package/dist/babel/preset.js.map +1 -1
  23. package/dist/babel/runtime-calls.d.ts +22 -0
  24. package/dist/babel/runtime-calls.d.ts.map +1 -0
  25. package/dist/babel/runtime-calls.js +69 -0
  26. package/dist/babel/runtime-calls.js.map +1 -0
  27. package/dist/babel/skip-runtime.d.ts +18 -11
  28. package/dist/babel/skip-runtime.d.ts.map +1 -1
  29. package/dist/babel/skip-runtime.js +52 -16
  30. package/dist/babel/skip-runtime.js.map +1 -1
  31. package/dist/babel/static-members.d.ts +4 -0
  32. package/dist/babel/static-members.d.ts.map +1 -0
  33. package/dist/babel/static-members.js +112 -0
  34. package/dist/babel/static-members.js.map +1 -0
  35. package/dist/core/async-generator.d.ts +51 -13
  36. package/dist/core/async-generator.d.ts.map +1 -1
  37. package/dist/core/async-generator.js +255 -58
  38. package/dist/core/async-generator.js.map +1 -1
  39. package/dist/core/async-local-storage.d.ts +3 -0
  40. package/dist/core/async-local-storage.d.ts.map +1 -1
  41. package/dist/core/async-local-storage.js +3 -0
  42. package/dist/core/async-local-storage.js.map +1 -1
  43. package/dist/core/callback-hook.d.ts +3 -3
  44. package/dist/core/coroutine.d.ts +6 -2
  45. package/dist/core/coroutine.d.ts.map +1 -1
  46. package/dist/core/coroutine.js +106 -44
  47. package/dist/core/coroutine.js.map +1 -1
  48. package/dist/core/function-name.d.ts +9 -0
  49. package/dist/core/function-name.d.ts.map +1 -0
  50. package/dist/core/function-name.js +12 -0
  51. package/dist/core/function-name.js.map +1 -0
  52. package/dist/core/hooks.d.ts +2 -1
  53. package/dist/core/hooks.d.ts.map +1 -1
  54. package/dist/core/hooks.js +21 -6
  55. package/dist/core/hooks.js.map +1 -1
  56. package/dist/core/snapshot.d.ts +2 -1
  57. package/dist/core/snapshot.d.ts.map +1 -1
  58. package/dist/core/snapshot.js +7 -3
  59. package/dist/core/snapshot.js.map +1 -1
  60. package/dist/core/store.d.ts +28 -0
  61. package/dist/core/store.d.ts.map +1 -1
  62. package/dist/core/store.js +90 -20
  63. package/dist/core/store.js.map +1 -1
  64. package/dist/core/variable.d.ts +2 -1
  65. package/dist/core/variable.d.ts.map +1 -1
  66. package/dist/core/variable.js +5 -2
  67. package/dist/core/variable.js.map +1 -1
  68. package/dist/node/index.d.ts +10 -4
  69. package/dist/node/index.d.ts.map +1 -1
  70. package/dist/node/index.js +51 -16
  71. package/dist/node/index.js.map +1 -1
  72. package/dist/node/runtime.d.ts +7 -6
  73. package/dist/node/runtime.d.ts.map +1 -1
  74. package/dist/node/runtime.js +29 -16
  75. package/dist/node/runtime.js.map +1 -1
  76. package/dist/opentelemetry/context-manager.d.ts.map +1 -1
  77. package/dist/opentelemetry/context-manager.js +172 -13
  78. package/dist/opentelemetry/context-manager.js.map +1 -1
  79. package/dist/patches/callbacks.d.ts.map +1 -1
  80. package/dist/patches/callbacks.js +84 -29
  81. package/dist/patches/callbacks.js.map +1 -1
  82. package/dist/patches/events.d.ts.map +1 -1
  83. package/dist/patches/events.js +203 -91
  84. package/dist/patches/events.js.map +1 -1
  85. package/dist/patches/handler-interfaces.d.ts +3 -2
  86. package/dist/patches/handler-interfaces.d.ts.map +1 -1
  87. package/dist/patches/handler-interfaces.js +3 -2
  88. package/dist/patches/handler-interfaces.js.map +1 -1
  89. package/dist/patches/promise.d.ts +3 -0
  90. package/dist/patches/promise.d.ts.map +1 -1
  91. package/dist/patches/promise.js +13 -5
  92. package/dist/patches/promise.js.map +1 -1
  93. package/dist/patches/streams.d.ts.map +1 -1
  94. package/dist/patches/streams.js +36 -16
  95. package/dist/patches/streams.js.map +1 -1
  96. package/dist/patches/timers.d.ts.map +1 -1
  97. package/dist/patches/timers.js +17 -5
  98. package/dist/patches/timers.js.map +1 -1
  99. package/dist/patches/to-string.d.ts +4 -0
  100. package/dist/patches/to-string.d.ts.map +1 -1
  101. package/dist/patches/to-string.js +12 -6
  102. package/dist/patches/to-string.js.map +1 -1
  103. package/dist/runtime.d.ts +3 -3
  104. package/dist/runtime.d.ts.map +1 -1
  105. package/dist/runtime.js +3 -3
  106. package/dist/runtime.js.map +1 -1
  107. package/dist/vite/plugin.d.ts +7 -2
  108. package/dist/vite/plugin.d.ts.map +1 -1
  109. package/dist/vite/plugin.js +30 -6
  110. package/dist/vite/plugin.js.map +1 -1
  111. package/package.json +49 -25
  112. package/src/babel/async-generator-helpers.ts +34 -23
  113. package/src/babel/async-generator-resources.ts +40 -0
  114. package/src/babel/bind-generators.ts +91 -47
  115. package/src/babel/name-coroutines.ts +170 -41
  116. package/src/babel/preset.ts +47 -15
  117. package/src/babel/runtime-calls.ts +73 -0
  118. package/src/babel/skip-runtime.ts +58 -18
  119. package/src/babel/static-members.ts +117 -0
  120. package/src/core/async-generator.ts +273 -62
  121. package/src/core/async-local-storage.ts +3 -0
  122. package/src/core/callback-hook.ts +3 -3
  123. package/src/core/coroutine.ts +104 -43
  124. package/src/core/function-name.ts +11 -0
  125. package/src/core/hooks.ts +23 -6
  126. package/src/core/snapshot.ts +8 -3
  127. package/src/core/store.ts +107 -21
  128. package/src/core/variable.ts +6 -2
  129. package/src/node/index.ts +62 -19
  130. package/src/node/runtime.ts +33 -15
  131. package/src/opentelemetry/context-manager.ts +188 -13
  132. package/src/patches/callbacks.ts +87 -30
  133. package/src/patches/events.ts +249 -103
  134. package/src/patches/handler-interfaces.ts +3 -2
  135. package/src/patches/promise.ts +17 -13
  136. package/src/patches/streams.ts +36 -16
  137. package/src/patches/timers.ts +17 -5
  138. package/src/patches/to-string.ts +12 -6
  139. package/src/runtime.ts +3 -3
  140. package/src/vite/plugin.ts +38 -8
package/src/runtime.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  /**
2
2
  * The runtime of the code that the Babel preset transforms. The transformed
3
- * code imports `coroutine`, `bindGenerator`, `asyncGenerator` and
4
- * `OverloadYield` from this module. When the
3
+ * code imports `coroutine`, `bindGenerator`, `asyncGenerator`, `OverloadYield`,
4
+ * `awaitAsyncGenerator` and `asyncGeneratorDelegate` from this module. When the
5
5
  * module loads, it installs the patches of the runtime.
6
6
  *
7
7
  * @packageDocumentation
8
8
  */
9
9
  import "./install.js";
10
10
 
11
- export { OverloadYield } from "./core/async-generator.js";
11
+ export { asyncGeneratorDelegate, awaitAsyncGenerator, OverloadYield } from "./core/async-generator.js";
12
12
  export { asyncGenerator, bindGenerator, coroutine } from "./core/coroutine.js";
@@ -9,12 +9,17 @@ export type ModuleFilter = RegExp | ((id : string) => boolean);
9
9
  export interface AsyncContextPluginOptions {
10
10
  /**
11
11
  * The modules to transform. The default is all JavaScript and TypeScript
12
- * modules, also the modules in `node_modules`.
12
+ * modules, also the modules in `node_modules`, and the JavaScript of Vue,
13
+ * Svelte, Astro and MDX components. This filter can only select from these
14
+ * modules: the plugin does not transform styles.
13
15
  */
14
16
  readonly include? : ModuleFilter | undefined;
15
17
  /** The modules not to transform. */
16
18
  readonly exclude? : ModuleFilter | undefined;
17
- /** The runtime module. The default is `async-browser-context/runtime`. */
19
+ /**
20
+ * The runtime module. It must export the six names of `async-browser-context/runtime`.
21
+ * The default is `async-browser-context/runtime`.
22
+ */
18
23
  readonly runtime? : string | undefined;
19
24
  /**
20
25
  * Give the name of each async function to the callback hooks. The default
@@ -30,15 +35,33 @@ export interface AsyncContextPluginOptions {
30
35
  }
31
36
 
32
37
  const SCRIPT = /\.[cm]?[jt]sx?$/;
38
+ /** The component files that other Vite plugins compile to JavaScript before this plugin operates. */
39
+ const COMPONENT = /\.(?:vue|svelte|astro|mdx)$/;
40
+ /** The parts of a component that are styles, not JavaScript, for example `App.vue?vue&type=style&lang.css`. */
41
+ const STYLE_QUERY = /[?&](?:type=style|lang\.(?:css|scss|sass|less|styl|stylus|pcss|postcss|sss))(?:&|$)/;
33
42
  /** A module without these words has no async function, generator or `for await`. */
34
43
  const MIGHT_CHANGE = /\basync\b|\byield\b|function\s*\*/;
35
44
  /** The runtime of this library. The plugin must not transform it. */
36
45
  const OWN_RUNTIME = /[\\/]async-browser-context[\\/]dist[\\/]/;
46
+ /** The marks of a CommonJS module. */
47
+ const COMMONJS = /\brequire\s*\(|\bmodule\.exports\b|\bexports\.[A-Za-z_$]/;
37
48
 
38
49
  function matches(filter : ModuleFilter, id : string) : boolean {
39
50
  return filter instanceof RegExp ? filter.test(id) : filter(id);
40
51
  }
41
52
 
53
+ /**
54
+ * This function gives the source type for Babel. Vite serves each module as an ES module, also
55
+ * a worker or a file with only a dynamic `import()`. With "unambiguous", Babel reads such a file
56
+ * as a script and adds a `require()` of the runtime, which a browser does not have. Thus only a
57
+ * `.cjs` or `.cts` file, or a file with CommonJS marks, gets "unambiguous". Babel then reads it
58
+ * as a script, unless it has a static import or export. A text of the code that looks like an
59
+ * import, for example in a string, does not change this.
60
+ */
61
+ function sourceTypeOf(file : string, code : string) : "module" | "unambiguous" {
62
+ return /\.c[jt]s$/.test(file) || COMMONJS.test(code) ? "unambiguous" : "module";
63
+ }
64
+
42
65
  /**
43
66
  * The Vite plugin of `async-browser-context`. It applies the Babel preset to
44
67
  * the modules of the application and of its dependencies. Thus, the context
@@ -58,6 +81,8 @@ function matches(filter : ModuleFilter, id : string) : boolean {
58
81
  */
59
82
  export function asyncContext(options : AsyncContextPluginOptions = {}) : Plugin {
60
83
  const runtime = options.runtime ?? DEFAULT_RUNTIME;
84
+ // One presets array for all modules, so the configuration cache of Babel operates
85
+ const presets : NonNullable<NonNullable<Parameters<typeof transformAsync>[1]>["presets"]> = [[preset, { runtime, names : options.names }]];
61
86
  return {
62
87
  name : "async-browser-context",
63
88
  enforce : "post",
@@ -71,7 +96,7 @@ export function asyncContext(options : AsyncContextPluginOptions = {}) : Plugin
71
96
  return null;
72
97
  }
73
98
  const file = id.split("?", 1)[0] ?? id;
74
- if (!SCRIPT.test(file) || OWN_RUNTIME.test(file)) {
99
+ if (!(SCRIPT.test(file) || COMPONENT.test(file)) || STYLE_QUERY.test(id) || OWN_RUNTIME.test(file)) {
75
100
  return null;
76
101
  }
77
102
  if (options.include !== undefined && !matches(options.include, file)) {
@@ -87,16 +112,21 @@ export function asyncContext(options : AsyncContextPluginOptions = {}) : Plugin
87
112
  filename : file,
88
113
  babelrc : false,
89
114
  configFile : false,
90
- sourceType : "unambiguous",
115
+ sourceType : sourceTypeOf(file, code),
91
116
  sourceMaps : true,
92
- presets : [[preset, { runtime, names : options.names }]],
117
+ // The map of Vite describes the code that this plugin gets, not a map file of the module
118
+ inputSourceMap : false,
119
+ // Large modules, for example the dependency bundles of Vite, keep their format
120
+ compact : false,
121
+ presets,
93
122
  });
94
- const changed = (result?.metadata as Record<string, unknown> | undefined)?.[METADATA_KEY] === true;
95
- if (result?.code == null || !changed) {
123
+ // Without the options ignore and only, Babel gives a result with code. With sourceMaps, it also gives a map.
124
+ const { code : output, map, metadata } = result as { code : string; map : object; metadata : Record<string, unknown> };
125
+ if (metadata[METADATA_KEY] !== true) {
96
126
  return null;
97
127
  }
98
128
  // A source map in JSON text satisfies both Rollup and Rolldown
99
- return { code : result.code, map : result.map == null ? null : JSON.stringify(result.map) };
129
+ return { code : output, map : JSON.stringify(map) };
100
130
  },
101
131
  },
102
132
  };