Last active
December 10, 2015 00:18
-
-
Save jpillora/4350122 to your computer and use it in GitHub Desktop.
Grunt task to convert a CSS file into an equivalent readable JS file
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
// Custom tasks | |
grunt.registerMultiTask('css2js', 'Convert CSS to JS.', function() { | |
var name = this.target, | |
src = grunt.file.expandFiles( this.data.file ), | |
dest = src + '.js'; | |
grunt.log.writeln("Converting: '" + src + "' to JavaScript"); | |
var css = fs.readFileSync(src).toString(); | |
if(!css) { | |
grunt.log.writeln("Failed to read file"); | |
return false; | |
} | |
var cssStrings = css.split("\n").map(function(l) { return '"' + l + '\\n"'; }).join(" + \n"); | |
var js = '$(function() { $("head").append($("<style/>").html(' + cssStrings + ')); });'; | |
grunt.log.writeln("Saved: '" + src + "' as '" + dest + "'"); | |
fs.writeFileSync(dest, js); | |
return true; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment