rollup-plugin-sass
Installation
npm install rollup-plugin-sass -D
Usage
import sass from 'rollup-plugin-sass';
export default {
input: 'index.js',
output: {
file: 'bundle.js',
format: 'cjs',
},
plugins: [
sass()
]
}
rollup.config.ts
Add allowSyntheticDefaultImports
, or esModuleInterop
(enables allowSyntheticDefaultImports
), to tsconfig.json:
{
"compilerOptions": {
"esModuleInterOp": true
}
}
Reference: (https://www.typescriptlang.org/tsconfig#esModuleInterop)
Write rollup.config.ts:
import sass from 'rollup-plugin-sass';
Profit.
Options
output
- Type:
Boolean|String|Function
(default: false)
sass({
output: false,
output: true,
output: 'bundle.css',
output(styles, styleNodes) {
writeFileSync('bundle.css', styles);
}
})
insert
- Type:
Boolean
(default: false)
If you specify true
, the plugin will insert compiled CSS into <head/>
tag.
sass({
insert: true
})
processor
If you specify a function as processor which will be called with compiled css before generate phase.
import autoprefixer from 'autoprefixer';
import postcss from 'postcss';
sass({
processor: css => postcss([autoprefixer])
.process(css)
.then(result => result.css)
})
The processor
also support object result. Reverse css
filed for stylesheet, the rest properties can be customized.
sass({
processor(code) {
return {
css: '.body {}',
foo: 'foo',
bar: 'bar',
};
},
})
Otherwise, you could do:
import style, { foo, bar } from 'stylesheet';
runtime
- Type:
Object
(default: sass)
If you specify an object, it will be used instead of sass. You can use this to pass a different sass compiler (for example the node-sass
npm package).
options
Options for sass or your own runtime sass compiler.
If you specify data
, the plugin will treat as prepend sass string.
Since you can inject variables during sass compilation with node.
sass({
options: {
data: '$color: #000;'
}
})
License
MIT © BinRui.Guan