Skip to content

Commit

Permalink
feat: use rollup, add unpkg bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
ambar committed Dec 7, 2018
1 parent 00b5391 commit ec586bf
Show file tree
Hide file tree
Showing 7 changed files with 1,256 additions and 463 deletions.
3 changes: 0 additions & 3 deletions .babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ const create = env => [
module.exports = {
presets: create(),
env: {
es: {
presets: create({modules: false}),
},
test: {
presets: create({targets: {node: 'current'}}),
plugins: [
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/coverage
/es
/lib
/dist
/tmp

# Files
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ myFetch('/api', {
// {"name":"JoJo"}
```

Import from [`module`](https://caniuse.com/#search=modules) script (1.4K gzip size):

```html
<script type="module">
import createFetch, {query, headers} from 'https://unpkg.com/create-fetch'
const myFetch = createFetch(fetch, [
query(),
headers({'x-requested-with': 'fetch'}),
])
myFetch('/example.html', {query: {foo: 'bar'}})
</script>
```

## API

- [`defaults(options)`](#defaults)
Expand Down
28 changes: 28 additions & 0 deletions example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Demo</title>
</head>
<body>
<pre>
npx serve .
open http://localhost:5000/example
</pre>
<pre id="log"></pre>
<script type="module">
import createFetch, {query, headers} from './dist/index.js'

const myFetch = createFetch(fetch, [
query(),
headers({'x-requested-with': 'fetch'}),
])
myFetch('/', {query: {foo: 'bar'}}).then(r => {
console.info(r)
log.textContent = [...r.headers].join('\n')
})
</script>
</body>
</html>
20 changes: 12 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"sideEffects": false,
"main": "lib/index.js",
"module": "es/index.js",
"unpkg": "dist/index.js",
"files": [
"es",
"lib",
Expand All @@ -19,23 +20,26 @@
"middleware"
],
"scripts": {
"prebuild": "rm -rf lib es",
"build:cjs": "NODE_ENV=production BABEL_ENV=cjs babel src --out-dir lib",
"build:es": "NODE_ENV=production BABEL_ENV=es babel src --out-dir es",
"build": "npm run build:cjs && npm run build:es",
"dev": "rollup -c -w",
"build": "rollup -c",
"prebuild": "rm -rf lib es dist",
"test": "jest",
"test:coverage": "jest --coverage",
"test:watch": "jest --watch --notify",
"prepare": "npm run test && npm run build"
},
"devDependencies": {
"@babel/cli": "^7.0.0-beta.54",
"@babel/core": "^7.0.0-beta.54",
"@babel/plugin-proposal-pipeline-operator": "^7.0.0-beta.54",
"@babel/preset-env": "^7.0.0-beta.54",
"@babel/cli": "^7.2.0",
"@babel/core": "^7.2.0",
"@babel/plugin-proposal-pipeline-operator": "^7.2.0",
"@babel/preset-env": "^7.2.0",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^23.4.2",
"jest": "^23.5.0",
"rollup": "^0.67.4",
"rollup-plugin-babel": "^4.1.0-0",
"rollup-plugin-node-builtins": "^2.1.2",
"rollup-plugin-terser": "^3.0.0",
"standard-version": "^4.4.0"
}
}
42 changes: 42 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import babel from 'rollup-plugin-babel'
import builtins from 'rollup-plugin-node-builtins'
import {terser} from 'rollup-plugin-terser'
import pkg from './package.json'

export default [
{
input: 'src/index.js',
output: [
//
{file: pkg.main, format: 'cjs'},
{file: pkg.module, format: 'es'},
],
plugins: [babel()],
},
// ES build for unpkg CDN
{
input: 'src/index.js',
output: {file: pkg.unpkg, format: 'es', indent: false},
plugins: [
babel({
babelrc: false,
presets: [
[
'@babel/preset-env',
{
loose: true,
useBuiltIns: 'entry',
targets: {
// https://caniuse.com/#search=modules
browsers: ['Chrome 61', 'Firefox 60', 'Safari 10.1'],
},
},
],
],
}),
// shims `querystring`
builtins(),
terser(),
],
},
]
Loading

0 comments on commit ec586bf

Please sign in to comment.