katrotz/rollup-plugin-less-modules(新しいタブで開きます)
主要言語: JavaScript
The rollup plugin that compiles LESS files into CSS
- star 数
- 5
- watcher 数
- 1
- fork 数
- 3
- issue 数
- 1
- 最終更新
- 2018/12/31
主要言語: JavaScript
The rollup plugin that compiles LESS files into CSS
The rollup less modules plugin compiles the LESS files into CSS before importing them into ES modules. A use case would be an Angular application that defines the styles at the component level, or any other component based application that implements styles encapsulation.
Why is it awesome?
npm install --save-dev rollup-plugin-less-modules
After compilation the less styles are available along with the source:
import { rollup } from 'rollup';
import lessModules from 'rollup-plugin-less-modules';
rollup({
entry: 'index.js',
sourceMap: true,
plugins: [
lessModules()
]
})
@import "typography"
body {
margin: 0;
}
html {
font-size: 100%;
}
import style, { sourceMap } from './index.less';
/* The application handles the styles based on the needs */
falsetrue{
// Contains the cwd and the imported less files directories
paths: Array,
// The relative path from the repository root to the file
filename: String,
// The source map configuration for [LESS](http://lesscss.org/usage/#programmatic-usage)
sourceMap: {}
}
{css: '', map: {mappings: ''}} signature. The css contains the compiled css content and the map contains the source maps. An object with the same signature should be returned otherwise the post-processing result is ignored.nullimport { rollup } from 'rollup';
import lessModules from 'rollup-plugin-less-modules';
import postcss from 'postcss';
import autoprefixer from 'autoprefixer';
const options = {
sourceMap: {}
};
const processor = (code, id) => {
const postCssOptions = {
from: id,
to: id,
map: {
prev: code.map
}
};
return postcss([autoprefixer])
.process(code.css, postCssOptions)
.then(result => ({
css: result.css,
map: result.map
}));
};
return rollup({
entry: 'index.js',
plugins: [
lessModules({options, processor})
]
})
falsereturn rollup({
input: 'index.js',
output: {
file: 'dist/app.js'
},
plugins: [
lessModules({
// Does not output the styles to an external file
// output: false,
// Outputs the styles to a separate bundle file with the same name as the input file name of the bundle.
// output: true,
// Outputs the bundled styles to a custom path
// output: 'dist/app.css'
})
]
})
The plugin provides means to configure and use the source maps. The source maps are provided only when the plugin is run with the sourcemap option enabled.
The source maps can be accessed in the ES module. Eg.
import { sourceMap } from 'path-to-styles.less'
When the source maps are configured to be inlined, the content is embedded into the CSS content as a base64 string
By default the source map paths to the original files are relative to the package root path (eg. src/components/component/less/styles.less)
To make the paths look like /components/component/less/styles.less, following less configuration required
return rollup({
entry: 'index.js',
dest: 'src/bundles/app.js'
plugins: [
lessModules({
// Defaults to src/bundles/app.css
output: true,
// less options
options: {
sourceMap: {
sourceMapRootpath: `/`,
sourceMapBasepath: `src`
}
}
})
]
})
MIT