Skip to content

Commit e4cdd16

Browse files
TrySoundrenatorib
authored andcommitted
Make powerplug fully treeshakable (#104)
The latest a couple of bytes (rollup: 365, webpack: 1356) are just left imports. They can't be treeshaked by default because itself may have side effects. But we may be sure that everything else will not exist if powerplug will be used by unused library.
1 parent 833c3b9 commit e4cdd16

File tree

5 files changed

+71
-118
lines changed

5 files changed

+71
-118
lines changed

.size-snapshot.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
{
22
"dist/react-powerplug.umd.js": {
3-
"bundled": 22959,
4-
"minified": 9274,
5-
"gzipped": 2551
3+
"bundled": 22909,
4+
"minified": 9228,
5+
"gzipped": 2522
66
},
77
"dist/react-powerplug.cjs.js": {
8-
"bundled": 20067,
9-
"minified": 10303,
10-
"gzipped": 2422
8+
"bundled": 20017,
9+
"minified": 10236,
10+
"gzipped": 2402
1111
},
1212
"dist/react-powerplug.esm.js": {
13-
"bundled": 19405,
14-
"minified": 9743,
15-
"gzipped": 2291,
13+
"bundled": 19355,
14+
"minified": 9676,
15+
"gzipped": 2267,
1616
"treeshaked": {
17-
"rollup": 1025,
18-
"webpack": 1835
17+
"rollup": 365,
18+
"webpack": 1356
1919
}
2020
}
2121
}

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"dist",
1111
"src"
1212
],
13-
"sideEffects": false,
1413
"scripts": {
1514
"build:flow": "echo \"// @flow\n\nexport * from '../src'\" > dist/react-powerplug.cjs.js.flow",
1615
"build:code": "cross-env NODE_ENV=code rollup -c",
@@ -84,12 +83,12 @@
8483
"react-dom": "^16.2.0",
8584
"react-test-renderer": "^16.2.0",
8685
"rimraf": "^2.6.1",
87-
"rollup": "^0.57.1",
86+
"rollup": "^0.59.1",
8887
"rollup-plugin-babel": "^4.0.0-beta.4",
8988
"rollup-plugin-node-resolve": "^3.3.0",
9089
"rollup-plugin-replace": "^2.0.0",
9190
"rollup-plugin-size-snapshot": "^0.4.1",
92-
"rollup-plugin-uglify": "^3.0.0"
91+
"rollup-plugin-uglify": "^4.0.0"
9392
},
9493
"peerDependencies": {
9594
"react": "^0.14.0 || ^15.0.0-0 || ^16.0.0-0"

rollup.config.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import nodeResolve from 'rollup-plugin-node-resolve'
22
import babel from 'rollup-plugin-babel'
33
import replace from 'rollup-plugin-replace'
4-
import uglify from 'rollup-plugin-uglify'
4+
import { uglify } from 'rollup-plugin-uglify'
55
import { sizeSnapshot } from 'rollup-plugin-size-snapshot'
6-
7-
const pkg = require('./package.json')
6+
import pkg from './package.json'
87

98
const input = './src/index.js'
109

11-
const isExternal = id => !id.startsWith('.') && !id.startsWith('/')
10+
const external = id => !id.startsWith('.') && !id.startsWith('/')
11+
12+
const globals = { react: 'React' }
13+
14+
const name = 'ReactPowerPlug'
1215

1316
const getBabelOptions = ({ useESModules }) => ({
1417
exclude: '**/node_modules/**',
@@ -27,12 +30,10 @@ export default [
2730
output: {
2831
file: 'dist/react-powerplug.umd.js',
2932
format: 'umd',
30-
name: 'ReactPowerPlug',
31-
globals: {
32-
react: 'React',
33-
},
33+
name,
34+
globals,
3435
},
35-
external: ['react'],
36+
external: Object.keys(globals),
3637
plugins: [
3738
nodeResolve(),
3839
babel(getBabelOptions({ useESModules: true })),
@@ -46,12 +47,10 @@ export default [
4647
output: {
4748
file: 'dist/react-powerplug.min.js',
4849
format: 'umd',
49-
name: 'ReactPowerPlug',
50-
globals: {
51-
react: 'React',
52-
},
50+
name,
51+
globals,
5352
},
54-
external: ['react'],
53+
external: Object.keys(globals),
5554
plugins: [
5655
nodeResolve(),
5756
babel(getBabelOptions({ useESModules: true })),
@@ -70,14 +69,14 @@ export default [
7069
{
7170
input,
7271
output: { file: pkg.main, format: 'cjs' },
73-
external: isExternal,
72+
external,
7473
plugins: [babel(getBabelOptions({ useESModules: false })), sizeSnapshot()],
7574
},
7675

7776
{
7877
input,
7978
output: { file: pkg.module, format: 'es' },
80-
external: isExternal,
79+
external,
8180
plugins: [babel(getBabelOptions({ useESModules: true })), sizeSnapshot()],
8281
},
8382
]

src/components/State.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@ import renderProps from '../utils/renderProps'
33
import noop from '../utils/noop'
44

55
class State extends Component {
6-
static defaultProps = {
7-
initial: {},
8-
onChange: noop,
9-
}
10-
116
state = {
12-
...this.props.initial
7+
...this.props.initial,
138
}
149

1510
_setState = (updater, cb = noop) => {
1611
this.setState(updater, () => {
17-
this.props.onChange(this.state)
12+
if (this.props.onChange) {
13+
this.props.onChange(this.state)
14+
}
1815
cb()
1916
})
2017
}

0 commit comments

Comments
 (0)