-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use rollup to generate bundles for UMD, ESM and CJS (#63)
Co-authored-by: Jack Clark ### What does this PR do? Generates bundles for UMD, ESM and CommonJS. This brings with it support for browsers >= IE11. Whilst working on this we published a few version to `npm` with the `test` tag so that it could be tested. This [codepen](https://codepen.io/anon/pen/XGbdxR) has been created which uses the `UMD` build. We spun up a quick Create react app to test out `ESM` which worked great. The nextjs example app has also been updated locally to use the commonjs version. Our rollup config was heavily influenced by the one [in redux](https://github.com/reduxjs/redux/blob/master/rollup.config.js). Bundle size has slightly increased but we now have increased browser support and our module is now tree shakeable, meaning potentially reduced size for end users. *Important* We now use Promises to write this library as transpiling from async/await is usually avoided in libraries as it's expensive. We opted instead to use untranspiled Promises and then inform the user they should provide an environment that includes Promises. ### Related issues Resolves #11 ### Checklist - [x] I have checked the [contributing document](../blob/master/CONTRIBUTING.md) - [x] I have added or updated any relevant documentation - [x] I have added or updated any relevant tests
- Loading branch information
Showing
13 changed files
with
296 additions
and
87 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,7 @@ node_modules | |
graphql-hooks-*.*.*.tgz | ||
package-lock.json | ||
/coverage | ||
.DS_Store | ||
lib | ||
dist | ||
es |
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,45 @@ | ||
{ | ||
"lib/graphql-hooks.js": { | ||
"bundled": 11105, | ||
"minified": 5606, | ||
"gzipped": 1919 | ||
}, | ||
"es/graphql-hooks.js": { | ||
"bundled": 10760, | ||
"minified": 5314, | ||
"gzipped": 1844, | ||
"treeshaked": { | ||
"rollup": { | ||
"code": 67, | ||
"import_statements": 21 | ||
}, | ||
"webpack": { | ||
"code": 1069 | ||
} | ||
} | ||
}, | ||
"es/graphql-hooks.mjs": { | ||
"bundled": 3954, | ||
"minified": 3954, | ||
"gzipped": 1521, | ||
"treeshaked": { | ||
"rollup": { | ||
"code": 67, | ||
"import_statements": 21 | ||
}, | ||
"webpack": { | ||
"code": 1071 | ||
} | ||
} | ||
}, | ||
"dist/graphql-hooks.js": { | ||
"bundled": 11352, | ||
"minified": 5114, | ||
"gzipped": 1921 | ||
}, | ||
"dist/graphql-hooks.min.js": { | ||
"bundled": 5070, | ||
"minified": 5070, | ||
"gzipped": 1900 | ||
} | ||
} |
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,107 @@ | ||
import nodeResolve from 'rollup-plugin-node-resolve'; | ||
import babel from 'rollup-plugin-babel'; | ||
import replace from 'rollup-plugin-replace'; | ||
import { terser } from 'rollup-plugin-terser'; | ||
import { sizeSnapshot } from 'rollup-plugin-size-snapshot'; | ||
|
||
import pkg from './package.json'; | ||
|
||
const external = [...Object.keys(pkg.peerDependencies || {})]; | ||
|
||
export default [ | ||
// CommonJS | ||
{ | ||
input: 'src/index.js', | ||
output: { file: 'lib/graphql-hooks.js', format: 'cjs', indent: false }, | ||
external, | ||
plugins: [babel(), sizeSnapshot()] | ||
}, | ||
|
||
// ES | ||
{ | ||
input: 'src/index.js', | ||
output: { file: 'es/graphql-hooks.js', format: 'es', indent: false }, | ||
external, | ||
plugins: [babel(), sizeSnapshot()] | ||
}, | ||
|
||
// ES for Browsers | ||
{ | ||
input: 'src/index.js', | ||
output: { file: 'es/graphql-hooks.mjs', format: 'es', indent: false }, | ||
external, | ||
plugins: [ | ||
nodeResolve({ | ||
jsnext: true | ||
}), | ||
replace({ | ||
'process.env.NODE_ENV': JSON.stringify('production') | ||
}), | ||
terser({ | ||
compress: { | ||
pure_getters: true, | ||
unsafe: true, | ||
unsafe_comps: true, | ||
warnings: false | ||
} | ||
}), | ||
sizeSnapshot() | ||
] | ||
}, | ||
|
||
// UMD Development | ||
{ | ||
input: 'src/index.js', | ||
output: { | ||
file: 'dist/graphql-hooks.js', | ||
format: 'umd', | ||
name: 'GraphQLHooks', | ||
indent: false | ||
}, | ||
external, | ||
plugins: [ | ||
nodeResolve({ | ||
jsnext: true | ||
}), | ||
babel({ | ||
exclude: 'node_modules/**' | ||
}), | ||
replace({ | ||
'process.env.NODE_ENV': JSON.stringify('development') | ||
}), | ||
sizeSnapshot() | ||
] | ||
}, | ||
|
||
// UMD Production | ||
{ | ||
input: 'src/index.js', | ||
output: { | ||
file: 'dist/graphql-hooks.min.js', | ||
format: 'umd', | ||
name: 'GraphQLHooks', | ||
indent: false | ||
}, | ||
external, | ||
plugins: [ | ||
nodeResolve({ | ||
jsnext: true | ||
}), | ||
babel({ | ||
exclude: 'node_modules/**' | ||
}), | ||
replace({ | ||
'process.env.NODE_ENV': JSON.stringify('production') | ||
}), | ||
terser({ | ||
compress: { | ||
pure_getters: true, | ||
unsafe: true, | ||
unsafe_comps: true, | ||
warnings: false | ||
} | ||
}), | ||
sizeSnapshot() | ||
] | ||
} | ||
]; |
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 @@ | ||
const { NODE_ENV } = process.env; | ||
|
||
module.exports = { | ||
presets: [ | ||
[ | ||
'@babel/env', | ||
{ | ||
targets: { | ||
browsers: ['ie >= 11'] | ||
}, | ||
modules: false, | ||
loose: true | ||
} | ||
] | ||
], | ||
plugins: [ | ||
// don't use `loose` mode here - need to copy symbols when spreading | ||
'@babel/proposal-object-rest-spread', | ||
NODE_ENV === 'test' && '@babel/transform-modules-commonjs' | ||
].filter(Boolean) | ||
}; |
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,7 +1,7 @@ | ||
const React = require('react'); | ||
import React from 'react'; | ||
|
||
const ClientContext = React.createContext(); | ||
|
||
ClientContext.displayName = 'ClientContext'; | ||
|
||
module.exports = ClientContext; | ||
export default ClientContext; |
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.