Skip to content

Commit 20769e2

Browse files
committed
v0.0.1
1 parent 0e793cd commit 20769e2

25 files changed

+198
-601
lines changed

.gitignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
*~
2-
*.swp
3-
node_modules/
1+
.DS_Store
2+
node_modules
43
test/results
5-
.git

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
tests/results
1+
.DS_Store
2+
test/results

README.md

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,51 @@
1-
# gulp-iconfont-scss [![NPM version](https://badge.fury.io/js/gulp-iconfont.png)](https://npmjs.org/package/gulp-iconfont-scss) [![Build status](https://api.travis-ci.org/backflip/gulp-iconfont-scss.png)](https://travis-ci.org/backflip/gulp-iconfont-scss)
2-
> Create a CSS from fonts with [Gulp](http://gulpjs.com/).
1+
# gulp-iconfont-css
2+
> Generate (S)CSS file for icon font created with [Gulp](http://gulpjs.com/)
33
44
## Usage
55

6-
First, install `gulp-iconfont-scss` as a development dependency:
6+
First, install `gulp-iconfont-css` as a development dependency:
77

88
```shell
9-
npm install --save-dev gulp-iconfont-scss
9+
npm install --save-dev gulp-iconfont-css
1010
```
1111

1212
Then, add it to your `gulpfile.js`:
1313

1414
```javascript
1515
var iconfont = require('gulp-iconfont');
16-
var scss = require('gulp-iconfont-scss');
16+
var iconfontCss = require('gulp-iconfont-css');
1717

18-
gulp.task('Iconfont', function(){
19-
gulp.src(['assets/icons/*.svg'])
20-
.pipe(scss({
18+
gulp.task('iconfont', function(){
19+
gulp.src(['app/assets/icons/*.svg'])
20+
.pipe(iconfontCss({
2121

2222
})
2323
.pipe(iconfont({
24-
fontName: 'myfont', // required
25-
appendCodepoints: true // recommanded option
24+
fontName: 'Icons'
2625
}))
27-
.pipe(gulp.dest('www/fonts/'));
26+
.pipe(gulp.dest('app/assets/fonts/icons/'));
2827
});
2928
```
3029
31-
`gulp-iconfont-scss` suits well with `gulp-iconfont` but you can use it in a
32-
more modular fashion by directly useing `gulp-svgicons2svgfont`,
33-
`gulp-svg2tff`, `gulp-ttf2eot` and/or `gulp-ttf2woff`.
30+
`gulp-iconfont-css` works well with `gulp-iconfont` but you can use it in a more modular fashion by directly using `gulp-svgicons2svgfont`, `gulp-svg2tff`, `gulp-ttf2eot` and/or `gulp-ttf2woff`.
3431
3532
## API
3633
37-
### scss(options)
34+
### iconfontCSS(options)
3835
39-
#### options.template
36+
#### options.path
4037
Type: `String`
4138
42-
The template path (required).
39+
The template path (optional, defaults to _icons.css provided with plugin).
4340
44-
#### options.ouputfile
41+
#### options.targetPath
4542
Type: `String`
4643
47-
The json path (required).
44+
The path where the (S)CSS file should be saved, relative to the path used in ```gulp.dest()``` (optional, defaults to ```_icons.css```).
45+
46+
#### options.engine
47+
Type: `String`
48+
49+
The template engine to use (optional, defaults to ```lodash```).
50+
See https://github.com/visionmedia/consolidate.js/ for available engines. The engine has to be installed before using.
4851

_icons.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<% _.each(glyphs, function(glyph) { %>.<%= glyph.filename %> {
2+
content: "\u<%= glyph.codepoint %>";
3+
}
4+
<% }); %>

icons.scss.swig

Lines changed: 0 additions & 5 deletions
This file was deleted.

index.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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;

package.json

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
{
2-
"name": "gulp-iconfont-scss",
3-
"version": "0.0.3",
4-
"description": "Create a SCSS file for the iconfonts.",
5-
"main": "src/index.js",
2+
"name": "gulp-iconfont-css",
3+
"version": "0.0.1",
4+
"description": "Generate (S)CSS file for icon font created with Gulp",
5+
"license": "MIT",
6+
"repository": "backflip/gulp-iconfont-css",
7+
"author": "Thomas Jaggi, Nicolas Froidure",
8+
"engines": {
9+
"node": ">=0.10.0"
10+
},
611
"scripts": {
7-
"test": "node_modules/mocha/bin/mocha tests/*.mocha.js"
12+
"test": "mocha"
813
},
14+
"main": "./index.js",
915
"keywords": [
1016
"gulpplugin",
1117
"gulp",
1218
"icon",
1319
"font",
14-
"scss"
20+
"scss",
21+
"css"
1522
],
16-
"author": "Thomas Jaggi, Nicolas Froidure",
17-
"license": "MIT",
1823
"dependencies": {
1924
"gulp-util": "~2.2.0",
20-
"swig": "~1.3.2"
25+
"consolidate": "~0.10.0",
26+
"lodash": "~2.4.1"
2127
},
2228
"devDependencies": {
23-
"mocha": "~1.16.2"
29+
"mocha": "*",
30+
"event-stream": "~3.1.0"
2431
}
2532
}

src/index.js

Lines changed: 0 additions & 83 deletions
This file was deleted.

test/expected/_icons.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.icon-github {
2+
content: "\u57345";
3+
}
4+
.icon-twitter {
5+
content: "\u57346";
6+
}

test/fixtures/_icons.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<% _.each(glyphs, function(glyph) { %>.icon-<%= glyph.filename %> {
2+
content: "\u<%= glyph.codepoint %>";
3+
}
4+
<% }); %>

0 commit comments

Comments
 (0)