A Sass-focused lightning talk version of this blog post: http://bensmithett.com/smarter-css-builds-with-webpack/
Here's the code used in the demo: https://github.com/bensmithett/webpack-css-example
Presented at MelbCSS, April 1 2015.
SMARTER CSS BUILDS WITH WEBPACKSuper fast lightning talk edition!@bensmithettSass
View Slide
Nobody writes plain old CSS in one big file any more
Use Sass!Modularize all the things!
config/colors.scssmedia_queries.scssmixins/breakpoint.scssmodules/btn.scssdropdown.scssheader.scss
How do you turn that into a singlebundle.cssto send down to users?
Sass @import ?
@import "config/colors";@import "config/media_queries";@import "mixins/breakpoint";@import "modules/btn";@import "modules/dropdown";@import "modules/header";
Shared global Sass scope
$red: #f00@import "config/colors";@import "config/media_queries";@import "mixins/breakpoint";@import "modules/btn";@import "modules/dropdown";@import "modules/header";
background: $red@import "config/colors";@import "config/media_queries";@import "mixins/breakpoint";@import "modules/btn";@import "modules/dropdown";@import "modules/header";$red: #f00
Does that remind you of anything?
defines $
defines $uses $
window.$window.Backbonewindow._window.MyApp$.fn.outrageousSliderPlugin
We don’t do that in JS any more.Instead we write small, self-contained modules that import theirdependencies into their own local scope.
components/dropdown.jsheader.jscss/btn.scssdropdown.scssheader.scss
var React = require(“react”);var Dropdown = require(“./dropdown”);module.exports = React.createClass({render () {return ();}});components/dropdown.jsheader.jscss/btn.scssdropdown.scssheader.scss
We can do that with Sass too, if…- we compile each file in isolation - we explicitly import each file’s dependencies
@import "modules/btn";@import "modules/dropdown";@import "modules/header";
// modules/header.scss@import "config/colors";.header {background: $red;}
Every Sass file compiled in its own private scope!Impossible to clobber variables across filesDoesn’t depend on a specific, fragile order
components/dropdown.jsheader.jscss/btn.scssdropdown.scssheader.scssvar React = require(“react”);var Dropdown = require(“./dropdown”);module.exports = React.createClass({render () {return ();}});
One of this component’s dependencies is in your head, not explicit in code!
UI components have non-JS dependenciesCSS, fonts, images, etc…
Webpack supercharges require() to let you require all of acomponent’s dependencies
components/dropdown.jsheader.jscss/btn.scssdropdown.scssheader.scssvar React = require(“react”);var Dropdown = require(“./dropdown”);require (“css/header”);module.exports = React.createClass({render () {return ();}});
Automatically build a single bundle.css based on explicitly declared dependencies in components your app actually uses$ webpack
STOP! DEMOTIME!(I just showed https://github.com/bensmithett/webpack-css-example and what happens to the output CSS when you add or remove differentcomponents)
THANKS!• http://bensmithett.com/smarter-css-builds-with-webpack/• https://github.com/bensmithett/webpack-css-example• http://webpack.github.io/@bensmithett