-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build with rollup instead of webpack
Rollup will produce a smaller and more optimized bundle than webpack, and can be configured in a way that works perfectly for libraries, such as Aphrodite. This will help to minimize the bundle size impact of using this package, and may even give a small runtime speedup. For reference, React 16 is built using Rollup. Rollup does not allow Babel 5, so I also updated to Babel 6 at the same time. In this update, I tried to take care to maintain the same list of browser support that we have listed in the CSS prefixes that we build. At some point, we probably want to unify this configuration via browserslist. Issue: #239 I noticed that this Babel update caused branch coverage to drop a little, and I was unable to fix it by adding a test that definitely covered the missing branch. Thankfully, all I needed to do was add the istanbul Babel plugin to fix this. This is the approach recommended by the istanbul documentation: https://github.com/istanbuljs/nyc#use-with-babel-plugin-istanbul-for-babel-support Along with this update, I decided to add an ES modules build since it was easy enough. This will be used automatically by tools such as webpack 2+ to import the ES6 module version directly. I think it still makes sense to run these through Babel since most people don't run their node_modules through Babel, so the main difference here is that the import/export statements will not be compiled to require/module.exports. This allows webpack to perform optimizations such as tree-shaking and scope hoisting. One risk to be on the lookout for when people update to this version is that if you are using `require` to bring in Aphrodite with a version of webpack that is ES modules capable, it will break. Those consumers will need to switch to import instead. For this reason, I would be okay with removing the `module` field from the package.json for an initial release of the rollup build, and then we can add it later when the ecosystem has time to catch up. This is the approach we landed on for react-waypoint: 1. civiccc/react-waypoint#220 2. civiccc/react-waypoint#223 The filesize of the dist builds before this change looked like: ``` 83K aphrodite.js 84K aphrodite.umd.js 104K aphrodite.umd.js.map 23K aphrodite.umd.min.js 204K aphrodite.umd.min.js.map ``` And after this change: ``` 72K aphrodite.js 73K aphrodite.umd.js 108K aphrodite.umd.js.map 20K aphrodite.umd.min.js 93K aphrodite.umd.min.js.map ``` So it looks like the minified UMD build dropped from 24 KiB to 20 KiB.
- Loading branch information
Showing
10 changed files
with
199 additions
and
71 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"presets": [["airbnb", { | ||
"modules": false, | ||
"additionalTargets": { | ||
"chrome": "30", | ||
"android": "4", | ||
"firefox": "25", | ||
"ios": "6", | ||
"safari": "6", | ||
"ie": "9", | ||
"edge": "12", | ||
"opera": "13", | ||
}, | ||
}]], | ||
|
||
"env": { | ||
"test": { | ||
"presets": ["airbnb"], | ||
"plugins": ["istanbul"], | ||
}, | ||
}, | ||
} |
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 |
---|---|---|
|
@@ -4,4 +4,5 @@ | |
coverage | ||
dist/*.map | ||
lib | ||
es | ||
node_modules |
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,68 @@ | ||
import babel from 'rollup-plugin-babel'; | ||
import commonjs from 'rollup-plugin-commonjs'; | ||
import replace from 'rollup-plugin-replace'; | ||
import resolve from 'rollup-plugin-node-resolve'; | ||
import uglify from 'rollup-plugin-uglify'; | ||
|
||
import pkg from './package.json'; | ||
|
||
function distBuild(options) { | ||
options = options || {}; | ||
|
||
return { | ||
input: 'src/index.js', | ||
output: { | ||
file: `dist/${options.filename}`, | ||
format: options.format, | ||
name: 'aphrodite', | ||
sourcemap: options.sourcemap, | ||
}, | ||
plugins: [ | ||
babel({ | ||
exclude: ['node_modules/**'], | ||
}), | ||
replace({ | ||
'process.env.NODE_ENV': JSON.stringify('production'), | ||
}), | ||
resolve({ | ||
browser: true, | ||
}), // so rollup can find node modules | ||
commonjs(), // so rollup can convert node modules to ESM if needed | ||
options.minify && uglify(), | ||
] | ||
}; | ||
} | ||
|
||
const externals = new Set(Object.keys(pkg.dependencies)); | ||
|
||
function standardBuilds(filename) { | ||
return { | ||
input: `src/${filename}.js`, | ||
external: (id /*: string */) => { | ||
if (externals.has(id)) { | ||
return true; | ||
} | ||
|
||
// Mark deep imports from inline-style-prefixer as external. | ||
return /^inline-style-prefixer\//.test(id); | ||
}, | ||
output: [ | ||
{ file: `lib/${filename}.js`, format: 'cjs' }, | ||
{ file: `es/${filename}.js`, format: 'es' }, | ||
], | ||
plugins: [ | ||
babel({ | ||
exclude: ['node_modules/**'], | ||
}), | ||
commonjs(), // so rollup can convert node modules to ESM if needed | ||
] | ||
}; | ||
} | ||
|
||
export default [ | ||
distBuild({ filename: 'aphrodite.umd.js', format: 'umd', sourcemap: true, minify: false }), | ||
distBuild({ filename: 'aphrodite.umd.min.js', format: 'umd', sourcemap: true, minify: true }), | ||
distBuild({ filename: 'aphrodite.js', format: 'cjs', sourcemap: false, minify: false }), | ||
standardBuilds('index'), | ||
standardBuilds('no-important'), | ||
]; |
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
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.