Created
November 20, 2014 04:14
-
-
Save micmro/7225561444f6039e682b to your computer and use it in GitHub Desktop.
Grunt-task for a quick and dirty conversion from a CSS file to a JS file containing the CSS as a variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//transform CSS file to JS variable | |
grunt.registerTask("inlineCssToJs", function() { | |
var cssFile = "src/style.css"; | |
var cssFileDestination = "dist/style.js"; | |
var varName = "cssFileText"; | |
var cssContent = grunt.file.read(cssFile); | |
//clean CSS content | |
cssContent = cssContent.replace( /\/\*(?:(?!\*\/)[\s\S])*\*\//g, "").replace(/[\r\n\t]+/g, " ").replace(/[ ]{2,}/g, " ").replace(/\"/g,"\\\""); | |
//make JS Var | |
cssContent = "var " + varName + "=\"" + cssContent + "\";"; | |
grunt.log.writeln(cssFile + " transformed to " + cssFileDestination); | |
grunt.file.write(cssFileDestination, cssContent); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment