|
| 1 | +import * as cheerio from 'cheerio'; |
| 2 | +import { dest, src, task } from 'gulp'; |
| 3 | +import concat from 'gulp-concat'; |
| 4 | +import { gulpPlugin } from 'gulp-plugin-extras'; |
| 5 | +import { basename } from 'path'; |
| 6 | + |
| 7 | +function createCopyright() { |
| 8 | + return `/** |
| 9 | + * @license |
| 10 | + * Copyright (c) 2015 - ${new Date().getFullYear()} Vaadin Ltd. |
| 11 | + * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ |
| 12 | + */`; |
| 13 | +} |
| 14 | + |
| 15 | +function transformIcon() { |
| 16 | + return gulpPlugin('gulp-transform-icon', (file) => { |
| 17 | + const id = basename(file.path, '.svg'); |
| 18 | + const svg = cheerio.load(file.contents, { xmlMode: true })('svg'); |
| 19 | + // Remove fill attributes. |
| 20 | + svg.children('[fill]').removeAttr('fill'); |
| 21 | + // Add closing tags instead of self-closing. |
| 22 | + const content = svg.children().toString().replace(/"\/>/gu, '"></path>'); |
| 23 | + console.warn(id, content); |
| 24 | + // Output the "meat" of the SVG as group element. |
| 25 | + file.contents = Buffer.from(`<g id="vaadin:${id}">${content}</g>`); |
| 26 | + return file; |
| 27 | + }); |
| 28 | +} |
| 29 | + |
| 30 | +function createIconset() { |
| 31 | + return gulpPlugin('gulp-create-iconset', (file) => { |
| 32 | + // Enclose all icons in a vaadin-iconset |
| 33 | + const contents = `${createCopyright()} |
| 34 | +import { Iconset } from '@vaadin/icon/vaadin-iconset.js'; |
| 35 | +
|
| 36 | +const template = document.createElement('template'); |
| 37 | +
|
| 38 | +template.innerHTML = \`<svg><defs>\n${file.contents}\n</defs></svg>\`; |
| 39 | +
|
| 40 | +Iconset.register('vaadin', 16, template);\n`; |
| 41 | + file.contents = Buffer.from(contents); |
| 42 | + return file; |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +task('icons', () => { |
| 47 | + return src(['assets/svg/*.svg'], { base: '.' }) |
| 48 | + .pipe(transformIcon()) |
| 49 | + .pipe(concat('vaadin-iconset.js')) |
| 50 | + .pipe(createIconset()) |
| 51 | + .pipe(dest('.')); |
| 52 | +}); |
0 commit comments