forked from styled-components/babel-plugin-styled-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added transformations to fix tree-shaking issues when using static pr…
…operties (styled-components#245)
- Loading branch information
Showing
12 changed files
with
369 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Run demo", | ||
"preLaunchTask": "npm: build", | ||
"program": "${workspaceFolder}/node_modules/.bin/babel", | ||
"cwd": "${workspaceFolder}/demo", | ||
"args": ["demoComponents.js"] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"editor.formatOnSave": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
// See https://go.microsoft.com/fwlink/?LinkId=733558 | ||
// for the documentation about the tasks.json format | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"type": "npm", | ||
"script": "build", | ||
"group": { | ||
"kind": "build", | ||
"isDefault": true | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
plugins: [ | ||
'@babel/plugin-proposal-class-properties', | ||
[require('../lib'), { pure: true }], | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React from 'react' | ||
import styled from 'styled-components' | ||
|
||
const Wrapper = styled.div` | ||
color: blue; | ||
` | ||
|
||
export function FunctionComponent() { | ||
return React.createElement(Wrapper) | ||
} | ||
FunctionComponent.displayName = 'FancyName1' | ||
FunctionComponent.defaultProps = {} | ||
|
||
export class ClassComponent extends React.Component { | ||
static displayName = 'FancyName2' | ||
static defaultProps = {} | ||
|
||
render() { | ||
return React.createElement(Wrapper) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "babel-pugin-styled-components-demo", | ||
"scripts": { | ||
"transpile": "../node_modules/.bin/babel demoComponents.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/** | ||
* Adapted from https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types/blob/master/src/isStatelessComponent.js | ||
*/ | ||
|
||
import { isStyled } from '../utils/detectors' | ||
|
||
const traversed = Symbol('traversed') | ||
|
||
function isJSXElementOrReactCreateElement( | ||
path, | ||
filterFn = null // optional filter function to match only certain kinds of React elements | ||
) { | ||
let visited = false | ||
|
||
path.traverse({ | ||
CallExpression(path2) { | ||
const callee = path2.get('callee') | ||
|
||
if ( | ||
callee.matchesPattern('React.createElement') || | ||
callee.matchesPattern('React.cloneElement') || | ||
callee.node.name === 'cloneElement' | ||
) { | ||
visited = filterFn ? filterFn(path2) : true | ||
} | ||
}, | ||
JSXElement(path2) { | ||
visited = filterFn ? filterFn(path2) : true | ||
}, | ||
}) | ||
|
||
return visited | ||
} | ||
|
||
function isReturningJSXElement(path, state, filterFn = null, iteration = 0) { | ||
// Early exit for ArrowFunctionExpressions, there is no ReturnStatement node. | ||
if ( | ||
path.node.init && | ||
path.node.init.body && | ||
isJSXElementOrReactCreateElement(path, filterFn) | ||
) { | ||
return true | ||
} | ||
|
||
if (iteration > 20) { | ||
throw new Error('transform-react-remove-prop-type: infinite loop detected.') | ||
} | ||
|
||
let visited = false | ||
|
||
path.traverse({ | ||
ReturnStatement(path2) { | ||
// We have already found what we are looking for. | ||
if (visited) { | ||
return | ||
} | ||
|
||
const argument = path2.get('argument') | ||
|
||
// Nothing is returned | ||
if (!argument.node) { | ||
return | ||
} | ||
|
||
if (isJSXElementOrReactCreateElement(path2, filterFn)) { | ||
visited = true | ||
return | ||
} | ||
|
||
if (argument.node.type === 'CallExpression') { | ||
const name = argument.get('callee').node.name | ||
const binding = path.scope.getBinding(name) | ||
|
||
if (!binding) { | ||
return | ||
} | ||
|
||
// Prevents infinite traverse loop. | ||
if (binding.path[traversed]) { | ||
return | ||
} | ||
|
||
binding.path[traversed] = true | ||
|
||
if ( | ||
isReturningJSXElement(binding.path, state, filterFn, iteration + 1) | ||
) { | ||
visited = true | ||
} | ||
} | ||
}, | ||
}) | ||
|
||
return visited | ||
} | ||
|
||
/** | ||
* IMPORTANT: This function assumes that the given path is a VariableDeclarator or FunctionDeclaration, | ||
* and will return false positives otherwise. If a more robust version is needed in the future, | ||
* see https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types/blob/master/src/isStatelessComponent.js | ||
* | ||
* Returns true if the given path is a React function component definition | ||
* @param {Path<VariableDeclarator | FunctionDeclaration>} path | ||
*/ | ||
export function isFunctionComponent( | ||
path, | ||
state, | ||
types, | ||
mustConsumeStyledComponent = false // only return true if the component might render a styled component | ||
) { | ||
let filterFn | ||
if (mustConsumeStyledComponent) { | ||
filterFn = reactElementPath => { | ||
// look up the component and check if it's a styled component | ||
const componentId = reactElementPath.isJSXElement() | ||
? reactElementPath.node.openingElement.name | ||
: reactElementPath.node.arguments[0] | ||
const binding = reactElementPath.scope.getBinding(componentId.name) | ||
if (binding && binding.path.isVariableDeclarator()) { | ||
const { init } = binding.path.node | ||
if ( | ||
types.isCallExpression(init) && | ||
types.isCallExpression(init.callee) && | ||
isStyled(types)(init.callee, state) | ||
) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
} | ||
|
||
if (isReturningJSXElement(path, state, filterFn)) { | ||
return true | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.