Skip to content

Instantly share code, notes, and snippets.

@robclancy
Last active January 3, 2016 22:19
Show Gist options
  • Save robclancy/8527250 to your computer and use it in GitHub Desktop.
Save robclancy/8527250 to your computer and use it in GitHub Desktop.
grunt.registerMultiTask('dragon', 'Dragon Rawwr', function () {
var options = this.options();
var commentWrap = ['/*!', '!*/'];
var stringWrap = ['"!!', '!!"'];
var mediaPrefix = '@media(max-width:1337)//MEDIA';
var preCompile = function(contents) {
/* Replace...
@property "html";
color: red;
@property "/html";
*/
contents = contents.replace(/@property(["a-z0-9 \/]*);/ig, commentWrap[0]+'property$1;'+commentWrap[1]);
// Replace <xen:xmlsyntax />
contents = contents.replace(/((<xen:|<\/xen:)[a-z0-9@_ \-\.="'\/]*>)/gi, commentWrap[0]+'$1'+commentWrap[1]);
// Replace {xen: methods, arg, arg2}
contents = contents.replace(/(\{xen:[a-z0-9_ \-\.="'@+,*\/]*})/gi, commentWrap[0]+'$1'+commentWrap[1]+stringWrap[0]+stringWrap[1]);
// @media (max-width something) needs a custom replace due to compiler not playing nice
contents = contents.replace(/(@media[a-z0-9_ \-\.="'@+,*\{\}\(\):\!*\/]*){[ ]*\n/gi, mediaPrefix+'$1\n{'); // { same line
contents = contents.replace(/(@media[a-z0-9_ \-\.="'@+,*\{\}\(\):\!*\/]*)/gi, mediaPrefix+'$1'); // { next line
// xenforo variables need to be strings out
contents = contents.replace(/@(?!media|keyframes|font-face|import)([a-z\.\-]*)/ig, stringWrap[0]+'@$1'+stringWrap[1]);
return contents;
};
var escapeRegex = function(regex) {
return regex.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
}
var postCompile = function(contents) {
contents = contents.replace(new RegExp(escapeRegex(commentWrap[0]), 'g'), '');
contents = contents.replace(new RegExp(escapeRegex(commentWrap[1]), 'g'), '');
contents = contents.replace(new RegExp(escapeRegex(stringWrap[0]), 'g'), '');
contents = contents.replace(new RegExp(escapeRegex(stringWrap[1]), 'g'), '');
contents = contents.replace(new RegExp(escapeRegex(mediaPrefix), 'g'), '');
return contents;
};
var contents = preCompile(grunt.file.read('test.scss'));
grunt.file.write('test2.css', contents);
grunt.file.write('test3.css', postCompile(contents));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment