【问题标题】:How to generate html page with elements from scss in Gulp如何在 Gulp 中使用来自 scss 的元素生成 html 页面
【发布时间】:2016-03-29 12:34:18
【问题描述】:

我需要制作简单的 html 页面,比如图标样式表。

Gulp task 'iconfont' 使用以下代码从 svg 图标生成字形字体:

gulp.task('iconfont', function(){
  return gulp.src(['assets/svg/*.svg'])
    .pipe(iconfontCss({
        fontName: 'my-icons',
        cssClass: 'icon',
        path: 'conf/icon-font.scss',
        targetPath: '../../scss/layout/_icon-font.scss',
        fontPath: '../fonts/'
    }))
    .pipe(iconfont({
        fontName: 'my-icons',
        prependUnicode: false,
        formats: ['ttf', 'eot', 'woff'],
        normalize: true,
        centerHorizontally: true
}))
    .on('glyphs', function(glyphs, options) {
        // CSS templating, e.g.
        console.log(glyphs, options);
    })
    .pipe(gulp.dest('assets/fonts/'));
});

并生成带有类的 .icon-font scss 文件:

.icon-calendar {
    @include icon(calendar);
}
.icon-circle {
    @include icon(circle);
}
.icon-sun {
    @include icon(sun);
}
.icon-home {
    @include icon(home);
}

是否可以生成简单的 html 页面,包含具有这些类名的元素:

<i class="icon-calendar">.icon-calendar</i>
<i class="icon-circle">.icon-circle</i>
<i class="icon-sun">.icon-sun</i>
<i class="icon-home">.icon-home</i>

【问题讨论】:

  • 是的,只需读取生成的css,解析并生成一个新的html文件。使用fs节点模块读取,gulp-wrapnpmjs.com/package/gulp-wrap从模板生成html
  • 您可以使用具有 gulp 插件的模板引擎(车把等),然后只需为类名创建一个带有占位符的模板。您只需要动态计算出这些逻辑。

标签: javascript html gulp glyph icon-fonts


【解决方案1】:

这是一个使用jade 模板引擎的示例。这将读取文件./test.scss,提取所有icon-* 单词并生成./template.html 文件:

Gulpfile.js

// npm i gulp gulp-jade --save-dev

var gulp = require('gulp'),
    jade = require('gulp-jade'),
    fs   = require('fs');


gulp.task('default', function () {
    var re = new RegExp(/icon-(\w+)/);

    fs.readFile('./test.scss', 'utf8', function(err, data) {
        var icons = []
        if(err)
            return console.log(err);
        data.split('\r\n').forEach(function(icon) {
            var match = re.exec(icon);
            if(match)
                icons.push('icon-' + match[1])
        })
        // the gulp-jade plugin expects template local data to be an object
        // such as:
        // {locals: YOUR_DATA_OBJECT_TO_BIND}
        bind({locals: {icons: icons}})
    });

    // method that will bind data to your template
    var bind = function(data) {     
        gulp.src('./template.jade')
            .pipe(jade(data))
            .pipe(gulp.dest('./'))
    }
});

./test.scss

.icon-calendar {
    @include icon(calendar);
}
.icon-circle {
    @include icon(circle);
}
.icon-sun {
    @include icon(sun);
}
.icon-home {
    @include icon(home);
}

./template.jade

icons 变量来自.pipe(jade(data)) 调用中的{locals: {icons: {}} 参数。

doctype html
html(lang="en")
    head
    // you may want to add a link to your compiled `css` file for a nicer display
    body
        for ic in icons
            i(class=ic)
                |.
                = ic

有用的链接

【讨论】:

  • 我对上述代码有疑问。当我运行我的 GULP 任务时,一切正常,除了 html 文件之外,一切正常。它只生成我在 SCSS 图标文件中列出的第一个图标。目前我在文件中有 4 个图标,但生成后只显示 1 个。我是否遗漏了 JADE 文件中的某些内容以使其重复并继续生成我的 SCSS 中列出的每个图标? (仅供参考,我正在为一家公司构建样式指南,并且需要列出每次有人将一个图标放入 SVG 文件夹时创建的所有图标)感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多