Skip to content

Commit

Permalink
Remove prop types in production (#59)
Browse files Browse the repository at this point in the history
* Remove prop types in production

* Configure webpack to set the NODE_ENV
  • Loading branch information
realityking authored and zpao committed Apr 20, 2019
1 parent 302da65 commit 72621a4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"lib"
],
"dependencies": {
"loose-envify": "^1.4.0",
"prop-types": "^15.6.0",
"qr.js": "0.0.0"
},
Expand Down Expand Up @@ -56,5 +57,10 @@
"react-dom": "^16.2.0",
"webpack": "^4.28.1",
"webpack-cli": "^3.1.2"
},
"browserify": {
"transform": [
"loose-envify"
]
}
}
14 changes: 10 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ const DEFAULT_PROPS = {
includeMargin: false,
};

const PROP_TYPES = {
const PROP_TYPES = process.env.NODE_ENV !== 'production' ? {
value: PropTypes.string.isRequired,
size: PropTypes.number,
level: PropTypes.oneOf(['L', 'M', 'Q', 'H']),
bgColor: PropTypes.string,
fgColor: PropTypes.string,
includeMargin: PropTypes.bool,
};
} : {};

const MARGIN_SIZE = 4;

Expand Down Expand Up @@ -132,7 +132,6 @@ class QRCodeCanvas extends React.PureComponent<QRProps> {
_canvas: ?HTMLCanvasElement;

static defaultProps = DEFAULT_PROPS;
static propTypes = PROP_TYPES;

componentDidMount() {
this.update();
Expand Down Expand Up @@ -222,9 +221,12 @@ class QRCodeCanvas extends React.PureComponent<QRProps> {
}
}

if (process.env.NODE_ENV !== 'production') {
QRCodeCanvas.propTypes = PROP_TYPES;
}

class QRCodeSVG extends React.PureComponent<QRProps> {
static defaultProps = DEFAULT_PROPS;
static propTypes = PROP_TYPES;

render() {
const {
Expand Down Expand Up @@ -273,6 +275,10 @@ class QRCodeSVG extends React.PureComponent<QRProps> {
}
}

if (process.env.NODE_ENV !== 'production') {
QRCodeSVG.propTypes = PROP_TYPES;
}

type RootProps = QRProps & {renderAs: 'svg' | 'canvas'};
const QRCode = (props: RootProps): React.Node => {
const {renderAs, ...otherProps} = props;
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ module.exports = {
rules: [{test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'}],
},
target: 'web',
mode: 'development',
mode: 'production',
};
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2753,7 +2753,7 @@ lodash@^4.17.10, lodash@^4.17.11, lodash@^4.17.5:
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==

loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.3.1:
loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.3.1, loose-envify@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==
Expand Down

0 comments on commit 72621a4

Please sign in to comment.