Skip to content

Commit f4387ed

Browse files
committed
chore: use process.getBuiltinModule
1 parent 8302adc commit f4387ed

2 files changed

Lines changed: 20 additions & 14 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { AsyncLocalStorage } from 'node:async_hooks';
2+
3+
type ProcessWithBuiltins = {
4+
getBuiltinModule?: (id: string) => unknown;
5+
};
6+
7+
/** @internal */
8+
export const getAsyncLocalStorage = () => {
9+
const process = (globalThis as { process?: ProcessWithBuiltins }).process;
10+
return (
11+
process?.getBuiltinModule?.('node:async_hooks') as
12+
| { AsyncLocalStorage?: new <T>() => AsyncLocalStorage<T> }
13+
| undefined
14+
)?.AsyncLocalStorage;
15+
};

packages/qwik/src/core/use/use-locale.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { tryGetInvokeContext } from './use-core';
2+
import { getAsyncLocalStorage } from '../shared/platform/async-local-storage';
23
import { isServer } from '@qwik.dev/core/build';
34
import type { AsyncLocalStorage } from 'node:async_hooks';
45

@@ -7,20 +8,10 @@ let _locale: string | undefined = undefined;
78
let localAsyncStore: AsyncLocalStorage<string> | undefined;
89

910
if (isServer) {
10-
// Hide the `node:` specifier from static analysis so consumers bundling the
11-
// published core for the browser without the Qwik Vite plugin (e.g. raw
12-
// webpack used by Cypress's webpack-preprocessor) don't fail with
13-
// "UnhandledSchemeError" on this dead-code branch. The `.join` defeats
14-
// Rollup's constant folding so the literal isn't restored at qwik build
15-
// time, and the magic comments cover Vite/webpack at consumer build time.
16-
const moduleName = ['node', 'async_hooks'].join(':');
17-
import(/* @vite-ignore */ /* webpackIgnore: true */ moduleName)
18-
.then((module: typeof import('node:async_hooks')) => {
19-
localAsyncStore = new module.AsyncLocalStorage();
20-
})
21-
.catch(() => {
22-
// ignore if AsyncLocalStorage is not available
23-
});
11+
const AsyncLocalStorage = getAsyncLocalStorage();
12+
if (AsyncLocalStorage) {
13+
localAsyncStore = new AsyncLocalStorage();
14+
}
2415
}
2516

2617
/**

0 commit comments

Comments
 (0)