-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Add support for CSS modules #295
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -122,6 +122,7 @@ module.exports = (program, directory, suppliedStage, webpackPort = 1500, routes | |
| }, | ||
| __PREFIX_LINKS__: program.prefixLinks, | ||
| }), | ||
| new ExtractTextPlugin('styles.css'), | ||
| ] | ||
| case 'build-javascript': | ||
| return [ | ||
|
|
@@ -269,20 +270,43 @@ module.exports = (program, directory, suppliedStage, webpackPort = 1500, routes | |
| loader: 'file-loader', | ||
| }) | ||
|
|
||
| const cssModulesConf = 'css?modules&importLoaders=1' | ||
| const cssModulesConfDev = | ||
| `${cssModulesConf}&sourceMap&localIdentName=[name]---[local]---[hash:base64:5]` | ||
|
Contributor
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.
Contributor
Author
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
Contributor
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 | ||
| 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')(), | ||
|
|
@@ -296,16 +320,33 @@ module.exports = (program, directory, suppliedStage, 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')(), | ||
|
|
@@ -322,19 +363,37 @@ module.exports = (program, directory, suppliedStage, webpackPort = 1500, routes | |
| case 'build-html': | ||
| // We don't deal with CSS at all when building the HTML. | ||
| // The 'null' loader is used to prevent 'module not found' errors. | ||
| // On the other hand CSS modules loaders are necessary | ||
|
|
||
| config.loader('css', { | ||
| test: /\.css$/, | ||
| exclude: /\.module\.css$/, | ||
| loader: 'null', | ||
| }) | ||
| config.loader('less', { | ||
| test: /\.less/, | ||
| exclude: /\.module\.less$/, | ||
| loader: 'null', | ||
| }) | ||
| config.loader('sass', { | ||
| test: /\.(sass|scss)/, | ||
| exclude: /\.module\.(sass|scss)$/, | ||
| loader: 'null', | ||
| }) | ||
|
|
||
| // 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']), | ||
| }) | ||
| return config | ||
|
|
||
| case 'build-javascript': | ||
|
|
||
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.
We don't extract styles here and in
build-javascripton purpose as we already do that in thebuild-cssstage. Doing it again would be wasted effort.Uh oh!
There was an error while loading. Please reload this page.
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.
Without this line the following snippet:
import styles from './style.css'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 comment
The 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
developtobuild-htmlas right now inbuild-htmlall css loaders are turned off. https://github.com/jakubrohleder/gatsby/blob/d6e5388ea0b5581e4422afd02110808716d13e02/lib/utils/webpack.config.js#L357-L373There 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.
I did some testing: for
build-htmlstep css-modules loaders has to be turned on (or just not turned off), they useExtractTextPluginso it also has to be included in the build step configuration. On the other hand they are not needed inbuild-javascriptstep. I don't know why previously it seemed to me that this is necessary.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.
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.