Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add jest-cli to dependencies I was getting a module resolution error after installing. Test command was throwing an error saying it couldn't find jest-cli so now its in deps. * Add stylus plugin * Add support for using stylus modules with the plugin * Add a validation whitelist to allow the "stylus" key in webpack config This is a special case but we may run in to other cases before upgrading to webpack v3. In webpack v1 many plugins and loaders used top-level configuration keys to handle custom configuration. webpack-validator does not like this, but it does provide the option to whitelist keys, which is exactly what I did here. I also added a (hopefully) helpful error message to clarify what went wrong in the case of an invalid webpack config. * Add example of stylus plugin usage to gatsby-plugin-stylus * Do not validate stylus options object shape Validating the shape felt like overkill, and it might prevent valid options form being used if I missed something. * Configure stylus to use module extension for overall gatsby consistency * Update readme to reflect css module configuration change * Add common-tags helper to improve long string formatting * Remove debug code
- Loading branch information
1 parent
5a37ebd
commit 0d3783c
Showing
6 changed files
with
200 additions
and
12 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
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 |
---|---|---|
@@ -1,3 +1,45 @@ | ||
# gatsby-plugin-stylus | ||
|
||
Stub README | ||
Provides drop-in support for Stylus with or without CSS Modules | ||
|
||
## Install | ||
|
||
`yarn add gatsby-plugin-stylus` | ||
|
||
## How to use | ||
|
||
1. Include the plugin in your `gatsby-config.js` file. | ||
2. Write your stylesheets in Stylus (`.styl` files) and require/import them | ||
|
||
### Without CSS Modules | ||
|
||
```javascript | ||
// in gatsby-config.js | ||
plugins: [ | ||
`gatsby-plugin-stylus` | ||
] | ||
``` | ||
|
||
### With CSS Modules | ||
|
||
Using CSS modules requires no additional configuration. Simply prepend `.module` to the extension. For example: `App.styl` -> `App.module.styl`. Any file with the `module` extension will use CSS modules. | ||
|
||
### With Stylus plugins | ||
|
||
This plugin has the same API as [stylus-loader](https://github.com/shama/stylus-loader#stylus-plugins), which means you can add stylus plugins with `use`: | ||
|
||
```javascript | ||
// in gatsby-config.js | ||
const rupture = require('rupture'); | ||
|
||
module.exports = { | ||
plugins: [ | ||
{ | ||
resolve: 'gatsby-plugin-stylus', | ||
options: { | ||
use: [rupture()], | ||
}, | ||
}, | ||
], | ||
}; | ||
``` |
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 |
---|---|---|
@@ -1,18 +1,25 @@ | ||
{ | ||
"name": "gatsby-plugin-stylus", | ||
"description": "Gatsby support for Stylus", | ||
"version": "1.0.1", | ||
"description": "Stub description for gatsby-plugin-stylus", | ||
"main": "index.js", | ||
"scripts": { | ||
"build": "babel src --out-dir . --ignore __tests__", | ||
"watch": "babel -w src --out-dir . --ignore __tests__" | ||
"author": "Ian Sinnott <ian@iansinnott.com>", | ||
"dependencies": { | ||
"babel-cli": "^6.24.1", | ||
"extract-text-webpack-plugin": "^1.0.1", | ||
"stylus": "^0.54.5", | ||
"stylus-loader": "webpack1" | ||
}, | ||
"devDependencies": {}, | ||
"keywords": [ | ||
"gatsby" | ||
"gatsby", | ||
"gatsby-plugin", | ||
"stylus" | ||
], | ||
"author": "Kyle Mathews <mathews.kyle@gmail.com>", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"babel-cli": "^6.24.1" | ||
"main": "./gatsby-node.js", | ||
"readme": "README.md", | ||
"scripts": { | ||
"build": "babel src --out-dir .", | ||
"watch": "babel -w src --out-dir ." | ||
} | ||
} |
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,120 @@ | ||
/** | ||
* Usage: | ||
* | ||
* // gatsby-config.js | ||
* plugins: [ | ||
* `gatsby-plugin-stylus`, | ||
* ], | ||
* | ||
* // Usage with options: | ||
* | ||
* // gatsby-config.js | ||
* plugins: [ | ||
* { | ||
* resolve: `gatsby-plugin-stylus`, | ||
* options: { | ||
* use: [], | ||
* }, | ||
* }, | ||
* ], | ||
*/ | ||
const ExtractTextPlugin = require(`extract-text-webpack-plugin`) | ||
|
||
exports.modifyWebpackConfig = ({ config, stage }, options = {}) => { | ||
const cssModulesConfProd = `css?modules&minimize&importLoaders=1` | ||
const cssModulesConfDev = | ||
`css?modules&importLoaders=1&localIdentName=[name]---[local]---[hash:base64:5]` | ||
|
||
// Pass in stylus plugins regardless of stage. | ||
if (Array.isArray(options.use)) { | ||
config.merge(current => { | ||
current.stylus = { | ||
use: options.use, | ||
} | ||
return current | ||
}) | ||
} else if (options.use) { | ||
throw new Error(`gatsby-plugin-stylus "use" option passed with ${options.use}. Pass an array of stylus plugins instead`) | ||
} | ||
|
||
const stylusFiles = /\.styl$/ | ||
const stylusModulesFiles = /\.module\.styl$/ | ||
|
||
switch (stage) { | ||
case `develop`: { | ||
config.loader(`stylus`, { | ||
test: stylusFiles, | ||
exclude: stylusModulesFiles, | ||
loaders: [`style`, `css`, `postcss`, `stylus`], | ||
}) | ||
config.loader(`stylusModules`, { | ||
test: stylusModulesFiles, | ||
loaders: [`style`, cssModulesConfDev, `postcss`, `stylus`], | ||
}) | ||
return config | ||
} | ||
|
||
case `build-css`: { | ||
config.loader(`stylus`, { | ||
test: stylusFiles, | ||
exclude: stylusModulesFiles, | ||
loader: ExtractTextPlugin.extract(`style`, [ | ||
`css?minimize`, | ||
`postcss`, | ||
`stylus`, | ||
]), | ||
}) | ||
config.loader(`stylusModules`, { | ||
test: stylusModulesFiles, | ||
loader: ExtractTextPlugin.extract(`style`, [ | ||
cssModulesConfProd, | ||
`postcss`, | ||
`stylus`, | ||
]), | ||
}) | ||
return config | ||
} | ||
|
||
case `develop-html`: | ||
case `build-html`: { | ||
const moduleLoader = ExtractTextPlugin.extract(`style`, [ | ||
cssModulesConfProd, | ||
`postcss`, | ||
`stylus`, | ||
]) | ||
|
||
config.loader(`stylus`, { | ||
test: stylusFiles, | ||
exclude: stylusModulesFiles, | ||
loader: `null`, | ||
}) | ||
config.loader(`stylusModules`, { | ||
test: stylusModulesFiles, | ||
loader: moduleLoader, | ||
}) | ||
return config | ||
} | ||
|
||
case `build-javascript`: { | ||
const moduleLoader = ExtractTextPlugin.extract(`style`, [ | ||
cssModulesConfProd, | ||
`stylus`, | ||
]) | ||
config.loader(`stylus`, { | ||
test: stylusFiles, | ||
exclude: stylusModulesFiles, | ||
loader: `null`, | ||
}) | ||
config.loader(`stylusModules`, { | ||
test: stylusModulesFiles, | ||
loader: moduleLoader, | ||
}) | ||
|
||
return config | ||
} | ||
|
||
default: { | ||
return config | ||
} | ||
} | ||
} |
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