1 | # Rollup multiple .scss, .sass and .css imports
|
2 |
|
3 | <a href="LICENSE">
|
4 | <img src="https://img.shields.io/badge/license-MIT-brightgreen.svg" alt="Software License" />
|
5 | </a>
|
6 | <a href="https://github.com/thgh/rollup-plugin-scss/issues">
|
7 | <img src="https://img.shields.io/github/issues/thgh/rollup-plugin-scss.svg" alt="Issues" />
|
8 | </a>
|
9 | <a href="http://standardjs.com/">
|
10 | <img src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg" alt="JavaScript Style Guide" />
|
11 | </a>
|
12 | <a href="https://npmjs.org/package/rollup-plugin-scss">
|
13 | <img src="https://img.shields.io/npm/v/rollup-plugin-scss.svg?style=flat-squar" alt="NPM" />
|
14 | </a>
|
15 | <a href="https://github.com/thgh/rollup-plugin-scss/releases">
|
16 | <img src="https://img.shields.io/github/release/thgh/rollup-plugin-scss.svg" alt="Latest Version" />
|
17 | </a>
|
18 |
|
19 | ## Installation
|
20 |
|
21 | ```
|
22 | # v4 uses the rollup asset system
|
23 | npm install --save-dev rollup-plugin-scss@3 sass
|
24 |
|
25 | # v3 needs sass installed seperately (or node-sass)
|
26 | npm install --save-dev rollup-plugin-scss@3 sass
|
27 | ```
|
28 |
|
29 | If any of them is installed, it will be used automatically, if both installed `sass` will be used.
|
30 |
|
31 | ## Usage
|
32 |
|
33 | ```js
|
34 | // rollup.config.js
|
35 | import scss from 'rollup-plugin-scss'
|
36 |
|
37 | export default {
|
38 | input: 'input.js',
|
39 | output: {
|
40 | file: 'output.js',
|
41 | format: 'esm',
|
42 | // Removes the hash from the asset filename
|
43 | assetFileNames: '[name][extname]'
|
44 | },
|
45 | plugins: [
|
46 | scss() // will output compiled styles to output.css
|
47 | ]
|
48 | }
|
49 |
|
50 | // OR
|
51 |
|
52 | export default {
|
53 | input: 'input.js',
|
54 | output: { file: 'output.js', format: 'esm' },
|
55 | plugins: [
|
56 | scss({ fileName: 'bundle.css' }) // will output compiled styles to "bundle.css"
|
57 | ]
|
58 | }
|
59 |
|
60 | // OR
|
61 |
|
62 | export default {
|
63 | input: 'input.js',
|
64 | output: { file: 'output.js', format: 'esm' },
|
65 | plugins: [
|
66 | scss() // will output compiled styles to "assets/output-123hash.css"
|
67 | ]
|
68 | }
|
69 | ```
|
70 |
|
71 | ```js
|
72 | // entry.js
|
73 | import './reset.scss'
|
74 | ```
|
75 |
|
76 | ### Options
|
77 |
|
78 | Options are passed to the sass compiler ([node-sass] by default). Refer to [ the Sass docs](https://sass-lang.com/documentation/js-api#options) for more details on these options. <br/>
|
79 | One notable option is `indentedSyntax` which you'll need if you're parsing Sass syntax instead of Scss syntax. (e.g. when extracting a Vue `<style lang="sass">` tag) <br/>
|
80 | By default the plugin will base the filename for the css on the bundle destination.
|
81 |
|
82 | ```js
|
83 | scss({
|
84 | // Defaults to output.css, Rollup may add a hash to this!
|
85 | name: 'output.css',
|
86 |
|
87 | // Literal asset filename, bypasses the automated filenaming transformations
|
88 | fileName: 'output.css',
|
89 |
|
90 | // Callback that will be called ongenerate with two arguments:
|
91 | // - styles: the contents of all style tags combined: 'body { color: green }'
|
92 | // - styleNodes: an array of style objects: { filename: 'body { ... }' }
|
93 | output: function (styles, styleNodes) {
|
94 | writeFileSync('bundle.css', styles)
|
95 | },
|
96 |
|
97 | // Disable any style output or callbacks, import as string
|
98 | output: false,
|
99 |
|
100 | // Enables/disables generation of source map (default: false)
|
101 | sourceMap: true,
|
102 |
|
103 | // Choose files to include in processing (default: ['/**/*.css', '/**/*.scss', '/**/*.sass'])
|
104 | include: [],
|
105 |
|
106 | // Choose files to exclude from processing (default: undefined)
|
107 | exclude: [],
|
108 |
|
109 | // Determine if node process should be terminated on error (default: false)
|
110 | failOnError: true,
|
111 |
|
112 | // Prefix global scss. Useful for variables and mixins.
|
113 | prefix: `@import "./fonts.scss";`,
|
114 |
|
115 | // A Sass (sass compatible) compiler to use
|
116 | // - sass and node-sass packages are picked up automatically
|
117 | // - you can use this option to specify custom package (e.g. a fork of one of them)
|
118 | sass: require('node-sass'),
|
119 |
|
120 | // Run postcss processor before output
|
121 | processor: () => postcss([autoprefixer({ overrideBrowserslist: 'Edge 18' })]),
|
122 |
|
123 | // Process resulting CSS
|
124 | processor: (css, map) => ({
|
125 | css: css.replace('/*date*/', '/* ' + new Date().toJSON() + ' */'),
|
126 | map
|
127 | }),
|
128 |
|
129 | // or, just string (for backward compatiblity with v2 or simplicity)
|
130 | processor: css =>
|
131 | css.replace('/*date*/', '/* ' + new Date().toJSON() + ' */'),
|
132 |
|
133 | // Log filename and size of generated CSS files (default: true)
|
134 | verbose: true
|
135 |
|
136 | // Add file/folder to be monitored in watch mode so that changes to these files will trigger rebuilds.
|
137 | // Do not choose a directory where rollup output or dest is pointed to as this will cause an infinite loop
|
138 | watch: 'src/styles/components',
|
139 | watch: ['src/styles/components', 'src/multiple/folders']
|
140 |
|
141 | // Any other options are passed to the sass compiler
|
142 | includePaths: ...
|
143 | })
|
144 | ```
|
145 |
|
146 | ## Examples
|
147 |
|
148 | Using postcss + autoprefixer + includePaths (sass option)
|
149 |
|
150 | ```js
|
151 | import scss from 'rollup-plugin-scss'
|
152 | import postcss from 'postcss'
|
153 | import autoprefixer from 'autoprefixer'
|
154 |
|
155 | export default {
|
156 | input: 'input.js',
|
157 | output: {
|
158 | file: 'output.js',
|
159 | format: 'esm'
|
160 | },
|
161 | plugins: [
|
162 | scss({
|
163 | processor: () => postcss([autoprefixer()]),
|
164 | includePaths: [
|
165 | path.join(__dirname, '../../node_modules/'),
|
166 | 'node_modules/'
|
167 | ]
|
168 | })
|
169 | ]
|
170 | }
|
171 | ```
|
172 |
|
173 | Minify CSS output:
|
174 |
|
175 | ```js
|
176 | scss({
|
177 | outputStyle: 'compressed'
|
178 | })
|
179 | ```
|
180 |
|
181 | ## Changelog
|
182 |
|
183 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
|
184 |
|
185 | ## Contributing
|
186 |
|
187 | Contributions and feedback are very welcome. New features should include a test.
|
188 |
|
189 | To get it running:
|
190 |
|
191 | 1. Clone the project.
|
192 | 2. `npm install`
|
193 |
|
194 | ## Credits
|
195 |
|
196 | - [Thomas Ghysels](https://github.com/thgh)
|
197 | - [All Contributors][link-contributors]
|
198 |
|
199 | ## License
|
200 |
|
201 | The MIT License (MIT). Please see [License File](LICENSE) for more information.
|
202 |
|
203 | [link-author]: https://github.com/thgh
|
204 | [link-contributors]: ../../contributors
|
205 | [rollup-plugin-vue]: https://www.npmjs.com/package/rollup-plugin-vue
|
206 | [rollup-plugin-buble]: https://www.npmjs.com/package/rollup-plugin-buble
|
207 | [rollup-plugin-babel]: https://www.npmjs.com/package/rollup-plugin-babel
|
208 | [node-sass]: https://www.npmjs.com/package/node-sass
|
209 | [sass]: https://www.npmjs.com/package/sass
|