Skip to content

Instantly share code, notes, and snippets.

@rctay
Last active April 5, 2017 17:26
Show Gist options
  • Save rctay/281c45147defda285005ccb5bf9fc67a to your computer and use it in GitHub Desktop.
Save rctay/281c45147defda285005ccb5bf9fc67a to your computer and use it in GitHub Desktop.
function copy(o) {
var p = {};
for (var k in o)
p[k] = o[k];
return p;
}
function Mapper(source) {
this.source = source;
this.result = {};
}
Mapper.prototype.walk = function(keys, key) {
var source_curr = this.source;
var result_curr = this.result;
keys = keys.concat([key]);
for (var i = 0; i < keys.length; i++) {
source_curr = source_curr[keys[i]];
result_curr[keys[i]] = {};
result_curr = result_curr[keys[i]];
}
this.source_curr = source_curr;
this.result_curr = result_curr;
return keys;
};
Mapper.prototype.refer = function(keys, key_src, key_dest) {
if (key_dest === undefined)
key_dest = key_src;
this.result_curr[key_dest] = copy(this.source_curr[key_src]);
this.source_curr = this.source_curr[key_src];
this.result_curr = this.result_curr[key_dest];
};
Mapper.prototype.transfer = function(key, val_or_fn) {
this.result_curr[key] = typeof val_or_fn === "function" ?
val_or_fn(this.source_curr[key])
: val_or_fn;
};
function add_suffix(path_str, suffix) {
var parts = path.parse(path_str);
var name = parts.name;
var i = name.indexOf(".");
if (i !== -1)
name = name.slice(0, i) + suffix + name.slice(i);
else
name += suffix;
var new_path = path.format({
dir: parts.dir,
base: name+parts.ext
});
return new_path;
}
var add_noprint_suffix = function(path_str) {
return add_suffix(path_str, "-noprint");
};
function() {
var child1, child2, child3, root = [];
var mapper = new Mapper(config);
child1 = mapper.walk(root, "less");
child2 = mapper.refer(child1, "compileCore", "compileNoPrint");
mapper.transfer("src", add_noprint_suffix);
mapper.transfer("dest", add_noprint_suffix);
child3 = mapper.refer(child2, "options");
mapper.transfer("sourceMapURL", add_noprint_suffix);
mapper.transfer("sourceMapFilename", add_noprint_suffix);
child1 = mapper.walk(root, "autoprefixer");
child2 = mapper.refer(child1, "core", "noPrint");
mapper.transfer("src", add_noprint_suffix);
child1 = mapper.walk(root, "cssmin");
child2 = mapper.refer(child1, "minifyCore", "minifyNoPrint");
mapper.transfer("src", add_noprint_suffix);
mapper.transfer("dest", add_noprint_suffix);
child1 = mapper.walk(root, "csscomb");
child2 = mapper.refer(child1, "dist", "noPrint");
mapper.transfer("src", "*-noprint.css");
grunt.config.merge(mapper.result);
}
var lenses = require("lenses"),
compose = require('pointfree-fantasy').compose;
function add_suffix(path_str, suffix) {
var parts = path.parse(path_str);
var name = parts.name;
var i = name.indexOf(".");
if (i !== -1)
name = name.slice(0, i) + suffix + name.slice(i);
else
name += suffix;
var new_path = path.format({
dir: parts.dir,
base: name+parts.ext
});
return new_path;
}
var add_noprint_suffix = function(path_str) {
return add_suffix(path_str, "-noprint");
};
function over2(lens, src, key_src, key_dest, cb) {
return function(dest) {
var obj = {};
obj[key_dest] = cb(lenses.view(lens, src)[key_src]);
return lenses.set(lens, obj, dest);
};
}
function() {
var Lenses1 = lenses.makeLenses(["less", "autoprefixer", "cssmin", "csscomb"]);
var Lenses2 = lenses.makeLenses(["src", "dest", "options"]);
var Lenses_less_options = lenses.makeLenses(["sourceMapURL", "sourceMapFilename"]);
var config = grunt.config.get();
var transform = compose(
over2(Lenses1.less, config, "compileCore", "compileNoPrint",
compose(lenses.set(Lenses2.src, "less/bootstrap-noprint.less"),
lenses.over(Lenses2.dest, add_noprint_suffix),
lenses.over(Lenses2.options,
compose(lenses.over(Lenses_less_options.sourceMapURL, add_noprint_suffix),
lenses.over(Lenses_less_options.sourceMapFilename, add_noprint_suffix))))),
over2(Lenses1.autoprefixer, config, "core", "noPrint",
lenses.over(Lenses2.src, add_noprint_suffix)),
over2(Lenses1.cssmin, config, "minifyCore", "minifyNoPrint",
compose(lenses.over(Lenses2.src, add_noprint_suffix),
lenses.over(Lenses2.dest, add_noprint_suffix))),
over2(Lenses1.csscomb, config, "dist", "noPrint",
lenses.set(Lenses2.src, "*-noprint.css")));
var additional_config = {
copy: {
noPrintLess: {
src: "less/bootstrap.less",
dest: "less/bootstrap-noprint.less",
options: {
process: function(content, srcpath) {
return content.replace('@import "print.less"', "");
}
}
},
noPrintDist: {
src: "dist/css/bootstrap-noprint.*",
dest: dir_dest
}
}
};
additional_config = transform(additional_config);
grunt.config.merge(additional_config);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment