Skip to content

Commit 423ad68

Browse files
committed
Consolidate checks that test if the current environment is Node
When bundled, it's very likely that the function "require" will actually exist at runtime, so we can't use this to determine if we are running in Node. Consolidate that logic and use other things to check instead. This is still not perfectly accurate, but I don't want to change this _too_ much, lest someone downstream depend on our inconsistent logic. There are yet other places this commit does not fix; search for "typeof process" for more examples.
1 parent 0a33553 commit 423ad68

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

src/compiler/core.ts

+16
Original file line numberDiff line numberDiff line change
@@ -2914,3 +2914,19 @@ function trimEndImpl(s: string) {
29142914
}
29152915
return s.slice(0, end + 1);
29162916
}
2917+
2918+
declare const process: any;
2919+
2920+
/** @internal */
2921+
export function isNodeLikeSystem(): boolean {
2922+
// This is defined here rather than in sys.ts to prevent a cycle from its
2923+
// use in performanceCore.ts.
2924+
//
2925+
// We don't use the presence of `require` to check if we are in Node;
2926+
// when bundled using esbuild, this function will be rewritten to `__require`
2927+
// and definitely exist.
2928+
return typeof process !== "undefined"
2929+
&& process.nextTick
2930+
&& !process.browser
2931+
&& typeof module === "object";
2932+
}

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");

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) {

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)