Skip to content

Commit 0fb1ba9

Browse files
committed
Consolidate checks that test if the current environment is Node
1 parent b5139d7 commit 0fb1ba9

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

Diff for: src/compiler/core.ts

+13
Original file line numberDiff line numberDiff line change
@@ -2910,3 +2910,16 @@ function trimEndImpl(s: string) {
29102910
}
29112911
return s.slice(0, end + 1);
29122912
}
2913+
2914+
declare const process: any;
2915+
2916+
/** @internal */
2917+
export function isNodeLikeSystem(): boolean {
2918+
// Note: we don't use the presence of `require` to check if we are in Node;
2919+
// when bundled using esbuild, this function will be rewritten to `__require`
2920+
// and definitely exist.
2921+
return typeof process !== "undefined"
2922+
&& process.nextTick
2923+
&& !process.browser
2924+
&& typeof module === "object";
2925+
}

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
AssertionLevel, closeFileWatcher, closeFileWatcherOf, combinePaths, Comparison, contains, containsPath,
33
createGetCanonicalFileName, createMultiMap, Debug, directorySeparator, emptyArray, emptyFileSystemEntries, endsWith,
44
enumerateInsertsAndDeletes, ESMap, FileSystemEntries, getDirectoryPath, getFallbackOptions,
5-
getNormalizedAbsolutePath, getRelativePathToDirectoryOrUrl, getRootLength, getStringComparer, isArray, isString,
5+
getNormalizedAbsolutePath, getRelativePathToDirectoryOrUrl, getRootLength, getStringComparer, isArray, isNodeLikeSystem, isString,
66
Map, mapDefined, matchesExclude, matchFiles, memoize, noop, normalizePath, normalizeSlashes, orderedRemoveItem,
77
Path, perfLogger, PollingWatchKind, RequireResult, resolveJSModule, some, startsWith, stringContains, timestamp,
88
unorderedRemoveItem, WatchDirectoryKind, WatchFileKind, WatchOptions, writeFileEnsuringDirectories,
@@ -1974,9 +1974,7 @@ export let sys: System = (() => {
19741974
}
19751975

19761976
let sys: System | undefined;
1977-
if (typeof process !== "undefined" && process.nextTick && !process.browser && typeof require !== "undefined") {
1978-
// process and process.nextTick checks if current environment is node-like
1979-
// process.browser check excludes webpack and browserify
1977+
if (isNodeLikeSystem()) {
19801978
sys = getNodeSystem();
19811979
}
19821980
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)