Skip to content

Instantly share code, notes, and snippets.

@micmro
Created November 20, 2014 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micmro/7225561444f6039e682b to your computer and use it in GitHub Desktop.
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
//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