From dc4ae4b4cfca67058cbac5a8d3f9a397f442e35a Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Mon, 3 Feb 2020 22:21:37 +0100 Subject: [PATCH] fix(animations): false positive when detecting Node in Webpack builds (#35134) We have to do some extra work in the animations module when we identify a Node environment which we determine based on the `process` global. The problem is that by default Webpack will polyfill the `process`, causing us to incorrectly identify it. These changes make it so that the check isn't thrown off by Webpack. Fixes #35117. PR Close #35134 --- packages/animations/browser/src/render/shared.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/animations/browser/src/render/shared.ts b/packages/animations/browser/src/render/shared.ts index 81f769d713..e3d6119fad 100644 --- a/packages/animations/browser/src/render/shared.ts +++ b/packages/animations/browser/src/render/shared.ts @@ -15,12 +15,17 @@ import {AnimationDriver} from '../../src/render/animation_driver'; // types. `process` is just declared locally here as a result. declare const process: any; -export function isBrowser() { +export function isBrowser(): boolean { return (typeof window !== 'undefined' && typeof window.document !== 'undefined'); } -export function isNode() { - return (typeof process !== 'undefined'); +export function isNode(): boolean { + // Checking only for `process` isn't enough to identify whether or not we're in a Node + // environment, because Webpack by default will polyfill the `process`. While we can discern + // that Webpack polyfilled it by looking at `process.browser`, it's very Webpack-specific and + // might not be future-proof. Instead we look at the stringified version of `process` which + // is `[object process]` in Node and `[object Object]` when polyfilled. + return typeof process !== 'undefined' && {}.toString.call(process) === '[object process]'; } export function optimizeGroupPlayer(players: AnimationPlayer[]): AnimationPlayer {