From 8cf7abbd55617443cf272a5bcd26f9bdffeeee6e Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Tue, 9 Oct 2018 08:31:50 +0200 Subject: [PATCH] [core] Fix ReferenceError in browser builds without process polyfill --- packages/material-ui/src/Button/Button.js | 6 ++--- .../src/styles/createTypography.js | 3 ++- packages/material-ui/src/utils/helpers.d.ts | 1 + packages/material-ui/src/utils/helpers.js | 7 ++++++ .../material-ui/src/utils/helpers.test.js | 25 ++++++++++++++++++- 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/packages/material-ui/src/Button/Button.js b/packages/material-ui/src/Button/Button.js index d288d918b19665..d852f99c265a9c 100644 --- a/packages/material-ui/src/Button/Button.js +++ b/packages/material-ui/src/Button/Button.js @@ -7,7 +7,7 @@ import warning from 'warning'; import withStyles from '../styles/withStyles'; import { fade } from '../styles/colorManipulator'; import ButtonBase from '../ButtonBase'; -import { capitalize } from '../utils/helpers'; +import { capitalize, getEnv } from '../utils/helpers'; export const styles = theme => ({ /* Styles applied to the root element. */ @@ -227,13 +227,13 @@ function Button(props) { } = props; warning( - process.env.MUI_SUPPRESS_DEPRECATION_WARNINGS || variant !== 'flat', + getEnv('MUI_SUPPRESS_DEPRECATION_WARNINGS') || variant !== 'flat', 'Material-UI: The `flat` Button variant will be removed in ' + 'the next major release. `text` is equivalent and should be used instead.', ); warning( - process.env.MUI_SUPPRESS_DEPRECATION_WARNINGS || variant !== 'raised', + getEnv('MUI_SUPPRESS_DEPRECATION_WARNINGS') || variant !== 'raised', 'Material-UI: The `raised` Button variant will be removed in ' + 'the next major release. `contained` is equivalent and should be used instead.', ); diff --git a/packages/material-ui/src/styles/createTypography.js b/packages/material-ui/src/styles/createTypography.js index 7941a403226219..ceb806953318ab 100644 --- a/packages/material-ui/src/styles/createTypography.js +++ b/packages/material-ui/src/styles/createTypography.js @@ -1,6 +1,7 @@ import deepmerge from 'deepmerge'; // < 1kb payload overhead when lodash/merge is > 3kb. import warning from 'warning'; import typographyMigration from './typographyMigration'; +import { getEnv } from '../utils/helpers'; function round(value) { return Math.round(value * 1e5) / 1e5; @@ -27,7 +28,7 @@ export default function createTypography(palette, typography) { // Tell Material-UI what's the font-size on the html element. // 16px is the default font-size used by browsers. htmlFontSize = 16, - suppressDeprecationWarnings = process.env.MUI_SUPPRESS_DEPRECATION_WARNINGS, + suppressDeprecationWarnings = getEnv('MUI_SUPPRESS_DEPRECATION_WARNINGS'), useNextVariants = false, // Apply the CSS properties to all the variants. allVariants, diff --git a/packages/material-ui/src/utils/helpers.d.ts b/packages/material-ui/src/utils/helpers.d.ts index ff479a3422c6ef..eaa7caea7599d7 100644 --- a/packages/material-ui/src/utils/helpers.d.ts +++ b/packages/material-ui/src/utils/helpers.d.ts @@ -3,5 +3,6 @@ export function contains(obj: O1, pred: O2): boolean; export function findIndex(arr: any[], pred: any): number; export function find(arr: T[], pred: any): T; export function createChainedFunction(...funcs: ChainedFunction[]): (...args: any[]) => never; +export function getEnv(name: string): any; export type ChainedFunction = ((...args: any[]) => void) | undefined | null; diff --git a/packages/material-ui/src/utils/helpers.js b/packages/material-ui/src/utils/helpers.js index 6c2006e9808325..918bcb5a2c2027 100644 --- a/packages/material-ui/src/utils/helpers.js +++ b/packages/material-ui/src/utils/helpers.js @@ -64,3 +64,10 @@ export function createChainedFunction(...funcs) { () => {}, ); } + +/** + * guarded getter for process.env.* + */ +export function getEnv(name) { + return typeof process !== 'undefined' && process.env ? process.env[name] : undefined; +} diff --git a/packages/material-ui/src/utils/helpers.test.js b/packages/material-ui/src/utils/helpers.test.js index fe1f9e2a8f3118..95fffe1da67975 100644 --- a/packages/material-ui/src/utils/helpers.test.js +++ b/packages/material-ui/src/utils/helpers.test.js @@ -1,5 +1,5 @@ import { assert } from 'chai'; -import { capitalize, contains, find } from './helpers'; +import { capitalize, contains, find, getEnv } from './helpers'; describe('utils/helpers.js', () => { describe('capitalize', () => { @@ -33,4 +33,27 @@ describe('utils/helpers.js', () => { assert.strictEqual(contains(obj, failPred), false); }); }); + + describe('getEnv', () => { + it('returns variables defined in process.env', () => { + assert.strictEqual(getEnv('NODE_ENV'), process.env.NODE_ENV); + }); + + describe('in browser', () => { + let oldEnv; + + beforeEach(() => { + oldEnv = process.env; + process.env = undefined; + }); + + afterEach(() => { + process.env = oldEnv; + }); + + it('does not crash with an undefined process.env', () => { + assert.strictEqual(getEnv('NODE_ENV'), undefined); + }); + }); + }); });