【问题标题】:Bootstrap Icons not displayed in Gulp 4Gulp 4 中未显示引导图标
【发布时间】:2021-07-18 10:39:24
【问题描述】:

我正在尝试将新的引导图标 npm 包与 gulp 4 一起使用,我的项目中的最新版本一切都安装得很好并且运行良好。

我遇到的问题是当我像这样在 index.html 文件中声明图标时:

<i class="bi bi-alarm"></i>

图标不显示我认为问题来自网络字体。

如果有人可以提供帮助,我是 gulp 的新手!

这是我的 gulpfile.js 文件:

"use strict";

const { src, dest, watch, series, parallel } = require("gulp");
const sass = require("gulp-sass");
const postcss = require("gulp-postcss");
const autoprefixer = require("autoprefixer");
const cssnano = require("cssnano");
const terser = require("gulp-terser");
const browsersync = require("browser-sync").create();

// FILE PATHS
const files = {
    scssPath: "src/assets/scss/app.scss",
    jsPath: "src/assets/js/app.js",
};

// COMPILE SCSS INTO CSS
function compileSCSS() {
    return src(files.scssPath, { sourcemaps: true })
        .pipe(sass().on("error", sass.logError))
        .pipe(postcss([autoprefixer(), cssnano()]))
        .pipe(dest("public/assets/css", { sourcemaps: "." }));
}

// COPY CUSTOM JS
function compileJS() {
    return src(files.jsPath, { sourcemaps: true })
        .pipe(terser())
        .pipe(dest("public/assets/js", { sourcemaps: "." }));
}

// BROWSER SYNC
function browserSyncInit(cb) {
    browsersync.init({
        server: {
            baseDir: ".",
        },
    });
    cb();
}

function browsersyncReload(cb) {
    browsersync.reload();
    cb();
}

// WATCH FILES
function watchFiles() {
    watch("*.html", browsersyncReload);
    watch([files.scssPath, files.jsPath], series(compileSCSS, compileJS, browsersyncReload));
}

exports.default = series(parallel(compileSCSS, compileJS), browserSyncInit, watchFiles);

这是我的 src/assets/scss/app.scss 文件:

@import "../../../node_modules/bootstrap-icons/font/bootstrap-icons";

这是我的文件结构:

my-project/
├── src/assets/scss
│   └── app.scss
├── src/assets/js
│   └── app.js
├── public/assets
│   └── css/
│   └── js/
└── node_modules/
└── index.html
└── gulpfile.js

【问题讨论】:

    标签: javascript css sass gulp font-awesome


    【解决方案1】:

    引导图标使用Webfont,但您只是导入了 SCSS,因此无法正常工作。让我们创建一个 Gulp 任务,将fonts 也复制到您的dest。

    Gulp 任务建议:

    function copyBootstrapFonts() {
      return gulp.src('path-to-bootstrap-icons/fonts')
        .pipe(gulp.dest('path-to-your-dest/fonts'));
    }
    

    或者您可以使用CDN 作为一种简单的方法。

    【讨论】:

    • 你能告诉我如何创建这个任务吗
    • @jhon-jhones 我刚刚添加了 Gulp 任务建议,未经测试,但它应该可以工作。如果您需要更多信息,请告诉我。
    • 它复制了目录的字体但它仍然没有显示图标??
    • 看来你需要把fonts文件夹和CSS文件一样放在dest。因为它们导入为./fonts/bootstrap-icons.woff2
    • 你的字体目标应该是public/assets/css/fonts/,如果有的话,不要忘记检查控制台中的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 2018-11-09
    • 2016-06-10
    相关资源
    最近更新 更多