-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable extension of webpack config by consumers #87
Enable extension of webpack config by consumers #87
Conversation
Using the new config extensions, it is possible to use css-modules and postcss. The following module.exports = function(config) {
// Remove the default css loader
config.removeLoader('css');
// Add our postcss/css-modules loader
config.loader('css', function(cfg) {
cfg.test = /\.css$/;
cfg.loader = 'style!css?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!postcss';
return cfg
})
// merge some postcss config into the cached webpack config
config.merge({
postcss: [
require('postcss-cssnext')({
browsers: 'last 2 versions'
}),
require('postcss-import')(),
require('postcss-nested'),
require('lost')
]
});
return config;
} |
👍 |
Add webpack-configurator which enables passing an object to a gatsby.config.js. This object has a few convenience functions for managing loaders as well as plugins (and arbitrary keys) relating to webpack configuration. * Move webpack config to JS * Allows consumers to modify loaders and plugins based on stage. Here is a webpack config which uses the new extensibility to add css-modules, classnames and postcss support to a static site. ```javascript var ExtractTextPlugin = require("extract-text-webpack-plugin"); function configureDevelopment(config) { config.removeLoader('css'); config.loader('css', function(cfg) { cfg.test = /\.css$/; cfg.loader = 'style!css?modules!postcss' return cfg }) config.merge({ postcss: [ require('postcss-import')(), require('postcss-cssnext')({ browsers: 'last 2 versions' }) ] }) } function configureForJSBundle(config) { config.removeLoader('css'); config.loader('css', function(cfg) { cfg.test = /\.css$/; cfg.loader = 'css/locals?modules!postcss' return cfg }) } function configureForHTMLGeneration(config) { config.removeLoader('css'); config.loader('css', function(cfg) { cfg.test = /\.css$/; cfg.loader = ExtractTextPlugin.extract('style', 'css?modules&minimize!postcss'); return cfg }) config.plugin('extract-css', ExtractTextPlugin, ["styles.css"]); config.merge({ postcss: [ require('postcss-import')(), require('postcss-cssnext')({ browsers: 'last 2 versions' }) ] }) } module.exports = function(config, env) { switch(env) { case 'production': configureForJSBundle(config); break; case 'static': configureForHTMLGeneration(config); break; default: configureDevelopment(config); break; } return config; } ``` Given `pages/_template.jsx` which imports and uses the following `pages/_template.css`: ```css :root { --black: black; } .header { background: var(--black); } ``` When running `gatsby build`, All css is extracted and turned into a minified `styles.css`: ```css ._2D1e_Piibk-xdIwKzGpKuS{background:#000} ``` The bundle.js and static html files have a div that looks like: ```html <div class="_2D1e_Piibk-xdIwKzGpKuS"> ... </div> ``` When running `gatsby develop`, the styles are included in the <head>, unminified: ```html <style type="text/css">._2D1e_Piibk-xdIwKzGpKuS { background: black; }</style> ```
e5f474c
to
652cf60
Compare
@KyleAMathews This is rebased on 0.7 and is in working condition/ready for a look. |
and replaces it with a loader that uses css-modules. | ||
|
||
```javascript | ||
modules.exports = function(config, env) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll probably want to introduce this by explaining we're using webpack-configurator so they can go there to learn more about what options are available..
Woah webpack-configurator is cool! I could only think of complicated & fragile ways for people to extend our base webpack config but that makes it pretty straightforward. 💯 One question from looking at your css setup—what are thoughts on making that the default for Gatsby + it's three starters? It seems PostCSS/CSS Modules has a lot of momentum and are full of good ideas. I like the idea of Gatsby having really solid defaults out of the box (that are also not overly complicated for a React/Webpack noob to pickup). Thoughts? |
@KyleAMathews As far as PostCSS and CSS Modules, I think those are great defaults and can make it easier to handle things like cssnext is the "pack" which I'd suggest including with gatsby. Compared to the example above, I'd suggest something more like this in dev: postcss: [
require('postcss-import'),
require('postcss-url'),
require('postcss-cssnext'),
require('postcss-browser-reporter')
] and this in prod postcss: [
require('postcss-import'),
require('postcss-url'),
require('postcss-cssnext'),
require('cssnano')
] Going a little deeper, one of the really nice things about postcss is plugins such as postcss-colorblind. Given a switch to postcss, it might be nice to consider either CLI extensions, flag passthrough to gatsby.config.js, or bundling in core so that something like and since we're talking about build stuffs, I think using image-loader instead of |
This sounds great! As I said, I'm behind on css so I'd really appreciate you adding your expertise here. Perhaps add the css setup in a follow-up PR? I could get rid of this then 😛 https://github.com/gatsbyjs/gatsby-starter-documentation/blob/master/html.jsx#L31-L68 Passing options like in your colorblind example is interesting. Perhaps give users some way of defining options on a per-site basis and then responding to them in their And 💯 on image-loader, that would fix #18 :-D |
sgtm, I'll put the css stuff and image-loader in follow-up PRs. |
Add react-hot back to coffeescript loader
f1b3f1f
to
c1b1ba9
Compare
@KyleAMathews I think this is good to go. lmk if you feel there's anything else I should do here. |
👍 |
👍 @KyleAMathews Graphene website will be powered soon by Gatsby! ;) |
@syrusakbary cool! Make sure to add a PR to get yourself listed to the site showcase in the README. |
Enable extension of webpack config by consumers
Merged! Thanks, this is a huge improvement! |
Nice! Thanks for the review! |
Just released 0.7.1 |
Under the scheme in this PR,
gatsby.config.js
would have to export a function which accepts and returns a config object. The config object is a webpack-configurator config object, which accepts merging an existing webpack config as well as incrementally adding loaders and plugins.Here is a
gatsby.config.js
which uses the new extensibility to addcss-modules and postcss support to a static site.
Given
pages/_template.jsx
which imports and uses the followingpages/_template.css
:When running
gatsby build
, All css is extracted and turned into aminified
styles.css
:The bundle.js and static html files have a div that looks like:
When running
gatsby develop
, the styles are included in the ,unminified:
Testing this PR
npm install -g
git clone git@github.com:ChristopherBiscardi/gatsby-default-template-extensible.git && cd gatsby-default-template-extensible
npm i && gatsby develop
TODO
gatsby.config.js
doesn't exist.react-hot
as a loader before, add that back.