UNPKG

7.53 kBMarkdownView Raw
1<p align="center">
2 <a href="https://stylus-lang.com/"><img src="https://stylus-lang.com/img/stylus-logo.svg" alt="Stylus" title="Stylus" height="80" valign="middle"></a>
3 <a href="https://webpack.js.org/"><img src="https://webpack.js.org/assets/icon-square-big.svg" alt="Webpack" title="Webpack" height="120" valign="middle"></a>
4</p>
5<p align="center">
6 <a href="https://www.npmjs.com/package/stylus-native-loader"><img src="https://img.shields.io/npm/v/stylus-native-loader.svg?sanitize=true" alt="Version"></a>
7 <a href="https://nodejs.org"><img src="https://img.shields.io/node/v/stylus-native-loader.svg" alt="Node.js Version"></a>
8 <a href="https://david-dm.org/slightlyfaulty/stylus-native-loader"><img src="https://david-dm.org/slightlyfaulty/stylus-native-loader.svg" alt="Dependencies"></a>
9 <a href="https://packagephobia.now.sh/result?p=stylus-native-loader"><img src="https://packagephobia.now.sh/badge?p=stylus-native-loader" alt="Size"></a>
10</p>
11
12# stylus-native-loader
13
14A super light-weight [Stylus](https://stylus-lang.com/) loader for [Webpack](https://webpack.js.org/) that leverages the built-in power of Stylus. Configured for modern development workflows.
15
16Unlike other Stylus loaders available, *stylus-native-loader* relies solely on the flexible "native" [@import/require](https://stylus-lang.com/docs/import.html) capabilities of Stylus, rather than hacking Webpack's async resolver to play nice with the synchronous Stylus compiler.
17
18The result is a highly configurable, lean Stylus loader with near-baseline build speeds and unhindered @import/require functionality (with support for Webpack aliases) 🥳
19
20## Benchmarks
21
22*Build times for the [vuejs.org Stylus source code](https://github.com/vuejs/vuejs.org/tree/master/themes/vue/source/css), sorted from fastest to slowest.*
23
24| | Min | Max | Average | Diff % |
25| ------------------------------------------------------------ | -------- | -------- | ------------ | ------- |
26| **[stylus](https://stylus-lang.com/docs/js.html)** (no Webpack) | 72.67ms | 104.75ms | **80.47ms** | |
27| **stylus-native-loader** | 79.61ms | 104.26ms | **86.41ms** | +7.38% |
28| **[stylus-loader](https://github.com/shama/stylus-loader)** | 85.62ms | 128.05ms | **104.39ms** | +29.73% |
29| **[stylus-relative-loader](https://github.com/walmartlabs/stylus-relative-loader)** | 117.32ms | 198.78ms | **143.10ms** | +77.83% |
30
31## Getting Started
32
33To begin, install `stylus-native-loader` and `stylus`:
34
35```sh
36yarn add -D stylus-native-loader stylus
37# or
38npm i -D stylus-native-loader stylus
39```
40
41Then add the loader to your **webpack.config.js**. For example, a minimal configuration might look like this:
42
43```js
44module.exports = {
45 module: {
46 rules: [
47 {
48 test: /\.styl$/i,
49 use: ['style-loader', 'css-loader', 'stylus-native-loader'],
50 },
51 ],
52 },
53}
54```
55
56## Configuration by Example
57
58Below is an example **webpack.config.js** using all stylus-native-loader options. None are required.
59
60```js
61const path = require('path')
62const MiniCssExtractPlugin = require('mini-css-extract-plugin')
63const nib = require('nib')
64
65module.exports = {
66 // Any "original source" option excluding "eval" enables source map generation
67 // @see https://webpack.js.org/configuration/devtool/
68 devtool: 'source-map',
69
70 resolve: {
71 // All aliases are used for Stylus @import and @require path resolution
72 alias: {
73 // Maps @import('~styl/*') to @import('/path/to/src/styl/*')
74 '~styl': path.join(__dirname, 'src/styl'),
75
76 // Maps @import('mixins') to @import('/path/to/src/styl/mixins')
77 'mixins$': path.join(__dirname, 'src/styl/mixins'),
78 },
79 },
80
81 module: {
82 rules: [
83 {
84 test: /\.styl$/,
85 use: [
86 {
87 // Extracts CSS to a separate file
88 loader: MiniCssExtractPlugin.loader
89 },
90 {
91 // Translates CSS into CommonJS
92 loader: 'css-loader',
93 options: {
94 // Required for Stylus source map output
95 sourceMap: true,
96 }
97 },
98 {
99 // Compiles Stylus to CSS
100 loader: 'stylus-native-loader',
101 options: {
102 /**
103 * Toggle/configure source map generation. This will be
104 * set automatically for you according to `devtool`.
105 *
106 * @see https://stylus-lang.com/docs/sourcemaps.html
107 *
108 * @type {boolean|Object}
109 * @default `devtool`|false
110 */
111 sourceMap: {
112 // Toggle loading of source map file contents
113 content: true,
114 // All other Stylus "sourcemap" options can be set if needed
115 },
116
117 /**
118 * Specify Stylus plugins to use.
119 *
120 * @type {Function|Function[]}
121 * @default []
122 */
123 use: nib(),
124
125 /**
126 * Add path(s) to the import lookup paths.
127 *
128 * @type {string|string[]}
129 * @default []
130 */
131 include: path.join(__dirname, 'src/styl/config'),
132
133 /**
134 * Import the specified Stylus files/paths.
135 *
136 * @type {string|string[]}
137 * @default []
138 */
139 import: [
140 'nib',
141 path.join(__dirname, 'src/styl/mixins'),
142 ],
143
144 /**
145 * Define Stylus variables or functions.
146 *
147 * @type {Object}
148 * @default {}
149 */
150 define: {
151 '$development': process.env.NODE_ENV === 'development'
152 },
153
154 /**
155 * Toggle built-in Stylus/Nib vendor prefixing.
156 * Disabled by default (prefer PostCSS Autoprefixer).
157 *
158 * @type {boolean}
159 * @default false
160 */
161 vendors: true,
162
163 /**
164 * Resolve relative url()'s inside imported files.
165 *
166 * @see https://stylus-lang.com/docs/js.html#stylusresolveroptions
167 *
168 * @type {boolean|Object}
169 * @default false
170 */
171 resolveUrl: true,
172
173 /**
174 * Include regular CSS on @import.
175 *
176 * @type {boolean}
177 * @default false
178 */
179 includeCSS: true,
180
181 /**
182 * Aliases used for @import and @require path resolution.
183 * If set, uses this instead of Webpack `resolve.alias` config.
184 *
185 * @type {Object|false}
186 * @default `resolve.alias`
187 */
188 alias: {
189 'mixins': path.join(__dirname, 'src/styl/mixins'),
190 },
191
192 /**
193 * Stylus has several other options/configurations that can be
194 * set here and will be passed directly to the Stylus compiler.
195 *
196 * @see https://stylus-lang.com/docs/js.html
197 * @see https://stylus-lang.com/docs/executable.html
198 */
199 },
200 },
201 ],
202 },
203 ],
204 },
205}
206```
207
208## License
209
210[MIT](./LICENSE)