|
1 year ago | |
---|---|---|
.. | ||
lib | 1 year ago | |
LICENSE | 1 year ago | |
README.md | 1 year ago | |
index.js | 1 year ago | |
package.json | 1 year ago |
npm install stylus-loader stylus --save-dev
Important: in order to have ability use any stylus
package version,
it won't be installed automatically. So it's required to
add it to package.json
along with stylus-loader
.
The latest version supporting webpack 1 can be installed with:
npm install stylus-loader@webpack1 stylus --save-dev
var css = require('!raw!stylus!./file.styl'); // Just the CSS
var css = require('!css!stylus!./file.styl'); // CSS with processed url(...)s
See css-loader to see the effect of processed url(...)
s.
Or within the webpack config:
module: {
loaders: [{
test: /\.styl$/,
loader: 'css-loader!stylus-loader?paths=node_modules/bootstrap-stylus/stylus/'
}]
}
Then you can: var css = require('./file.styl');
.
Use in tandem with the style-loader to add the css rules to your document
:
module: {
loaders: [
{ test: /\.styl$/, loader: 'style-loader!css-loader!stylus-loader' }
]
}
and then require('./file.styl');
will compile and add the CSS to your page.
stylus-loader
can also take advantage of webpack's resolve options. With the default options it'll find files in web_modules
as well as node_modules
, make sure to prefix any lookup in node_modules with ~
. For example if you have a styles package lookup files in it like @import '~styles/my-styles
. It can also find stylus files without having the extension specified in the @import
and index files in folders if webpack is configured for stylus's file extension.
module: {
resolve: {
extensions: ['', '.js', '.styl']
}
}
will let you have an index.styl
file in your styles package and require('styles')
or @import '~styles'
it. It also lets you load a stylus file from a package installed in node_modules or if you add a modulesDirectories, like modulesDirectories: ['node_modules', 'web_modules', 'bower_components']
option you could load from a folder like bower_components. To load files from a relative path leave off the ~
and @import 'relative-styles/my-styles';
it.
Be careful though not to use the extensions configuration for two types of in one folder. If a folder has a index.js
and a index.styl
and you @import './that-folder'
, it'll end up importing a javascript file into your stylus.
You can also use stylus plugins by adding an extra stylus
section to your webpack.config.js
.
var stylus_plugin = require('stylus_plugin');
module: {
loaders: [
{ test: /\.styl$/, loader: 'style-loader!css-loader!stylus-loader' }
]
},
stylus: {
use: [stylus_plugin()]
}
Multiple configs can be used by giving other configs different names and referring to the with the config
query option.
var stylus_plugin = require('stylus_plugin');
module: {
loaders: [
{
test: /\.other\.styl$/,
loader: 'style-loader!css-loader!stylus-loader?config=stylusOther'
}
]
},
stylusOther: {
use: [stylus_plugin()]
}
Webpack 2 formalizes its options with a schema. Options can be provided to stylus-loader
in the options field to module.rules
or through LoaderOptionsPlugin or stylus-loader
's OptionsPlugin (a convenience wrapper around LoaderOptionsPlugin).
Config through module rules:
module: {
rules: [
{
test: /\.styl$/,
use: [
'style-loader',
'css-loader',
{
loader: 'stylus-loader',
options: {
use: [stylus_plugin()],
},
},
],
}
],
},
Config through LoaderOptionsPlugin:
module: {
rules: [
{
test: /\.styl$/,
loader: 'style-loader!css-loader!stylus-loader',
},
],
},
plugins: [
new webpack.LoaderOptionsPlugin({
test: /\.styl$/,
stylus: {
// You can have multiple stylus configs with other names and use them
// with `stylus-loader?config=otherConfig`.
default: {
use: [stylus_plugin()],
},
otherConfig: {
use: [other_plugin()],
},
},
}),
],
Config through stylus-loader
's OptionsPlugin (convenience wrapper for LoaderOptionsPlugin):
plugins: [
new stylusLoader.OptionsPlugin({
default: {
use: [stylus_plugin()],
},
}),
],
The easiest way of enabling nib
is to import it in the stylus options:
stylus: {
use: [require('nib')()],
import: ['~nib/lib/nib/index.styl']
}
where ~
resolves to node_modules/
stylus-loader
currently prefers resolving paths with stylus's resovling utilities and then falling back to webpack when it can't find files. Use the preferPathResolver
option with the value 'webpack'
to swap this. This has the benefit of using webpack's async resolving instead of stylus's sync resolving. If you have a lot of dependencies in your stylus files this'll let those dependencies be found in parallel.
stylus: {
preferPathResolver: 'webpack',
}
npm test
open http://localhost:8080/test/
In lieu of a formal styleguide, take care to maintain the existing coding style.
Copyright (c) 2018 Kyle Robinson Young
Licensed under the MIT license.