grunt-extract-styles
Extract styles from css file based on decelerations matching.
Getting Started
This plugin requires Grunt ~0.4.2
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
npm install grunt-extract-styles --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt;
The "extractStyles" task
Overview
- In your project's Gruntfile, add a section named
extractStyles
to the data object passed intogrunt.initConfig()
. - In your
HTML
file add to style href url the suffix?__extractStyles=extracted-style-filename.css
.
Options
options.pattern
Type: RegExp
Mandatory parameter, the pattern that matchs the declaration for the extracted styles.
options.remove
Type: Boolean
Default value: true
Whether or not to remove the matching declarations from the original stylesheet.
options.preProcess
Type: function
Default: null
Pre-process function that apply on the matched by identifierPattern
source file content
options.postProcess
Type: function
Default: null
Post-process function that apply on the output content files (original & extracted)
options.remainSuffix
Type: string
Default: .remain
The filename suffix of the remaining content. (style.css
=> style.remain.css
)
options.extractedFileSuffix
Type: string
Default: '.extracted'
The filename suffix of the extracted content. (style.css
=> style.extracted.css
)
If the Link
tag doesn't contains a file name for the extracted content, the file name will be the original filename with that suffix.
options.extractedSuffix
Type: string
Default: ``
suffix for the extracted file link. (with extractedSuffix:"?inline=true"
extracted.css
=> extracted.css?inline=true
)
options.linkIdentifier
Type: string
Default: ?__extractStyles
Identifier of the links in the HTML to extract from. This string will convert to the following Regex
:
'<link.*href="(.*' + linkIdentifier + '(?:=([^"]+))?)".*>'
options.usemin
Type: boolean
Default: false
If true
the plugin will try to add the remain
file to the last css block.
Note: If there is no usemin css block you can add an empty css block.
<!-- build:css({.tmp}) main.css --> <!-- endbuild -->
Will extract the css declerations from style.css, save them to inline.css and style.remian.css
file will be added to main.css
concat & minified.
For example if your options are:
options: pattern: /\[\[[^\]]+\]\]/ files: dest: '.tmp/' src: '*.html'
And you apply it to the following:
{ }
This will be extracted:
{}
Usage Examples
Splitting Wix tpa params into their own stylesheet
Gruntfile:
grunt;
index.html
Demo
style.css
};}; //};} {};}; }}; }}{}; }}
Will generate in .tmp folder to following files:
.tmp/index.html
Demo
.tmp/style.remain.css
{}
.tmp/wix-styles.css
};}};}};} {}; }}; }}; }}; }}
Note: By default, the matching rules are removed from
style.css
(set byremove
property).
Credit
Uses the excellent PostCSS for the actual CSS post-processing.
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.