|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var path = require('path'), |
| 4 | + gutil = require('gulp-util'), |
| 5 | + consolidate = require('consolidate'), |
| 6 | + _ = require('lodash'), |
| 7 | + Stream = require('stream'); |
| 8 | + |
| 9 | +var PLUGIN_NAME = 'gulp-iconfont-css'; |
| 10 | + |
| 11 | +function iconfontCSS(config) { |
| 12 | + var glyphMap = [], |
| 13 | + currentGlyph, |
| 14 | + inputFilePrefix, |
| 15 | + stream, |
| 16 | + outputFile, |
| 17 | + engine; |
| 18 | + |
| 19 | + // Set default values |
| 20 | + config = _.merge({ |
| 21 | + path: __dirname + '/_icons.scss', |
| 22 | + targetPath: '_icons.scss', |
| 23 | + engine: 'lodash', |
| 24 | + firstGlyph: 0xE001 |
| 25 | + }, config); |
| 26 | + |
| 27 | + // Validate config |
| 28 | + if (!consolidate[config.engine]) { |
| 29 | + throw new gutil.PluginError(PLUGIN_NAME, 'Consolidate missing template engine "' + config.engine + '"'); |
| 30 | + } |
| 31 | + try { |
| 32 | + engine = require(config.engine); |
| 33 | + } catch(e) { |
| 34 | + throw new gutil.PluginError(PLUGIN_NAME, 'Template engine "' + config.engine + '" not present'); |
| 35 | + } |
| 36 | + |
| 37 | + // Define starting point |
| 38 | + currentGlyph = config.firstGlyph; |
| 39 | + |
| 40 | + // Happy streaming |
| 41 | + stream = Stream.PassThrough({ |
| 42 | + objectMode: true |
| 43 | + }); |
| 44 | + |
| 45 | + stream._transform = function(file, unused, cb) { |
| 46 | + if (file.isNull()) { |
| 47 | + this.push(file); |
| 48 | + return cb(); |
| 49 | + } |
| 50 | + |
| 51 | + // Create output file |
| 52 | + if (!outputFile) { |
| 53 | + outputFile = new gutil.File({ |
| 54 | + base: file.base, |
| 55 | + cwd: file.cwd, |
| 56 | + path: path.join(file.base, config.targetPath), |
| 57 | + contents: file.isBuffer() ? new Buffer(0) : new Stream.PassThrough() |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + // Add glyph |
| 62 | + glyphMap.push({ |
| 63 | + filename: path.basename(file.path, '.svg'), |
| 64 | + codepoint: currentGlyph |
| 65 | + }); |
| 66 | + |
| 67 | + // Prepend codepoint to input file path for gulp-iconfont |
| 68 | + inputFilePrefix = 'u' + (currentGlyph).toString(16).toUpperCase() + '-'; |
| 69 | + |
| 70 | + file.path = path.dirname(file.path) + '/' + inputFilePrefix + path.basename(file.path); |
| 71 | + |
| 72 | + // Increase counter |
| 73 | + currentGlyph++; |
| 74 | + |
| 75 | + this.push(file); |
| 76 | + cb(); |
| 77 | + }; |
| 78 | + |
| 79 | + stream._flush = function(cb) { |
| 80 | + var content; |
| 81 | + |
| 82 | + if (glyphMap.length) { |
| 83 | + consolidate[config.engine](config.path, { |
| 84 | + glyphs: glyphMap |
| 85 | + }, function(error, html) { |
| 86 | + if (error) { |
| 87 | + throw error; |
| 88 | + } |
| 89 | + |
| 90 | + content = Buffer(html); |
| 91 | + |
| 92 | + if (outputFile.isBuffer()) { |
| 93 | + outputFile.contents = content; |
| 94 | + } else { |
| 95 | + outputFile.contents.write(content); |
| 96 | + outputFile.contents.end(); |
| 97 | + } |
| 98 | + |
| 99 | + stream.push(outputFile); |
| 100 | + |
| 101 | + cb(); |
| 102 | + }); |
| 103 | + } else { |
| 104 | + cb(); |
| 105 | + } |
| 106 | + }; |
| 107 | + |
| 108 | + return stream; |
| 109 | +}; |
| 110 | + |
| 111 | +module.exports = iconfontCSS; |
0 commit comments