Skip to content

Commit 55fa199

Browse files
authored
build: switch to rollup (#74)
This should create properly optimized builds for umd, cjs and es. Previously the files were just run through Babel once to prepare them for shipping
1 parent 8e17cbb commit 55fa199

File tree

7 files changed

+442
-1094
lines changed

7 files changed

+442
-1094
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ node_modules
44
reports
55
example
66
lib/
7-
es/
7+
dist/
88
coverage/
99
nuget
1010
npm-debug.log*

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ cache: yarn
55
script:
66
- yarn lint
77
- yarn test --coverage
8-
- yarn flow
8+
- yarn flow check
99
- yarn build:storybook
1010
after_success:
1111
- yarn coveralls

package.json

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22
"name": "react-intersection-observer",
33
"version": "4.0.2",
44
"description": "Monitor if a component is inside the viewport, using IntersectionObserver API",
5-
"main": "lib/index.js",
5+
"main": "dist/react-intersection-observer.cjs.js",
6+
"jsnext:main": "dist/react-intersection-observer.esm.js",
7+
"module": "dist/react-intersection-observer.esm.js",
68
"author": "Daniel Schmidt",
79
"typings": "index.d.ts",
10+
"files": [
11+
"dist"
12+
],
813
"repository": {
914
"type": "git",
10-
"url": "https://github.com/thebuilder/react-intersection-observer"
15+
"url": "https://github.com/thebuilder/react-intersection-observer.git"
1116
},
1217
"license": "MIT",
1318
"keywords": [
@@ -19,12 +24,16 @@
1924
],
2025
"scripts": {
2126
"coveralls": "cat ./coverage/lcov.info | coveralls",
22-
"build": "rm -rf lib es && npm run build:lib && npm run build:flow",
23-
"build:lib": "babel src --out-dir lib --ignore __*,*.story.js,*.test.js",
27+
"build": "rm -rf dist && npm run build:lib && npm run build:flow",
28+
"build:lib": "run-p rollup:*",
2429
"build:storybook": "build-storybook --output-dir example",
25-
"build:flow": "flow-copy-source -v src lib",
30+
"build:flow": "node scripts/create-flow",
2631
"dev": "concurrently -k -r 'jest --watch' 'npm run storybook'",
2732
"lint": "eslint {src,stories,tests}/. ",
33+
"rollup:es": "rollup -c --environment FORMAT:es",
34+
"rollup:cjs": "rollup -c --environment FORMAT:cjs",
35+
"rollup:umd": "rollup -c --environment FORMAT:umd",
36+
"rollup:umd.min": "rollup -c --environment MINIFY,FORMAT:umd",
2837
"precommit": "flow && lint-staged",
2938
"postcommit": "git update-index --again",
3039
"prepare": "npm run build",
@@ -59,7 +68,7 @@
5968
"enzyme-to-json/serializer"
6069
],
6170
"setupFiles": [
62-
"<rootDir>/jest-setup.js"
71+
"<rootDir>/scripts/jest-setup.js"
6372
],
6473
"coveragePathIgnorePatterns": [
6574
"/node_modules/",
@@ -79,6 +88,7 @@
7988
"babel-cli": "^6.24.1",
8089
"babel-core": "^6.26.3",
8190
"babel-jest": "^22.4.3",
91+
"babel-plugin-external-helpers": "^6.22.0",
8292
"babel-preset-env": "^1.7.0",
8393
"babel-preset-react": "^6.24.1",
8494
"babel-preset-stage-2": "^6.24.1",
@@ -96,10 +106,16 @@
96106
"intersection-observer": "^0.5.0",
97107
"jest": "^22.4.3",
98108
"lint-staged": "^7.0.0",
109+
"npm-run-all": "^4.1.3",
99110
"prettier": "^1.12.1",
100111
"react": "^16.3.2",
101112
"react-dom": "^16.3.2",
102113
"react-test-renderer": "^16.3.2",
103-
"request": "~2.86.0"
114+
"rollup": "^0.59.1",
115+
"rollup-plugin-babel": "^3.0.3",
116+
"rollup-plugin-commonjs": "^9.1.0",
117+
"rollup-plugin-node-resolve": "^3.3.0",
118+
"rollup-plugin-replace": "^2.0.0",
119+
"rollup-plugin-uglify": "^4.0.0"
104120
}
105121
}

rollup.config.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import resolve from 'rollup-plugin-node-resolve'
2+
import babel from 'rollup-plugin-babel'
3+
import commonjs from 'rollup-plugin-commonjs'
4+
import replace from 'rollup-plugin-replace'
5+
import { uglify } from 'rollup-plugin-uglify'
6+
import pkg from './package.json'
7+
8+
const minify = process.env.MINIFY
9+
const format = process.env.FORMAT
10+
const es = format === 'es'
11+
const umd = format === 'umd'
12+
const cjs = format === 'cjs'
13+
14+
let output
15+
16+
if (es) {
17+
output = { file: pkg.module, format: 'es' }
18+
} else if (umd) {
19+
if (minify) {
20+
output = {
21+
file: `dist/react-intersection-observer.umd.min.js`,
22+
format: 'umd',
23+
}
24+
} else {
25+
output = { file: `dist/react-intersection-observer.umd.js`, format: 'umd' }
26+
}
27+
} else if (cjs) {
28+
output = { file: pkg.main, format: 'cjs' }
29+
} else if (format) {
30+
throw new Error(`invalid format specified: "${format}".`)
31+
} else {
32+
throw new Error('no format specified. --environment FORMAT:xxx')
33+
}
34+
35+
export default [
36+
{
37+
input: 'src/index.js',
38+
output: {
39+
name: 'reactIntersectionObserver',
40+
globals: {
41+
react: 'React',
42+
},
43+
...output,
44+
},
45+
external: umd
46+
? Object.keys(pkg.peerDependencies || {})
47+
: [
48+
...Object.keys(pkg.dependencies || {}),
49+
...Object.keys(pkg.peerDependencies || {}),
50+
],
51+
plugins: [
52+
resolve({
53+
jsnext: true,
54+
main: true,
55+
}),
56+
commonjs({ include: 'node_modules/**' }),
57+
babel({
58+
exclude: 'node_modules/**',
59+
babelrc: false,
60+
presets: [['env', { loose: true, modules: false }], 'react', 'stage-2'],
61+
plugins: ['external-helpers'],
62+
}),
63+
umd
64+
? replace({
65+
'process.env.NODE_ENV': JSON.stringify(
66+
minify ? 'production' : 'development',
67+
),
68+
})
69+
: null,
70+
minify ? uglify() : null,
71+
].filter(Boolean),
72+
},
73+
]

scripts/create-flow.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const pkg = require('../package')
4+
5+
/**
6+
* Create a flow file that references the original source file, so Flow mapping is kept intact.
7+
* https://blog.kentcdodds.com/distributing-flow-type-definitions-for-node-and-browser-modules-3952ad38b357
8+
*/
9+
function createFlow() {
10+
fs.writeFileSync(
11+
path.join(process.cwd(), pkg.main + '.flow'),
12+
createFlowFile(),
13+
'utf-8',
14+
)
15+
fs.writeFileSync(
16+
path.join(process.cwd(), pkg.module + '.flow'),
17+
createFlowFile(),
18+
'utf-8',
19+
)
20+
}
21+
22+
function createFlowFile(file = 'index.js') {
23+
return `// @flow
24+
export * from '../src/${file}'
25+
export { default } from '../src/${file}'
26+
`
27+
}
28+
29+
if (require.main === module) {
30+
createFlow()
31+
}
32+
33+
module.exports = createFlow
File renamed without changes.

0 commit comments

Comments
 (0)