Skip to content

Commit bde0986

Browse files
committed
Consolidate checks that test if the current environment is Node
1 parent eac528e commit bde0986

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

Diff for: src/compiler/performanceCore.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Version, VersionRange } from "./_namespaces/ts";
1+
import { isNodeLikeSystem, Version, VersionRange } from "./_namespaces/ts";
22

33
// The following definitions provide the minimum compatible support for the Web Performance User Timings API
44
// between browsers and NodeJS:
@@ -80,7 +80,7 @@ function tryGetWebPerformanceHooks(): PerformanceHooks | undefined {
8080
}
8181

8282
function tryGetNodePerformanceHooks(): PerformanceHooks | undefined {
83-
if (typeof process !== "undefined" && process.nextTick && !process.browser && typeof module === "object" && typeof require === "function") {
83+
if (isNodeLikeSystem()) {
8484
try {
8585
let performance: Performance;
8686
const { performance: nodePerformance, PerformanceObserver } = require("perf_hooks") as typeof import("perf_hooks");

Diff for: src/compiler/sys.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,17 @@ export function getNodeMajorVersion(): number | undefined {
14291429
return parseInt(version.substring(1, dot));
14301430
}
14311431

1432+
/** @internal */
1433+
export function isNodeLikeSystem(): boolean {
1434+
// Note: we don't use the presence of `require` to check if we are in Node;
1435+
// when bundled using esbuild, this function will be rewritten to `__require`
1436+
// and definitely exist.
1437+
return typeof process !== "undefined"
1438+
&& process.nextTick
1439+
&& !process.browser
1440+
&& typeof module === "object";
1441+
}
1442+
14321443
// TODO: GH#18217 this is used as if it's certainly defined in many places.
14331444
// eslint-disable-next-line prefer-const
14341445
export let sys: System = (() => {
@@ -1978,9 +1989,7 @@ export let sys: System = (() => {
19781989
}
19791990

19801991
let sys: System | undefined;
1981-
if (typeof process !== "undefined" && process.nextTick && !process.browser && typeof require !== "undefined") {
1982-
// process and process.nextTick checks if current environment is node-like
1983-
// process.browser check excludes webpack and browserify
1992+
if (isNodeLikeSystem()) {
19841993
sys = getNodeSystem();
19851994
}
19861995
if (sys) {

Diff for: src/services/globalThisShim.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TypeScriptServicesFactory, versionMajorMinor } from "./_namespaces/ts";
1+
import { isNodeLikeSystem, TypeScriptServicesFactory, versionMajorMinor } from "./_namespaces/ts";
22

33
// We polyfill `globalThis` here so re can reliably patch the global scope
44
// in the contexts we want to in the same way across script and module formats
@@ -47,7 +47,7 @@ declare global {
4747

4848
// if `process` is undefined, we're probably not running in node - patch legacy members onto the global scope
4949
// @ts-ignore
50-
if (typeof process === "undefined" || process.browser) {
50+
if (!isNodeLikeSystem()) {
5151
/// TODO: this is used by VS, clean this up on both sides of the interface
5252

5353
//@ts-ignore

0 commit comments

Comments
 (0)