-
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
Add support for CSS modules #295
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,6 +117,7 @@ module.exports = (program, directory, stage, webpackPort = 1500, routes = []) => | |
}, | ||
__PREFIX_LINKS__: program.prefixLinks, | ||
}), | ||
new ExtractTextPlugin('styles.css'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't extract styles here and in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without this line the following snippet:
is producing an empty object. I don't fully understand the reason of it. Maybe there is a better alternative for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh hmmm... when building the HTML pages you mean? That does make sense. Probably what you need to do is just copy the modules css loader config from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did some testing: for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems reasonable. And since this is a new experimental change that doesn't conflict with anything (unless someone named their css file *.modules.css) we can ship something we're not 100% on and refine it after the fact. |
||
] | ||
case 'build-javascript': | ||
return [ | ||
|
@@ -132,6 +133,7 @@ module.exports = (program, directory, stage, webpackPort = 1500, routes = []) => | |
}), | ||
new webpack.optimize.DedupePlugin(), | ||
new webpack.optimize.UglifyJsPlugin(), | ||
new ExtractTextPlugin('styles.css'), | ||
] | ||
default: | ||
throw new Error(`The state requested ${stage} doesn't exist.`) | ||
|
@@ -262,20 +264,43 @@ module.exports = (program, directory, stage, webpackPort = 1500, routes = []) => | |
loader: 'file-loader', | ||
}) | ||
|
||
const cssModulesConf = 'css?modules&importLoaders=1' | ||
const cssModulesConfDev = | ||
`${cssModulesConf}&sourceMap&localIdentName=[name]---[local]---[hash:base64:5]` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly, by default class names are rewritten to hashes, but for development it is more convenient to have extra There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
switch (stage) { | ||
case 'develop': | ||
|
||
config.loader('css', { | ||
test: /\.css$/, | ||
exclude: /\.module\.css$/, | ||
loaders: ['style', 'css', 'postcss'], | ||
}) | ||
config.loader('less', { | ||
test: /\.less/, | ||
exclude: /\.module\.less$/, | ||
loaders: ['style', 'css', 'less'], | ||
}) | ||
config.loader('sass', { | ||
test: /\.(sass|scss)/, | ||
exclude: /\.module\.(sass|scss)$/, | ||
loaders: ['style', 'css', 'sass'], | ||
}) | ||
|
||
// Css modules | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Proper capitalization |
||
config.loader('cssModules', { | ||
test: /\.module\.css$/, | ||
loaders: ['style', cssModulesConfDev, 'postcss'], | ||
}) | ||
config.loader('lessModules', { | ||
test: /\.module\.less/, | ||
loaders: ['style', cssModulesConfDev, 'less'], | ||
}) | ||
config.loader('sassModules', { | ||
test: /\.module\.(sass|scss)/, | ||
loaders: ['style', cssModulesConfDev, 'sass'], | ||
}) | ||
|
||
config.merge({ | ||
postcss: [ | ||
require('postcss-import')(), | ||
|
@@ -289,16 +314,33 @@ module.exports = (program, directory, stage, webpackPort = 1500, routes = []) => | |
case 'build-css': | ||
config.loader('css', { | ||
test: /\.css$/, | ||
exclude: /\.module\.css$/, | ||
loader: ExtractTextPlugin.extract(['css', 'postcss']), | ||
}) | ||
config.loader('less', { | ||
test: /\.less/, | ||
exclude: /\.module\.less$/, | ||
loader: ExtractTextPlugin.extract(['css', 'less']), | ||
}) | ||
config.loader('sass', { | ||
test: /\.(sass|scss)/, | ||
exclude: /\.module\.(sass|scss)$/, | ||
loader: ExtractTextPlugin.extract(['css', 'sass']), | ||
}) | ||
|
||
// Css modules | ||
config.loader('cssModules', { | ||
test: /\.module\.css$/, | ||
loader: ExtractTextPlugin.extract('style', [cssModulesConf, 'postcss']), | ||
}) | ||
config.loader('lessModules', { | ||
test: /\.module\.less/, | ||
loader: ExtractTextPlugin.extract('style', [cssModulesConf, 'less']), | ||
}) | ||
config.loader('sassModules', { | ||
test: /\.module\.(sass|scss)/, | ||
loader: ExtractTextPlugin.extract('style', [cssModulesConf, 'sass']), | ||
}) | ||
config.merge({ | ||
postcss: [ | ||
require('postcss-import')(), | ||
|
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.
Should be "CSS Modules" here plus in the header.
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.
Perhaps add examples of two css files where one (main.css) wouldn't use CSS Modules and another (
my-awesome-component.module.css
) would be.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.
By example, you mean to add example to gatsby-starter-default (or other starter) or to readme?
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.
Well that would be great too (adding to the starter) :-) but I meant here to add a simple example like: