Skip to content

Commit

Permalink
[core] Fix ReferenceError in browser builds without process polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Oct 9, 2018
1 parent a308ba5 commit 8cf7abb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
6 changes: 3 additions & 3 deletions packages/material-ui/src/Button/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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.',
);
Expand Down
3 changes: 2 additions & 1 deletion packages/material-ui/src/styles/createTypography.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions packages/material-ui/src/utils/helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export function contains<O1 extends O2, O2>(obj: O1, pred: O2): boolean;
export function findIndex(arr: any[], pred: any): number;
export function find<T>(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;
7 changes: 7 additions & 0 deletions packages/material-ui/src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
25 changes: 24 additions & 1 deletion packages/material-ui/src/utils/helpers.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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);
});
});
});
});

0 comments on commit 8cf7abb

Please sign in to comment.