If you have chosen the manual way, also install postcss and fast-glob.
Installation
npm
npm i -D rollup-plugin-postcss-amstramgram
Manually
Download the plugin in a rollup-plugin-postcss-amstramgram folder located at the root of your project.
Dependencies
npm i -D postcss fast-glob
Postcss plugins
Pick up postcss plugins you need from here or there and enjoy...
Usage
...
//import rollup-plugin-postcss-amstramgram from the folder where you downloaded it :
import cssPlugin from './rollup-plugin-postcss-amstramgram/esm/index.mjs'
//Or if you have installed it with npm :
//import cssPlugin from 'rollup-plugin-postcss-amstramgram'
//import postcss plugins you want to use :
//https://github.com/postcss/postcss-import
import postcssImport from 'postcss-import'
//https://github.com/csstools/postcss-plugins/tree/main/plugin-packs/postcss-preset-env
import postcssPresetEnv from 'postcss-preset-env'
//https://github.com/cssnano/cssnano
import cssnano from 'cssnano'
...
export default = {
input: 'src/js/index.js',
output: {
file: 'dist/js/index.js',
format: ...
},
plugins: [
...
cssPlugin({
jobs: {
//Select all files in the src/css directory
//including those located in subdirectories
//except those contained in the src/css/import folder
from: [`src/css/**/*`, `!src/css/import`],
to: `dist`,
//Processed files will be emitted recursively in the dist folder
//eg :
// - src/ccs/main.css => dist/css/main.css
// - src/ccs/components/component-01.css => dist/css/components/component-01.css
root: `src`
},
//Watch src/css directory and subdirectories when in development
watch: process.env.BUILD === 'development',
//Emit source maps when in development
sourcemap: process.env.BUILD === 'development',
plugins: [
postcssImport(),
postcssPresetEnv({
stage: 1
}),
//Minify when in production
...(process.env.BUILD === 'production' ? [cssnano()] : [])
]
}),
...
]
}
Options
Examples
Basic
As already mentioned, by default, all processed files are placed at the root of the destination folder(s) set by the to option of a job.
So, if you work with a tree-structure like this :
- css
- components
- component-01.css
- component-02.css
- import
- import-01.css
- import-02.css
- page-01.css
- page-02.css
- components
And if you set the plugin options like that :
...
export default = {
...
plugins: [
...
cssPlugin({
jobs: {
from: [`src/css/**/*`, `!src/css/import`],
to: `dist/css`
}
}),
...
]
}
You will get :
- css
- component-01.css
- component-02.css
- page-01.css
- page-02.css
Keep nested structure
If you need to keep the original nested structure of your files, you should consider taking advantage of the job's root property.
Something like below where we also use the rename feature to spice it up a bit :
...
export default = {
...
plugins: [
...
cssPlugin({
jobs: {
from: [`src/css/**/*`, `!src/css/import`],
to: `dist`,
root: `src`,
rename: (name, fullPath) => (fullPath.includes('components') ? 'my-' : 'the-') + name
}
}),
...
]
}
- css
- components
- my-component-01.css
- my-component-02.css
- the-page-01.css
- the-page-02.css
- components
SCSS
...
//https://github.com/csstools/postcss-sass
import sass from '@csstools/postcss-sass'
//https://github.com/postcss/postcss-scss
import scss from 'postcss-scss'
export default = {
...
plugins: [
...
cssPlugin({
jobs: {
from: `src/scss/main.scss`,
to: `dist/css`,
rename: 'main-from-scss'
},
parser: scss,
plugins: [sass()]
}),
...
]
}
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
:root {
/*Traditional css comment*/
//SCSS comment
color: $primary-color;
font: 100% $font-stack;
}
:root {
/*Traditional css comment*/
/*SCSS comment*/
color: #333;
font: 100% Helvetica, sans-serif;
}
Output plugin
The plugin can be set as an output plugin if you really need it.
However, even if the job is indeed done, you will be warned by rollup about using "transform" and "watchChange" hooks if you don't set an output generation hook for the processHook option.
...
export default = {
input: `src/js/main.js`,
output: {
file: `dist/js/main.js`,
format: 'esm',
plugins: [
cssPlugin({
jobs: { from: `src/css/main.css`, to: `dist/css`, rename: `main.min`},
processHook: 'writeBundle',
plugins: [cssnano()]
})
]
},
}