Skip to content

Consolidate checks that test if the current environment is Node #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2914,3 +2914,19 @@ function trimEndImpl(s: string) {
}
return s.slice(0, end + 1);
}

declare const process: any;

/** @internal */
export function isNodeLikeSystem(): boolean {
// This is defined here rather than in sys.ts to prevent a cycle from its
// use in performanceCore.ts.
//
// We don't use the presence of `require` to check if we are in Node;
// when bundled using esbuild, this function will be rewritten to `__require`
// and definitely exist.
return typeof process !== "undefined"
&& process.nextTick
&& !process.browser
&& typeof module === "object";
}
4 changes: 2 additions & 2 deletions src/compiler/performanceCore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Version, VersionRange } from "./_namespaces/ts";
import { isNodeLikeSystem, Version, VersionRange } from "./_namespaces/ts";

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

function tryGetNodePerformanceHooks(): PerformanceHooks | undefined {
if (typeof process !== "undefined" && process.nextTick && !process.browser && typeof module === "object" && typeof require === "function") {
if (isNodeLikeSystem()) {
try {
let performance: Performance;
const { performance: nodePerformance, PerformanceObserver } = require("perf_hooks") as typeof import("perf_hooks");
Expand Down
6 changes: 2 additions & 4 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
AssertionLevel, closeFileWatcher, closeFileWatcherOf, combinePaths, Comparison, contains, containsPath,
createGetCanonicalFileName, createMultiMap, Debug, directorySeparator, emptyArray, emptyFileSystemEntries, endsWith,
enumerateInsertsAndDeletes, ESMap, FileSystemEntries, getDirectoryPath, getFallbackOptions,
getNormalizedAbsolutePath, getRelativePathToDirectoryOrUrl, getRootLength, getStringComparer, isArray, isString,
getNormalizedAbsolutePath, getRelativePathToDirectoryOrUrl, getRootLength, getStringComparer, isArray, isNodeLikeSystem, isString,
Map, mapDefined, matchesExclude, matchFiles, memoize, noop, normalizePath, normalizeSlashes, orderedRemoveItem,
Path, perfLogger, PollingWatchKind, RequireResult, resolveJSModule, some, startsWith, stringContains, timestamp,
unorderedRemoveItem, WatchDirectoryKind, WatchFileKind, WatchOptions, writeFileEnsuringDirectories,
Expand Down Expand Up @@ -1974,9 +1974,7 @@ export let sys: System = (() => {
}

let sys: System | undefined;
if (typeof process !== "undefined" && process.nextTick && !process.browser && typeof require !== "undefined") {
// process and process.nextTick checks if current environment is node-like
// process.browser check excludes webpack and browserify
if (isNodeLikeSystem()) {
sys = getNodeSystem();
}
if (sys) {
Expand Down
4 changes: 2 additions & 2 deletions src/services/globalThisShim.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TypeScriptServicesFactory, versionMajorMinor } from "./_namespaces/ts";
import { isNodeLikeSystem, TypeScriptServicesFactory, versionMajorMinor } from "./_namespaces/ts";

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

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

//@ts-ignore
Expand Down