babel-plugin-transform-react-remove-prop-types
Advanced tools
Comparing version 0.4.1 to 0.4.2
@@ -24,3 +24,4 @@ 'use strict'; | ||
visitedKey: 'transform-react-remove-prop-types' + Date.now(), | ||
wrapperIfTemplate: template('\n if (process.env.NODE_ENV !== "production") {\n NODE;\n }\n '), | ||
unsafeWrapTemplate: template('\n if (process.env.NODE_ENV !== "production") {\n NODE;\n }\n '), | ||
wrapTemplate: template('\n LEFT = process.env.NODE_ENV !== "production" ? RIGHT : {}\n '), | ||
mode: state.opts.mode || 'remove', | ||
@@ -106,29 +107,33 @@ ignoreFilenames: ignoreFilenames, | ||
if (globalOptions.removeImport && globalOptions.mode === 'remove') { | ||
programPath.scope.crawl(); | ||
if (globalOptions.removeImport) { | ||
if (globalOptions.mode === 'remove') { | ||
programPath.scope.crawl(); | ||
programPath.traverse({ | ||
ImportDeclaration: function ImportDeclaration(path) { | ||
var _path$node = path.node, | ||
source = _path$node.source, | ||
specifiers = _path$node.specifiers; | ||
programPath.traverse({ | ||
ImportDeclaration: function ImportDeclaration(path) { | ||
var _path$node = path.node, | ||
source = _path$node.source, | ||
specifiers = _path$node.specifiers; | ||
if (source.value !== 'prop-types') { | ||
return; | ||
} | ||
var haveUsedSpecifiers = specifiers.some(function (specifier) { | ||
var importedIdentifierName = specifier.local.name; | ||
if (source.value !== 'prop-types') { | ||
return; | ||
} | ||
var haveUsedSpecifiers = specifiers.some(function (specifier) { | ||
var importedIdentifierName = specifier.local.name; | ||
var _path$scope$getBindin = path.scope.getBinding(importedIdentifierName), | ||
referencePaths = _path$scope$getBindin.referencePaths; | ||
var _path$scope$getBindin = path.scope.getBinding(importedIdentifierName), | ||
referencePaths = _path$scope$getBindin.referencePaths; | ||
return referencePaths.length > 0; | ||
}); | ||
return referencePaths.length > 0; | ||
}); | ||
if (!haveUsedSpecifiers) { | ||
path.remove(); | ||
if (!haveUsedSpecifiers) { | ||
path.remove(); | ||
} | ||
path.stop(); | ||
} | ||
path.stop(); | ||
} | ||
}); | ||
}); | ||
} else { | ||
throw new Error('react-remove-prop-types: removeImport and mode=remove can not be used at the same time.'); | ||
} | ||
} | ||
@@ -135,0 +140,0 @@ } |
@@ -26,3 +26,4 @@ 'use strict'; | ||
var visitedKey = globalOptions.visitedKey, | ||
wrapperIfTemplate = globalOptions.wrapperIfTemplate, | ||
unsafeWrapTemplate = globalOptions.unsafeWrapTemplate, | ||
wrapTemplate = globalOptions.wrapTemplate, | ||
mode = globalOptions.mode, | ||
@@ -37,2 +38,9 @@ ignoreFilenames = globalOptions.ignoreFilenames, | ||
// Prevent infinity loop. | ||
if (path.node[visitedKey]) { | ||
return; | ||
} | ||
path.node[visitedKey] = true; | ||
if (mode === 'remove') { | ||
@@ -45,10 +53,7 @@ // remove() crash in some conditions. | ||
} | ||
} else if (mode === 'wrap') { | ||
// Prevent infinity loop. | ||
if (path.node[visitedKey]) { | ||
return; | ||
} | ||
path.node[visitedKey] = true; | ||
return; | ||
} | ||
if (mode === 'wrap' || mode === 'unsafe-wrap') { | ||
switch (options.type) { | ||
@@ -79,3 +84,2 @@ // This is legacy, we do not optimize it. | ||
pathClassDeclaration.insertAfter(node); | ||
path.remove(); | ||
@@ -87,5 +91,13 @@ break; | ||
case 'stateless': | ||
path.replaceWith(wrapperIfTemplate({ | ||
NODE: path.node | ||
})); | ||
if (mode === 'unsafe-wrap') { | ||
path.replaceWith(unsafeWrapTemplate({ | ||
NODE: path.node | ||
})); | ||
} else { | ||
path.replaceWith(wrapTemplate({ | ||
LEFT: path.node.left, | ||
RIGHT: path.node.right | ||
})); | ||
} | ||
path.node[visitedKey] = true; | ||
break; | ||
@@ -96,5 +108,7 @@ | ||
} | ||
} else { | ||
throw new Error('transform-react-remove-prop-type: unsupported mode ' + mode + '.'); | ||
return; | ||
} | ||
throw new Error('transform-react-remove-prop-type: unsupported mode ' + mode + '.'); | ||
} |
{ | ||
"name": "babel-plugin-transform-react-remove-prop-types", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"description": "Remove unnecessary React propTypes from the production build", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -117,9 +117,20 @@ # babel-plugin-transform-react-remove-prop-types | ||
```js | ||
Component.propTypes = process.env.NODE_ENV !== "production" ? { | ||
// ... | ||
} : {}; | ||
``` | ||
Accessing `Component.propTypes.className` won't throw. It's a tradeoff between the size of the output file and the likelihood libraries like [react-native-hyperlink](https://github.com/obipawan/react-native-hyperlink/pull/11) breaks. | ||
- `unsafe-wrap`: | ||
the `propTypes` definitions are wrapped with the following code: | ||
```js | ||
if (process.env.NODE_ENV !== "production") { | ||
// ... | ||
Component.propTypes = { | ||
// ... | ||
} | ||
} | ||
``` | ||
Accessing `Component.propTypes.className` will throw. | ||
The `wrap` mode is targeting react libraries like [material-ui](https://github.com/callemall/material-ui). | ||
It's not intended to be used in userland. | ||
The *wrap* modes are targeting React libraries like [material-ui](https://github.com/callemall/material-ui) or [react-native-web](https://github.com/necolas/react-native-web). | ||
They are not intended to be used by application authors. | ||
@@ -164,3 +175,3 @@ ### `removeImport` | ||
- Not parsing the `node_modules`. | ||
If you do, test that things are still working before shipping into production. | ||
If you do, test that your code is still working before shipping into production. | ||
@@ -167,0 +178,0 @@ [eslint-plugin-react has a rule forbid-foreign-prop-types](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/forbid-foreign-prop-types.md) that can help you make this plugin safer to use. |
@@ -61,3 +61,3 @@ // @flow weak | ||
visitedKey: `transform-react-remove-prop-types${Date.now()}`, | ||
wrapperIfTemplate: template(` | ||
unsafeWrapTemplate: template(` | ||
if (process.env.NODE_ENV !== "production") { | ||
@@ -67,2 +67,5 @@ NODE; | ||
`), | ||
wrapTemplate: template(` | ||
LEFT = process.env.NODE_ENV !== "production" ? RIGHT : {} | ||
`), | ||
mode: state.opts.mode || 'remove', | ||
@@ -150,23 +153,29 @@ ignoreFilenames, | ||
if (globalOptions.removeImport && globalOptions.mode === 'remove') { | ||
programPath.scope.crawl(); | ||
if (globalOptions.removeImport) { | ||
if (globalOptions.mode === 'remove') { | ||
programPath.scope.crawl(); | ||
programPath.traverse({ | ||
ImportDeclaration(path) { | ||
const { source, specifiers } = path.node; | ||
if (source.value !== 'prop-types') { | ||
return; | ||
} | ||
const haveUsedSpecifiers = specifiers.some((specifier) => { | ||
const importedIdentifierName = specifier.local.name; | ||
const { referencePaths } = path.scope.getBinding(importedIdentifierName); | ||
return referencePaths.length > 0; | ||
}); | ||
programPath.traverse({ | ||
ImportDeclaration(path) { | ||
const { source, specifiers } = path.node; | ||
if (source.value !== 'prop-types') { | ||
return; | ||
} | ||
const haveUsedSpecifiers = specifiers.some((specifier) => { | ||
const importedIdentifierName = specifier.local.name; | ||
const { referencePaths } = path.scope.getBinding(importedIdentifierName); | ||
return referencePaths.length > 0; | ||
}); | ||
if (!haveUsedSpecifiers) { | ||
path.remove(); | ||
} | ||
path.stop(); | ||
}, | ||
}); | ||
if (!haveUsedSpecifiers) { | ||
path.remove(); | ||
} | ||
path.stop(); | ||
}, | ||
}); | ||
} else { | ||
throw new Error( | ||
'react-remove-prop-types: removeImport and mode=remove can not be used at the same time.', | ||
); | ||
} | ||
} | ||
@@ -173,0 +182,0 @@ }, |
@@ -21,3 +21,4 @@ // @flow weak | ||
visitedKey, | ||
wrapperIfTemplate, | ||
unsafeWrapTemplate, | ||
wrapTemplate, | ||
mode, | ||
@@ -32,2 +33,9 @@ ignoreFilenames, | ||
// Prevent infinity loop. | ||
if (path.node[visitedKey]) { | ||
return; | ||
} | ||
path.node[visitedKey] = true; | ||
if (mode === 'remove') { | ||
@@ -40,10 +48,7 @@ // remove() crash in some conditions. | ||
} | ||
} else if (mode === 'wrap') { | ||
// Prevent infinity loop. | ||
if (path.node[visitedKey]) { | ||
return; | ||
} | ||
path.node[visitedKey] = true; | ||
return; | ||
} | ||
if (mode === 'wrap' || mode === 'unsafe-wrap') { | ||
switch (options.type) { | ||
@@ -75,3 +80,2 @@ // This is legacy, we do not optimize it. | ||
pathClassDeclaration.insertAfter(node); | ||
path.remove(); | ||
@@ -83,7 +87,17 @@ break; | ||
case 'stateless': | ||
path.replaceWith(wrapperIfTemplate( | ||
{ | ||
NODE: path.node, | ||
}, | ||
)); | ||
if (mode === 'unsafe-wrap') { | ||
path.replaceWith(unsafeWrapTemplate( | ||
{ | ||
NODE: path.node, | ||
}, | ||
)); | ||
} else { | ||
path.replaceWith(wrapTemplate( | ||
{ | ||
LEFT: path.node.left, | ||
RIGHT: path.node.right, | ||
}, | ||
)); | ||
} | ||
path.node[visitedKey] = true; | ||
break; | ||
@@ -94,5 +108,7 @@ | ||
} | ||
} else { | ||
throw new Error(`transform-react-remove-prop-type: unsupported mode ${mode}.`); | ||
return; | ||
} | ||
throw new Error(`transform-react-remove-prop-type: unsupported mode ${mode}.`); | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28204
616
181