async-browser-context 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +19 -0
- package/dist/babel/preset.d.ts +9 -1
- package/dist/babel/preset.d.ts.map +1 -1
- package/dist/babel/preset.js +4 -2
- package/dist/babel/preset.js.map +1 -1
- package/dist/vite/plugin.d.ts +5 -0
- package/dist/vite/plugin.d.ts.map +1 -1
- package/dist/vite/plugin.js +1 -1
- package/dist/vite/plugin.js.map +1 -1
- package/package.json +7 -3
- package/src/babel/preset.ts +12 -3
- package/src/vite/plugin.ts +6 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Change history
|
|
2
2
|
|
|
3
|
+
## 0.2.1
|
|
4
|
+
|
|
5
|
+
### New functions
|
|
6
|
+
|
|
7
|
+
- The option `names` of the Babel preset and of the Vite plugin. With `names: false`, the preset does not give the names of the async functions to `coroutine`. The code is then smaller by 0.1 to 1.6 percent.
|
|
8
|
+
|
|
9
|
+
### Documentation and tests
|
|
10
|
+
|
|
11
|
+
- The guide "Build tools" gives the setup for webpack, Rspack, Rollup, esbuild, Next.js and SWC. The test `pnpm test:bundlers` builds a page with each setup and examines it in Chromium.
|
|
12
|
+
- SWC and esbuild with the target ES2016 keep all rules except rule C5. Two new test projects start all rule tests with these compilers in place of the preset.
|
|
13
|
+
- The command `pnpm bench:size` measures the size of the runtime and the size that the transform adds to real npm packages. The runtime is 5.9 kB with gzip. The transform adds 2.9 percent to an example application.
|
|
14
|
+
|
|
3
15
|
## 0.2.0
|
|
4
16
|
|
|
5
17
|
### New functions
|
package/README.md
CHANGED
|
@@ -81,6 +81,7 @@ The plugin transforms the modules of the application and of its dependencies, al
|
|
|
81
81
|
| `include` | All JavaScript and TypeScript modules | A regular expression or a function that selects the modules to transform. |
|
|
82
82
|
| `exclude` | None | A regular expression or a function that selects the modules not to transform. |
|
|
83
83
|
| `runtime` | `async-browser-context/runtime` | The module that the transformed code imports. |
|
|
84
|
+
| `names` | `true` | Set `false` to remove the names of the async functions for the callback hooks. The code is then smaller by 0.1 to 1.6 percent. |
|
|
84
85
|
| `ssr` | `false` | Set `true` to also transform the modules of server-side rendering, for example for Vitest in Node.js. |
|
|
85
86
|
|
|
86
87
|
### Babel
|
|
@@ -97,6 +98,20 @@ The plugin transforms the modules of the application and of its dependencies, al
|
|
|
97
98
|
|
|
98
99
|
The preset operates with Babel 7.22 and later, and with Babel 8. CI examines Babel 7.22, the newest Babel 7 and the newest Babel 8 with the packed package (`pnpm test:babel`).
|
|
99
100
|
|
|
101
|
+
### Other build tools
|
|
102
|
+
|
|
103
|
+
A test in CI (`pnpm test:bundlers`) builds a page with each of these setups and examines it in Chromium. The [build tools guide](https://mark1russell7.github.io/AsyncBrowserContext/docs/guides/build-tools) gives each configuration.
|
|
104
|
+
|
|
105
|
+
| Tool | Setup | Rules |
|
|
106
|
+
| --- | --- | --- |
|
|
107
|
+
| webpack 5, Rspack 2 | `babel-loader` with the preset. Do not exclude `node_modules`. | All |
|
|
108
|
+
| Rollup 4 | `@rollup/plugin-babel` with the preset | All |
|
|
109
|
+
| esbuild | A small plugin that applies the preset | All |
|
|
110
|
+
| Next.js 16, Turbopack or webpack | `babel.config.js` with `next/babel` and the preset, and `transpilePackages` for the dependencies | All |
|
|
111
|
+
| SWC, esbuild, Next.js without Babel | The target ES2016: the compiler changes async functions into generators | All except rule C5 |
|
|
112
|
+
|
|
113
|
+
With the target ES2016, the patch of `then` keeps the context through `await`. A generator body then gets the context of each `next()` call, and the callback hooks get no names.
|
|
114
|
+
|
|
100
115
|
### Node.js
|
|
101
116
|
|
|
102
117
|
Do not set up a transform. The `node` export condition selects the Node.js entry, which uses the native `AsyncLocalStorage`.
|
|
@@ -259,6 +274,8 @@ One read with `get()` takes approximately 4 ns. The [documentation website](http
|
|
|
259
274
|
|
|
260
275
|
When the page loads, the library installs its patches in approximately 8 ms: the median of Chromium, Firefox and WebKit on the same computer.
|
|
261
276
|
|
|
277
|
+
The runtime is 5.9 kB with gzip, and zone.js 0.16.3 is 13.3 kB. The transform adds 4 to 9 percent to packages with much async code, and nothing to React DOM. In an example application with React DOM, TanStack Query, supabase-js and ky, it adds 2.9 percent (`pnpm bench:size`).
|
|
278
|
+
|
|
262
279
|
## Tests
|
|
263
280
|
|
|
264
281
|
| Command | Function |
|
|
@@ -268,8 +285,10 @@ When the page loads, the library installs its patches in approximately 8 ms: the
|
|
|
268
285
|
| `pnpm test:browser` | The tests in Chromium, Firefox and WebKit |
|
|
269
286
|
| `pnpm smoke` | Packs the package, installs it in a new Vite application, and examines the application in Chromium |
|
|
270
287
|
| `pnpm test:babel` | Packs the package, transforms a program with the preset in Babel 7.22, the newest Babel 7 and Babel 8, and starts the output |
|
|
288
|
+
| `pnpm test:bundlers` | Packs the package, builds a page with webpack, Rspack, Rollup, esbuild and Next.js, and examines each page in Chromium |
|
|
271
289
|
| `pnpm mutation` | Mutation testing with Stryker |
|
|
272
290
|
| `pnpm bench` | The benchmarks |
|
|
291
|
+
| `pnpm bench:size` | The size of the runtime, and the size that the transform adds to a set of npm packages |
|
|
273
292
|
| `pnpm lint:ste` | The STE linter of the documentation and the TSDoc comments |
|
|
274
293
|
|
|
275
294
|
Set `BROWSERS=chromium,firefox,webkit` to select the browsers. Playwright WebKit does not start on all Windows computers, so the default on Windows is Chromium and Firefox. `docs/testing.md` tells more.
|
package/dist/babel/preset.d.ts
CHANGED
|
@@ -7,6 +7,13 @@ export interface PresetOptions {
|
|
|
7
7
|
* `bindGenerator`. The default is `async-browser-context/runtime`.
|
|
8
8
|
*/
|
|
9
9
|
readonly runtime?: string | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Give the name of each async function to `coroutine`, for the callback
|
|
12
|
+
* hooks. The default is `true`. With `false`, the hooks get no name for the
|
|
13
|
+
* steps after an `await`. The code is then smaller by 0.1 to 1.6 percent
|
|
14
|
+
* of its gzip size (`pnpm bench:size`).
|
|
15
|
+
*/
|
|
16
|
+
readonly names?: boolean | undefined;
|
|
10
17
|
}
|
|
11
18
|
/** The default runtime module. */
|
|
12
19
|
export declare const DEFAULT_RUNTIME = "async-browser-context/runtime";
|
|
@@ -17,7 +24,8 @@ export declare const DEFAULT_RUNTIME = "async-browser-context/runtime";
|
|
|
17
24
|
* 1. A plugin of this library binds each generator to the context of its
|
|
18
25
|
* creation.
|
|
19
26
|
* 2. A plugin of this library gives the name of each async function to
|
|
20
|
-
* `coroutine`, for the callback hooks.
|
|
27
|
+
* `coroutine`, for the callback hooks. The option `names: false` removes
|
|
28
|
+
* this step.
|
|
21
29
|
* 3. `@babel/plugin-transform-async-generator-functions` changes async
|
|
22
30
|
* generators and `for await` loops.
|
|
23
31
|
* 4. `@babel/plugin-transform-async-to-generator` changes each async function
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"preset.d.ts","sourceRoot":"","sources":["../../src/babel/preset.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"preset.d.ts","sourceRoot":"","sources":["../../src/babel/preset.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,SAAS,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAMvE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,iCAAiC;AACjC,MAAM,WAAW,aAAa;IAC1B;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAG,MAAM,GAAG,SAAS,CAAC;IACvC;;;;;OAKG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAG,OAAO,GAAG,SAAS,CAAC;CACzC;AAED,kCAAkC;AAClC,eAAO,MAAM,eAAe,kCAAkC,CAAC;AAE/D;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAAC,GAAG,EAAG,SAAS,EAAE,OAAO,GAAG,aAAkB,GAAI,YAAY,CAY9G"}
|
package/dist/babel/preset.js
CHANGED
|
@@ -12,7 +12,8 @@ export const DEFAULT_RUNTIME = "async-browser-context/runtime";
|
|
|
12
12
|
* 1. A plugin of this library binds each generator to the context of its
|
|
13
13
|
* creation.
|
|
14
14
|
* 2. A plugin of this library gives the name of each async function to
|
|
15
|
-
* `coroutine`, for the callback hooks.
|
|
15
|
+
* `coroutine`, for the callback hooks. The option `names: false` removes
|
|
16
|
+
* this step.
|
|
16
17
|
* 3. `@babel/plugin-transform-async-generator-functions` changes async
|
|
17
18
|
* generators and `for await` loops.
|
|
18
19
|
* 4. `@babel/plugin-transform-async-to-generator` changes each async function
|
|
@@ -24,10 +25,11 @@ export const DEFAULT_RUNTIME = "async-browser-context/runtime";
|
|
|
24
25
|
export default function asyncBrowserContextPreset(api, options = {}) {
|
|
25
26
|
api.assertVersion("^7.22.0 || ^8.0.0");
|
|
26
27
|
const runtime = options.runtime ?? DEFAULT_RUNTIME;
|
|
28
|
+
const names = options.names === false ? [] : [[nameCoroutinesPlugin, { runtime }]];
|
|
27
29
|
return {
|
|
28
30
|
plugins: [
|
|
29
31
|
[bindGeneratorsPlugin, { runtime }],
|
|
30
|
-
|
|
32
|
+
...names,
|
|
31
33
|
asyncGeneratorFunctions,
|
|
32
34
|
[asyncToGenerator, { module: runtime, method: "coroutine" }],
|
|
33
35
|
],
|
package/dist/babel/preset.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"preset.js","sourceRoot":"","sources":["../../src/babel/preset.ts"],"names":[],"mappings":"AACA,OAAO,uBAAuB,MAAM,mDAAmD,CAAC;AACxF,OAAO,gBAAgB,MAAM,4CAA4C,CAAC;AAC1E,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"preset.js","sourceRoot":"","sources":["../../src/babel/preset.ts"],"names":[],"mappings":"AACA,OAAO,uBAAuB,MAAM,mDAAmD,CAAC;AACxF,OAAO,gBAAgB,MAAM,4CAA4C,CAAC;AAC1E,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAkBpD,kCAAkC;AAClC,MAAM,CAAC,MAAM,eAAe,GAAG,+BAA+B,CAAC;AAE/D;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAAC,GAAe,EAAE,UAA0B,EAAE;IAC3F,GAAG,CAAC,aAAa,CAAC,mBAAmB,CAAC,CAAC;IACvC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,eAAe,CAAC;IACnD,MAAM,KAAK,GAAkB,OAAO,CAAC,KAAK,KAAK,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,oBAAoB,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;IAClG,OAAO;QACH,OAAO,EAAG;YACN,CAAC,oBAAoB,EAAE,EAAE,OAAO,EAAE,CAAC;YACnC,GAAG,KAAK;YACR,uBAAuB;YACvB,CAAC,gBAAgB,EAAE,EAAE,MAAM,EAAG,OAAO,EAAE,MAAM,EAAG,WAAW,EAAE,CAAC;SACjE;KACJ,CAAC;AACN,CAAC"}
|
package/dist/vite/plugin.d.ts
CHANGED
|
@@ -12,6 +12,11 @@ export interface AsyncContextPluginOptions {
|
|
|
12
12
|
readonly exclude?: ModuleFilter | undefined;
|
|
13
13
|
/** The runtime module. The default is `async-browser-context/runtime`. */
|
|
14
14
|
readonly runtime?: string | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Give the name of each async function to the callback hooks. The default
|
|
17
|
+
* is `true`. Refer to the option `names` of the Babel preset.
|
|
18
|
+
*/
|
|
19
|
+
readonly names?: boolean | undefined;
|
|
15
20
|
/**
|
|
16
21
|
* Also transform the modules of server-side rendering. The default is
|
|
17
22
|
* `false`, because on Node.js the native `AsyncLocalStorage` keeps the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/vite/plugin.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAGnC,mEAAmE;AACnE,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,CAAC,CAAC,EAAE,EAAG,MAAM,KAAK,OAAO,CAAC,CAAC;AAE/D,sCAAsC;AACtC,MAAM,WAAW,yBAAyB;IACtC;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAG,YAAY,GAAG,SAAS,CAAC;IAC7C,oCAAoC;IACpC,QAAQ,CAAC,OAAO,CAAC,EAAG,YAAY,GAAG,SAAS,CAAC;IAC7C,0EAA0E;IAC1E,QAAQ,CAAC,OAAO,CAAC,EAAG,MAAM,GAAG,SAAS,CAAC;IACvC;;;;OAIG;IACH,QAAQ,CAAC,GAAG,CAAC,EAAG,OAAO,GAAG,SAAS,CAAC;CACvC;AAYD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAG,yBAA8B,GAAI,MAAM,CA4C9E"}
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/vite/plugin.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAGnC,mEAAmE;AACnE,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,CAAC,CAAC,EAAE,EAAG,MAAM,KAAK,OAAO,CAAC,CAAC;AAE/D,sCAAsC;AACtC,MAAM,WAAW,yBAAyB;IACtC;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAG,YAAY,GAAG,SAAS,CAAC;IAC7C,oCAAoC;IACpC,QAAQ,CAAC,OAAO,CAAC,EAAG,YAAY,GAAG,SAAS,CAAC;IAC7C,0EAA0E;IAC1E,QAAQ,CAAC,OAAO,CAAC,EAAG,MAAM,GAAG,SAAS,CAAC;IACvC;;;OAGG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAG,OAAO,GAAG,SAAS,CAAC;IACtC;;;;OAIG;IACH,QAAQ,CAAC,GAAG,CAAC,EAAG,OAAO,GAAG,SAAS,CAAC;CACvC;AAYD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAG,yBAA8B,GAAI,MAAM,CA4C9E"}
|
package/dist/vite/plugin.js
CHANGED
|
@@ -58,7 +58,7 @@ export function asyncContext(options = {}) {
|
|
|
58
58
|
configFile: false,
|
|
59
59
|
sourceType: "unambiguous",
|
|
60
60
|
sourceMaps: true,
|
|
61
|
-
presets: [[preset, { runtime }]],
|
|
61
|
+
presets: [[preset, { runtime, names: options.names }]],
|
|
62
62
|
});
|
|
63
63
|
const changed = result?.metadata?.[METADATA_KEY] === true;
|
|
64
64
|
if (result?.code == null || !changed) {
|
package/dist/vite/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/vite/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C,OAAO,MAAM,EAAE,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/vite/plugin.ts"],"names":[],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "async-browser-context",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "AsyncLocalStorage and the TC39 AsyncContext API (Variable, Snapshot) for browsers. The context stays through await, promises, timers, events and generators. A Babel preset and a Vite plugin transform async functions. On Node.js, the package uses the native AsyncLocalStorage.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Mark Russell",
|
|
@@ -106,7 +106,7 @@
|
|
|
106
106
|
"build": "tsc -p tsconfig.build.json",
|
|
107
107
|
"typecheck": "tsc -p tsconfig.json",
|
|
108
108
|
"test": "vitest run",
|
|
109
|
-
"test:node": "vitest run --project unit --project \"rules (browser runtime, Node.js)\" --project \"rules (node runtime)\" --project \"rules (node runtime, no transform)\"",
|
|
109
|
+
"test:node": "vitest run --project unit --project \"rules (browser runtime, Node.js)\" --project \"rules (browser runtime, SWC ES2016)\" --project \"rules (browser runtime, esbuild ES2016)\" --project \"rules (node runtime)\" --project \"rules (node runtime, no transform)\"",
|
|
110
110
|
"test:browser": "vitest run --project \"browser (chromium)\" --project \"browser (firefox)\" --project \"browser (webkit)\"",
|
|
111
111
|
"coverage": "vitest run --coverage --project unit --project \"rules (browser runtime, Node.js)\" --project \"rules (node runtime)\"",
|
|
112
112
|
"mutation": "stryker run",
|
|
@@ -118,7 +118,9 @@
|
|
|
118
118
|
"site:build": "pnpm --filter ./site build",
|
|
119
119
|
"prepublishOnly": "pnpm build && pnpm test",
|
|
120
120
|
"site:data": "tsx scripts/site-data.ts",
|
|
121
|
-
"readme:media": "tsx scripts/readme-media.ts"
|
|
121
|
+
"readme:media": "tsx scripts/readme-media.ts",
|
|
122
|
+
"test:bundlers": "pnpm build && tsx test/bundlers/run.ts",
|
|
123
|
+
"bench:size": "tsx bench/size.ts"
|
|
122
124
|
},
|
|
123
125
|
"dependencies": {
|
|
124
126
|
"@babel/core": "^8.0.7",
|
|
@@ -141,10 +143,12 @@
|
|
|
141
143
|
"devDependencies": {
|
|
142
144
|
"@opentelemetry/api": "^1.9.1",
|
|
143
145
|
"@stryker-mutator/core": "^10.0.0",
|
|
146
|
+
"@swc/core": "^1.16.18",
|
|
144
147
|
"@types/node": "^25.3.0",
|
|
145
148
|
"@vitest/browser": "^5.0.3",
|
|
146
149
|
"@vitest/browser-playwright": "^5.0.3",
|
|
147
150
|
"@vitest/coverage-v8": "^5.0.3",
|
|
151
|
+
"esbuild": "^0.28.2",
|
|
148
152
|
"playwright": "^1.64.0",
|
|
149
153
|
"tinybench": "^6.2.1",
|
|
150
154
|
"tsx": "^4.23.15",
|
package/src/babel/preset.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { PresetAPI, PresetObject } from "@babel/core";
|
|
1
|
+
import type { PluginItem, PresetAPI, PresetObject } from "@babel/core";
|
|
2
2
|
import asyncGeneratorFunctions from "@babel/plugin-transform-async-generator-functions";
|
|
3
3
|
import asyncToGenerator from "@babel/plugin-transform-async-to-generator";
|
|
4
4
|
import { bindGeneratorsPlugin } from "./bind-generators.js";
|
|
@@ -13,6 +13,13 @@ export interface PresetOptions {
|
|
|
13
13
|
* `bindGenerator`. The default is `async-browser-context/runtime`.
|
|
14
14
|
*/
|
|
15
15
|
readonly runtime? : string | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Give the name of each async function to `coroutine`, for the callback
|
|
18
|
+
* hooks. The default is `true`. With `false`, the hooks get no name for the
|
|
19
|
+
* steps after an `await`. The code is then smaller by 0.1 to 1.6 percent
|
|
20
|
+
* of its gzip size (`pnpm bench:size`).
|
|
21
|
+
*/
|
|
22
|
+
readonly names? : boolean | undefined;
|
|
16
23
|
}
|
|
17
24
|
|
|
18
25
|
/** The default runtime module. */
|
|
@@ -25,7 +32,8 @@ export const DEFAULT_RUNTIME = "async-browser-context/runtime";
|
|
|
25
32
|
* 1. A plugin of this library binds each generator to the context of its
|
|
26
33
|
* creation.
|
|
27
34
|
* 2. A plugin of this library gives the name of each async function to
|
|
28
|
-
* `coroutine`, for the callback hooks.
|
|
35
|
+
* `coroutine`, for the callback hooks. The option `names: false` removes
|
|
36
|
+
* this step.
|
|
29
37
|
* 3. `@babel/plugin-transform-async-generator-functions` changes async
|
|
30
38
|
* generators and `for await` loops.
|
|
31
39
|
* 4. `@babel/plugin-transform-async-to-generator` changes each async function
|
|
@@ -37,10 +45,11 @@ export const DEFAULT_RUNTIME = "async-browser-context/runtime";
|
|
|
37
45
|
export default function asyncBrowserContextPreset(api : PresetAPI, options : PresetOptions = {}) : PresetObject {
|
|
38
46
|
api.assertVersion("^7.22.0 || ^8.0.0");
|
|
39
47
|
const runtime = options.runtime ?? DEFAULT_RUNTIME;
|
|
48
|
+
const names : PluginItem[] = options.names === false ? [] : [[nameCoroutinesPlugin, { runtime }]];
|
|
40
49
|
return {
|
|
41
50
|
plugins : [
|
|
42
51
|
[bindGeneratorsPlugin, { runtime }],
|
|
43
|
-
|
|
52
|
+
...names,
|
|
44
53
|
asyncGeneratorFunctions,
|
|
45
54
|
[asyncToGenerator, { module : runtime, method : "coroutine" }],
|
|
46
55
|
],
|
package/src/vite/plugin.ts
CHANGED
|
@@ -16,6 +16,11 @@ export interface AsyncContextPluginOptions {
|
|
|
16
16
|
readonly exclude? : ModuleFilter | undefined;
|
|
17
17
|
/** The runtime module. The default is `async-browser-context/runtime`. */
|
|
18
18
|
readonly runtime? : string | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Give the name of each async function to the callback hooks. The default
|
|
21
|
+
* is `true`. Refer to the option `names` of the Babel preset.
|
|
22
|
+
*/
|
|
23
|
+
readonly names? : boolean | undefined;
|
|
19
24
|
/**
|
|
20
25
|
* Also transform the modules of server-side rendering. The default is
|
|
21
26
|
* `false`, because on Node.js the native `AsyncLocalStorage` keeps the
|
|
@@ -84,7 +89,7 @@ export function asyncContext(options : AsyncContextPluginOptions = {}) : Plugin
|
|
|
84
89
|
configFile : false,
|
|
85
90
|
sourceType : "unambiguous",
|
|
86
91
|
sourceMaps : true,
|
|
87
|
-
presets : [[preset, { runtime }]],
|
|
92
|
+
presets : [[preset, { runtime, names : options.names }]],
|
|
88
93
|
});
|
|
89
94
|
const changed = (result?.metadata as Record<string, unknown> | undefined)?.[METADATA_KEY] === true;
|
|
90
95
|
if (result?.code == null || !changed) {
|