Created
April 19, 2015 20:17
-
-
Save apoleshchuk/2df7681e9fae7f745c1b to your computer and use it in GitHub Desktop.
This file contains 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
module.exports = function(grunt) { | |
'use strict'; | |
var path = require('path'); | |
grunt.registerMultiTask('build-icons', 'Collect font icons by common.deps.js and build font', function() { | |
var options = this.options({ | |
block: 'icon', | |
mod: 'font', | |
origin: 'svg', | |
ext: '.svg', | |
webfont: { | |
font: 'icon_font', | |
hashes: false, | |
startCodepoint: 0x00c4, | |
templateOptions: { | |
baseClass: 'icon', | |
classPrefix: 'icon_', | |
mixinPrefix: 'icon-' | |
} | |
}, | |
copy: { | |
process: function (content) { | |
return content.replace( | |
/url\((.*?)\)/g, 'url(get-abs-url($1))' | |
).replace( | |
/url\(get-abs-url\("(.*)\?#(.*)"\)\)/g, | |
"url(s('%s?#$2', unquote(get-abs-url('$1'))))" | |
); | |
} | |
} | |
}); | |
var cwd = process.cwd(); | |
var targetName = [this.name, this.target].join('_'); | |
this.files.forEach(function(file) { | |
var deps = file.src.reduce(function(deps, filePath) { | |
var splitPath = filePath.split(path.sep); | |
var bundlePath = splitPath.slice(0, splitPath.length - 2).join(path.sep); | |
var levelsDir = [cwd, bundlePath, '.bem'].join(path.sep); | |
var levelsPath = [levelsDir, 'levelPaths.js'].join(path.sep); | |
var levels = require(levelsPath).levels.map(function(level) { | |
return path.resolve(levelsDir, level); | |
}); | |
return deps.concat(require([cwd, filePath].join(path.sep)).deps.filter(function(dep) { | |
return dep.block == options.block && dep.mod == options.mod && dep.val; | |
})).map(function(dep) { | |
dep.levels = levels; | |
return dep; | |
}); | |
}, []); | |
var iconList = deps.reduce(function(iconList, dep) { | |
var iconPath = dep.levels.reduce(function(lastPath, levelPath) { | |
var iconPath = path.join(levelPath, dep.block, '_' + dep.mod, options.origin, dep.val + options.ext); | |
if (grunt.file.isFile(iconPath)) { | |
return iconPath; | |
} | |
return lastPath; | |
}, false); | |
if (iconPath) { | |
iconList.push(iconPath); | |
} | |
return iconList; | |
}, []); | |
grunt.verbose.writeln('Icons ' + iconList.join().cyan + ', dest ' + file.dest.cyan + '.'); | |
var fontDir = path.dirname(file.dest); | |
grunt.config('webfont.' + targetName, { | |
options: options.webfont, | |
src: iconList, | |
dest: fontDir | |
}); | |
grunt.config('copy.' + targetName, { | |
options: options.copy, | |
files: [ | |
{ | |
src: path.join(fontDir, options.webfont.font + '.css'), | |
dest: file.dest | |
} | |
] | |
}); | |
grunt.task.run(['webfont:' + targetName, 'copy:' + targetName]); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment