-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix so that we dont blow up when encountering an "export default… (
#4) * fix: fix so that we dont blow up when encountering an "export default class ..." also adds tests to cover various scenarios * fix: have wrapped classes actually return the class * fix: wrap in standard function instead of arrow function for better compatibility ideally we would be able to have it go through preset-env to get it to transform the arrow functions if necessary, but we dont appear to get that for free the way we are doing this. perhaps it can be investigated in the future * updated readme, added changelog, and prepared for publishing Co-authored-by: Matt Browne <[email protected]>
- Loading branch information
Showing
24 changed files
with
3,450 additions
and
49 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 |
---|---|---|
|
@@ -2,3 +2,5 @@ node_modules | |
*.log | ||
lib | ||
.DS_Store | ||
.idea | ||
*.tgz |
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,20 @@ | ||
# Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) | ||
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). | ||
|
||
## 0.2.0 | ||
|
||
### Fixed | ||
|
||
Fix bug when encountering `export default` with class components | ||
|
||
### Added | ||
|
||
Test suite | ||
|
||
## 0.1.0 | ||
|
||
Initial version |
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 |
---|---|---|
@@ -1,12 +1,37 @@ | ||
# babel-plugin-pure-static-props | ||
|
||
## DEPRECATED | ||
Fixes an issue with tree shaking that can occur when using static properties on React components using styled-components. | ||
|
||
This plugin is deprecated in favor of https://github.com/styled-components/babel-plugin-styled-components/pull/248. | ||
It's possible that something other than styled-components might cause a similar issue, in which case this plugin might still be useful, but I won't be maintaining it. (Also, my implementation for babel-plugin-styled-components is a bit more efficient than what I did here.) | ||
This plugin replaces static property assignments on React components (e.g. `MyComponent.defaultProps = {...}`) with `Object.assign()` statements annotated with `/*#__PURE__*/` comments so that tree-shaking will work correctly. | ||
|
||
--- | ||
## Install | ||
|
||
Fixes an issue with tree shaking that can occur when using static properties on React components using styled-components. | ||
```bash | ||
npm i -D babel-plugin-pure-static-props | ||
``` | ||
|
||
This plugin replaces static property assignments on React components (e.g. `MyComponent.defaultProps = {...}`) with `Object.assign()` statements annotated with `/*#__PURE__*/` comments so that tree-shaking will work correctly. | ||
Or: | ||
|
||
```bash | ||
yarn add -D babel-plugin-pure-static-props | ||
``` | ||
|
||
Then add the plugin to your Babel config as explained in the [Babel documentation](https://babeljs.io/docs/en/options#plugin-and-preset-options). | ||
|
||
Example `babel.config.js`: | ||
|
||
```js | ||
module.exports = { | ||
presets: ['@babel/preset-env'], | ||
plugins: [ | ||
'@babel/plugin-proposal-class-properties', | ||
'babel-plugin-pure-static-props', | ||
], | ||
} | ||
``` | ||
|
||
## Note on styled components | ||
|
||
The tree-shaking issue with static properties on React components also affects [styled components](https://styled-components.com/). This plugin only addresses tree-shaking for regular React components; it will not work on components created using the `styled` helper. | ||
|
||
A fix for styled components is in this PR: https://github.com/styled-components/babel-plugin-styled-components/pull/248. |
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
8 changes: 8 additions & 0 deletions
8
test/__fixtures__/dont-wrap-class-components-without-static-props/.babelrc
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,8 @@ | ||
{ | ||
"plugins": [ | ||
"@babel/plugin-proposal-class-properties", | ||
[ | ||
"../../../src" | ||
] | ||
] | ||
} |
7 changes: 7 additions & 0 deletions
7
test/__fixtures__/dont-wrap-class-components-without-static-props/code.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,7 @@ | ||
import React from 'react' | ||
|
||
export class MyComponent extends React.Component { | ||
render() { | ||
return <div>My Component</div> | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
test/__fixtures__/dont-wrap-class-components-without-static-props/output.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,6 @@ | ||
import React from 'react' | ||
export class MyComponent extends React.Component { | ||
render() { | ||
return <div>My Component</div> | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
test/__fixtures__/dont-wrap-function-components-without-static-props/code.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,5 @@ | ||
import React from 'react' | ||
|
||
export function FunctionComponent() { | ||
return <div>My Component</div> | ||
} |
4 changes: 4 additions & 0 deletions
4
test/__fixtures__/dont-wrap-function-components-without-static-props/output.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,4 @@ | ||
import React from 'react' | ||
export function FunctionComponent() { | ||
return <div>My Component</div> | ||
} |
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,8 @@ | ||
import React from 'react' | ||
|
||
export class MyComponent extends React.Component { | ||
static displayName = 'FancyName1' | ||
render() { | ||
return <div>My Component</div> | ||
} | ||
} |
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,28 @@ | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
}) | ||
} else { | ||
obj[key] = value | ||
} | ||
return obj | ||
} | ||
|
||
import React from 'react' | ||
export const MyComponent = | ||
/*#__PURE__*/ | ||
(function () { | ||
class MyComponent extends React.Component { | ||
render() { | ||
return <div>My Component</div> | ||
} | ||
} | ||
|
||
_defineProperty(MyComponent, 'displayName', 'FancyName1') | ||
|
||
return MyComponent | ||
})() |
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,8 @@ | ||
import React from 'react' | ||
|
||
export class MyComponent extends React.PureComponent { | ||
static displayName = 'FancyName1' | ||
render() { | ||
return <div>My Component</div> | ||
} | ||
} |
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,28 @@ | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
}) | ||
} else { | ||
obj[key] = value | ||
} | ||
return obj | ||
} | ||
|
||
import React from 'react' | ||
export const MyComponent = | ||
/*#__PURE__*/ | ||
(function () { | ||
class MyComponent extends React.PureComponent { | ||
render() { | ||
return <div>My Component</div> | ||
} | ||
} | ||
|
||
_defineProperty(MyComponent, 'displayName', 'FancyName1') | ||
|
||
return MyComponent | ||
})() |
7 changes: 7 additions & 0 deletions
7
test/__fixtures__/wrap-const-arrow-function-components/code.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,7 @@ | ||
import React from 'react' | ||
|
||
export const FunctionComponent = () => { | ||
return <div>My Component</div> | ||
} | ||
FunctionComponent.displayName = 'FancyName1' | ||
FunctionComponent.defaultProps = {} |
15 changes: 15 additions & 0 deletions
15
test/__fixtures__/wrap-const-arrow-function-components/output.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,15 @@ | ||
import React from 'react' | ||
|
||
const FunctionComponent = | ||
/*#__PURE__*/ | ||
(function () { | ||
const FunctionComponent = () => { | ||
return <div>My Component</div> | ||
} | ||
|
||
FunctionComponent.displayName = 'FancyName1' | ||
FunctionComponent.defaultProps = {} | ||
return FunctionComponent | ||
})() | ||
|
||
export { FunctionComponent } |
8 changes: 8 additions & 0 deletions
8
test/__fixtures__/wrap-default-export-class-components/code.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,8 @@ | ||
import React from 'react' | ||
|
||
export default class MyComponent extends React.Component { | ||
static displayName = 'FancyName1' | ||
render() { | ||
return <div>My Component</div> | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
test/__fixtures__/wrap-default-export-class-components/output.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,31 @@ | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
}) | ||
} else { | ||
obj[key] = value | ||
} | ||
return obj | ||
} | ||
|
||
import React from 'react' | ||
|
||
const MyComponent = | ||
/*#__PURE__*/ | ||
(function () { | ||
class MyComponent extends React.Component { | ||
render() { | ||
return <div>My Component</div> | ||
} | ||
} | ||
|
||
_defineProperty(MyComponent, 'displayName', 'FancyName1') | ||
|
||
return MyComponent | ||
})() | ||
|
||
export default MyComponent |
7 changes: 7 additions & 0 deletions
7
test/__fixtures__/wrap-default-export-function-components/code.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,7 @@ | ||
import React from 'react' | ||
|
||
export default function FunctionComponent() { | ||
return <div>My Component</div> | ||
} | ||
FunctionComponent.displayName = 'FancyName1' | ||
FunctionComponent.defaultProps = {} |
Oops, something went wrong.