Skip to content
This repository was archived by the owner on May 29, 2020. It is now read-only.

Commit 3703907

Browse files
cesarizusapegin
authored andcommitted
Support multiple stylesheets types (#349)
1 parent 17376ff commit 3703907

File tree

4 files changed

+73
-30
lines changed

4 files changed

+73
-30
lines changed

Diff for: Gruntfile.js

+9
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ module.exports = function(grunt) {
126126
stylesheet: 'less'
127127
}
128128
},
129+
css_plus_scss: {
130+
src: 'test/src/*.svg',
131+
dest: 'test/tmp/sass',
132+
destCss: 'test/tmp/css',
133+
destScss: 'test/tmp/scss',
134+
options: {
135+
stylesheets: ['css', 'scss']
136+
}
137+
},
129138
stylus_bem: {
130139
src: 'test/src/*.svg',
131140
dest: 'test/tmp/stylus_bem',

Diff for: Readme.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Directory for resulting files.
119119

120120
Type: `string` Default: _`dest` value_
121121

122-
Directory for resulting CSS files (if different than font directory).
122+
Directory for resulting CSS files (if different than font directory). You can also define `destScss`, `destSass`, `destLess` and `destStyl` to specify a directory per stylesheet type.
123123

124124
#### Options
125125

@@ -257,11 +257,11 @@ options: {
257257
}
258258
```
259259

260-
#### stylesheet
260+
#### stylesheets
261261

262-
Type: `string` Default: `'css'` or extension of `template`
262+
Type: `array` Default: `['css']` or extension of `template`
263263

264-
Stylesheet type. Can be `css`, `sass`, `scss` or `less`. If `sass` or `scss` is used, `_` will prefix the file (so it can be a used as a partial).
264+
Stylesheet type. Can be `css`, `sass`, `scss` or `less`. If `sass` or `scss` is used, `_` will prefix the file (so it can be a used as a partial). You can define just `stylesheet` if you are generating just one type.
265265

266266
#### relativeFontPath
267267

Diff for: tasks/webfont.js

+52-26
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,18 @@ module.exports = function(grunt) {
8181
logger: logger,
8282
fontBaseName: options.font || 'icons',
8383
destCss: options.destCss || params.destCss || params.dest,
84+
destScss: options.destScss || params.destScss || params.destCss || params.dest,
85+
destSass: options.destSass || params.destSass || params.destCss || params.dest,
86+
destLess: options.destLess || params.destLess || params.destCss || params.dest,
87+
destStyl: options.destStyl || params.destStyl || params.destCss || params.dest,
8488
dest: options.dest || params.dest,
8589
relativeFontPath: options.relativeFontPath,
8690
addHashes: options.hashes !== false,
8791
addLigatures: options.ligatures === true,
8892
template: options.template,
8993
syntax: options.syntax || 'bem',
9094
templateOptions: options.templateOptions || {},
91-
stylesheet: options.stylesheet || path.extname(options.template).replace(/^\./, '') || 'css',
95+
stylesheets: options.stylesheets || [options.stylesheet || path.extname(options.template).replace(/^\./, '') || 'css'],
9296
htmlDemo: options.htmlDemo !== false,
9397
htmlDemoTemplate: options.htmlDemoTemplate,
9498
htmlDemoFilename: options.htmlDemoFilename,
@@ -117,6 +121,13 @@ module.exports = function(grunt) {
117121

118122
o = _.extend(o, {
119123
fontName: o.fontBaseName,
124+
destCssPaths: {
125+
css: o.destCss,
126+
scss: o.destScss,
127+
sass: o.destSass,
128+
less: o.destLess,
129+
styl: o.destStyl
130+
},
120131
relativeFontPath: o.relativeFontPath || path.relative(o.destCss, o.dest),
121132
destHtml: options.destHtml || o.destCss,
122133
fontfaceStyles: has(o.styles, 'font'),
@@ -160,7 +171,9 @@ module.exports = function(grunt) {
160171
}
161172
else {
162173
generatedFiles.push(getDemoFilePath());
163-
generatedFiles.push(getCssFilePath());
174+
o.stylesheets.forEach(function(stylesheet) {
175+
generatedFiles.push(getCssFilePath(stylesheet));
176+
});
164177

165178
regenerationNeeded = _.some(generatedFiles, function(filename) {
166179
if (!filename) return false;
@@ -185,7 +198,7 @@ module.exports = function(grunt) {
185198
cleanOutputDir,
186199
generateFont,
187200
generateWoff2Font,
188-
generateStylesheet,
201+
generateStylesheets,
189202
generateDemoHtml,
190203
generateCustomOutputs,
191204
printDone
@@ -237,7 +250,9 @@ module.exports = function(grunt) {
237250
* @param {Function} done
238251
*/
239252
function createOutputDirs(done) {
240-
mkdirp.sync(o.destCss);
253+
o.stylesheets.forEach(function(stylesheet) {
254+
mkdirp.sync(option(o.destCssPaths, stylesheet));
255+
});
241256
mkdirp.sync(o.dest);
242257
done();
243258
}
@@ -248,7 +263,7 @@ module.exports = function(grunt) {
248263
* @param {Function} done
249264
*/
250265
function cleanOutputDir(done) {
251-
var htmlDemoFileMask = path.join(o.destCss, o.fontBaseName + '*.{' + o.stylesheet + ',html}');
266+
var htmlDemoFileMask = path.join(o.destCss, o.fontBaseName + '*.{css,html}');
252267
var files = glob.sync(htmlDemoFileMask).concat(wf.generatedFontFiles(o));
253268
async.forEach(files, function(file, next) {
254269
fs.unlink(file, next);
@@ -310,7 +325,28 @@ module.exports = function(grunt) {
310325
*
311326
* @param {Function} done
312327
*/
313-
function generateStylesheet(done) {
328+
function generateStylesheets(done) {
329+
// Convert codepoints to array of strings
330+
var codepoints = [];
331+
_.each(o.glyphs, function(name) {
332+
codepoints.push(o.codepoints[name].toString(16));
333+
});
334+
o.codepoints = codepoints;
335+
336+
// Prepage glyph names to use as CSS classes
337+
o.glyphs = _.map(o.glyphs, classnameize);
338+
339+
o.stylesheets.forEach(generateStylesheet);
340+
341+
done();
342+
}
343+
344+
/**
345+
* Generate CSS
346+
*
347+
* @param {String} stylesheet type: css, scss, ...
348+
*/
349+
function generateStylesheet(stylesheet) {
314350
o.relativeFontPath = normalizePath(o.relativeFontPath);
315351

316352
// Generate font URLs to use in @font-face
@@ -324,24 +360,14 @@ module.exports = function(grunt) {
324360
});
325361
});
326362

327-
// Convert them to strings that could be used in CSS
328-
var fontSrcSeparator = option(wf.fontSrcSeparators, o.stylesheet);
363+
// Convert urls to strings that could be used in CSS
364+
var fontSrcSeparator = option(wf.fontSrcSeparators, stylesheet);
329365
fontSrcs.forEach(function(font, idx) {
330366
// o.fontSrc1, o.fontSrc2
331367
o['fontSrc'+(idx+1)] = font.join(fontSrcSeparator);
332368
});
333369
o.fontRawSrcs = fontSrcs;
334370

335-
// Convert codepoints to array of strings
336-
var codepoints = [];
337-
_.each(o.glyphs, function(name) {
338-
codepoints.push(o.codepoints[name].toString(16));
339-
});
340-
o.codepoints = codepoints;
341-
342-
// Prepage glyph names to use as CSS classes
343-
o.glyphs = _.map(o.glyphs, classnameize);
344-
345371
// Read JSON file corresponding to CSS template
346372
var templateJson = readTemplate(o.template, o.syntax, '.json', true);
347373
if (templateJson) o = _.extend(o, JSON.parse(templateJson.template));
@@ -353,20 +379,19 @@ module.exports = function(grunt) {
353379
var ext = path.extname(o.template) || '.css'; // Use extension of o.template file if given, or default to .css
354380
o.cssTemplate = readTemplate(o.template, o.syntax, ext);
355381
var cssContext = _.extend(o, {
356-
iconsStyles: true
382+
iconsStyles: true,
383+
stylesheet: stylesheet
357384
});
358385

359386
var css = renderTemplate(o.cssTemplate, cssContext);
360387

361388
// Fix CSS preprocessors comments: single line comments will be removed after compilation
362-
if (has(['sass', 'scss', 'less', 'styl'], o.stylesheet)) {
389+
if (has(['sass', 'scss', 'less', 'styl'], stylesheet)) {
363390
css = css.replace(/\/\* *(.*?) *\*\//g, '// $1');
364391
}
365392

366393
// Save file
367-
fs.writeFileSync(getCssFilePath(), css);
368-
369-
done();
394+
fs.writeFileSync(getCssFilePath(stylesheet), css);
370395
}
371396

372397
/**
@@ -747,11 +772,12 @@ module.exports = function(grunt) {
747772
/**
748773
* Return path of CSS file.
749774
*
775+
* @param {String} stylesheet (css, scss, ...)
750776
* @return {String}
751777
*/
752-
function getCssFilePath() {
753-
var cssFilePrefix = option(wf.cssFilePrefixes, o.stylesheet);
754-
return path.join(o.destCss, cssFilePrefix + o.fontBaseName + '.' + o.stylesheet);
778+
function getCssFilePath(stylesheet) {
779+
var cssFilePrefix = option(wf.cssFilePrefixes, stylesheet);
780+
return path.join(option(o.destCssPaths, stylesheet), cssFilePrefix + o.fontBaseName + '.' + stylesheet);
755781
}
756782

757783
/**

Diff for: test/webfont_test.js

+8
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,14 @@ exports.webfont = {
388388
test.done();
389389
},
390390

391+
css_plus_scss: function(test) {
392+
test.ok(fs.existsSync('test/tmp/scss/_icons.scss'), 'SCSS file with underscore created.');
393+
test.ok(!fs.existsSync('test/tmp/scss/icons.scss'), 'SCSS file without underscore not created.');
394+
test.ok(fs.existsSync('test/tmp/css/icons.css'), 'CSS file is created.');
395+
396+
test.done();
397+
},
398+
391399
stylus_bem: function(test) {
392400
test.ok(fs.existsSync('test/tmp/stylus_bem/icons.styl'), 'Stylus file created.');
393401
test.ok(!fs.existsSync('test/tmp/stylus_bem/icons.css'), 'CSS file not created.');

0 commit comments

Comments
 (0)