Gulp 使用 includePaths 载入外部 Sass资源 DAY95

若我们要使用外部套件载入的 Sass
那要怎麽载入呢??

这里我们以 Bootstrap为例子
先使用 npm 的方式来载入
首先我们先砍掉 bower 载入的 Bootstrap

bower uninstall bootstrap --save

再以 npm 方式载入 Bootstrap

npm install bootstrap --save

这里可能会出错
但不会影响次次要介绍的内容
明天将介绍如何解决此错误

并把 source 资料夹改名为 stylesheets
当然 watch 与 sass 都必须改动它的 gulp.src来源

gulp.task('sass', function () {
    // 更改地方
    return gulp.src('./source/stylesheets/**/*.scss')
    .pipe($.plumber())
    .pipe($.sourcemaps.init())
    .pipe($.sass({
        outputStyle: 'nested',
    }).on('error', $.sass.logError))
    .pipe($.postcss([autoprefixer()]))
    .pipe($.if(option.env === 'production',$.cleanCss()))
    .pipe($.sourcemaps.write('.'))
    .pipe(gulp.dest('./public/stylesheet'))
    .pipe(browserSync.stream())
});
gulp.task('watch', function () {
    // 更改地方
    gulp.watch('./source/stylesheets/**/*.scss', ['sass']);
    gulp.watch('./source/**/*.jade', ['jade']);
    gulp.watch('./source/js/**/*.js', ['babel']);
});

更改完後 index.jade当然也要更改

<!DOCTYPE html>
html(lang="en")
    head
        meta(charset="UTF-8")
        meta(name="viewport", content="width=device-width, initial-scale=1.0")
        title Document
        // 更改地方
        link(rel="stylesheet", href="stylesheet/all.css")
        script(src="js/all.js")
    body

接下来我们就必须增加 includePaths

gulp.task('sass', function () {
    return gulp.src('./source/stylesheets/**/*.scss')
    .pipe($.plumber())
    .pipe($.sourcemaps.init())
    .pipe($.sass({
        outputStyle: 'nested',
        includePaths: ['./node_modules/bootstrap/scss']
    }).on('error', $.sass.logError))
    .pipe($.postcss([autoprefixer()]))
    .pipe($.if(option.env === 'production',$.cleanCss()))
    .pipe($.sourcemaps.write('.'))
    .pipe(gulp.dest('./public/stylesheet'))
    .pipe(browserSync.stream())
});

接着
我们在all.scss 输入

@import "bootstrap"

当然若你想要客制化你的样式
可以尝试另一种方法(可看 twbs 来决定你想要载入的 scss档)
https://github.com/twbs/bootstrap/blob/main/scss/bootstrap.scss

那我们就来介绍第二种载入方式啦
在 stylesheets 新增一个 helpers 资料夹
然後我们复制 node_modules/scss/_variables.scss 到 helpers
https://ithelp.ithome.com.tw/upload/images/20201124/20123039hCc0irIbwi.jpg

这一份_variables.scss

你可以自行调整你所想要的样式
https://ithelp.ithome.com.tw/upload/images/20201124/20123039LLprjpPqsg.jpg

那今天的介绍就到这里啦
若有任何问题 或 内容有误
都可以跟我说唷/images/emoticon/emoticon41.gif


<<:  【资料结构】读档相关 12/18更

>>:  Day 19 - 卷积神经网络 CNN (4)-Pooling layer & Activation Function

Day03:小姐,你手上那是什麽?

昨天提到了一个奇怪的现象: byte num = 128; 如上撰写,你的IDE将会在128底下亮出...

[05] 挂telegram机器人的hook

把上一篇刚打得code删一删 指留下需要的 post 有 data 的部分来呼叫 hook 相关功能...

那些被忽略但很好用的 Web API / 拖拉式待办清单

就算拖拖拉拉,也可以把待办事项处理好 昨天虽然已经知道该如何使用 Drag & Drop ...

day17:First-class function

在了解 first-class 一等公民函式前,我们先来看看 MDN 对於 first class ...

【Day24】闭包(Closure)

今天我要讲解的是闭包(Closure),在进入之前我们先来看一段程序码, 首先准备一个随机生成字串的...