Partial Import is a PostCSS plugin that inlines @import
statements in CSS. It supports partial imports like Sass, automatically looks for stylesheets within npm and Bower packages, and can generate files if they don’t already exist.
/* before: file.css */
@import "foo/bar";
/* before: foo/_bar.css */
html {
background-color: #fafafa;
}
/* after */
html {
background-color: #fafafa;
}
Follow these steps to use Partial Import.
Add Partial Import to your build tool:
npm install postcss-partial-import --save-dev
require('postcss-partial-import')({ /* options */ }).process(YOUR_CSS);
Add PostCSS to your build tool:
npm install postcss --save-dev
Load Partial Import as a PostCSS plugin:
postcss([
require('postcss-partial-import')({ /* options */ })
]);
Add Gulp PostCSS to your build tool:
npm install gulp-postcss --save-dev
Enable Partial Import within your Gulpfile:
var postcss = require('gulp-postcss');
gulp.task('css', function () {
return gulp.src('./css/src/*.css').pipe(
postcss([
require('postcss-partial-import')({ /* options */ })
])
).pipe(
gulp.dest('./css')
);
});
Add Grunt PostCSS to your build tool:
npm install grunt-postcss --save-dev
Enable Partial Import within your Gruntfile:
grunt.loadNpmTasks('grunt-postcss');
grunt.initConfig({
postcss: {
options: {
processors: [
require('postcss-partial-import')({ /* options */ })
]
},
dist: {
src: 'css/*.css'
}
}
});
Type: String
Default: utf8
The character encoding of files being imported.
Type: String
Default: .css
The file extension appended to partials being imported.
Type: String
Default: _
The leading characters prepended to partials being imported.
Type: Boolean
Default: false
Whether partials should be generated if they do not already exist.
Type: Array
Default: []
A list of alternate directories to find partials in.
Type: Array
Default: []
A list of PostCSS plugins to run over individual partials.
Type: function
Default: null
To pass CSS @import files to a compiler (such as webpack), which would otherwise not know which CSS files to watch for browser reloading.
Example
// webpack.config.js
postcss: function(webpack) {
return [
require('postcss-partial-import')({
addDependencyTo: webpack
})
];
}