【问题标题】:SASS / Gulp: Using mixins and vars from another fileSASS / Gulp:使用来自另一个文件的 mixins 和 vars
【发布时间】:2020-03-26 13:37:50
【问题描述】:

我有以下文件夹结构设置:

theme
   - assets
      - sass
         - _theme-styles.scss
      - _styles.scss
   - modules
      - hero
         - hero.html
         - hero.scss
         - hero.css (gulp will compile this file)

_theme-styles.scss 目前真的很简单,看起来像这样:

// VARS
$white: #FFFFFF;

_styles.scss 看起来像这样:

@import "../../../node_modules/bootstrap/scss/bootstrap.scss";
@import "../../../node_modules/bootstrap/scss/variables.scss";
@import "../../../node_modules/bootstrap/scss/mixins.scss";

$grid-breakpoints: (
  xs: 0px,
  sm: 576px,
  md: 786px,
  lg: 992px,
  xl: 1200px,
);

// Import Globals
@import "config/**.scss";

作为参考,我的gulpfile.js 看起来像这样:

'use strict';

var gulp = require('gulp');
var sass = require('gulp-sass');
var autoprefixer = require('autoprefixer');
var postcss = require('gulp-postcss');
var paths = {
    styles: {
        src: 'modules/**/*.scss',
        dest: 'modules'
    }
}
function scss() {
    return gulp.src(paths.styles.src)
        .pipe(sass().on('error', sass.logError))
        .pipe(sass({ outputStyle: 'compressed' }))
        .pipe(postcss([autoprefixer()]))
        .pipe(gulp.dest(paths.styles.dest));
}
exports.scss = scss
function watch() {

    scss()

    gulp.watch(paths.styles.src, scss);
}
exports.watch = watch

我要做什么

我想引用其他文件中定义的variablesmixins。例如,按照我当前的工作流程设置,在 hero.scss 我有:

.hero { background: $white; }

我已经在 _theme-styles.scss 中定义了这个变量,并且希望能够在这里使用它而无需重新定义。

同样,当使用 mixins 时,例如下面的例子:

.hero{
    @include media-breakpointup(md){
        padding: 60px;
    }
}

上面的mixin是在bootstrap节点模块文件夹中定义的。但是当引用这些时,我在运行gulp watch 时得到undefined errors

【问题讨论】:

    标签: sass gulp mixins gulp-watch


    【解决方案1】:

    您应该在hero.scss 的顶部

    @use 'pathsToYourOtherSCSSFiles';
    

    喜欢:

    @use '../assets/sass/theme-styles';
    @use '../assets/styles';
    and same to the bootstrap file with the mixin defined
    

    我没有测试这些路径,但它们应该是从您的 hero.scss 文件到您想要的 @use 文件的相对路径。见https://sass-lang.com/guideModules

    【讨论】:

    • 在我创建的每个模块中不添加上述内容有什么办法吗?
    • 唯一可能的选择是在你的模块或资产文件夹的顶部有一个scss文件@use那里的所有文件,然后@use下面的每个子scss是那个文件.我还没有尝试过,但我相信您可以以这种方式链接@use 调用。在任何情况下,您都必须在每个模块中添加 @use 一些东西——要么是一个收集所有 @uses 的文件,要么是分别收集它们中的每一个。
    • 我正在研究一种更“主文件”生成的方法。例如,就像这篇文章中回答的那样:stackoverflow.com/questions/17598996/…。但是,正如您在我原来的问题中看到的那样,使用 @import 仍然会出现未定义的错误
    • 这些行看起来不正确:@import "../../../node_modules/bootstrap/scss/bootstrap.scss"; 你的意思是@import "./././node_modules/bootstrap/scss/bootstrap.scss"; 还是@import "../node_modules/bootstrap/scss/bootstrap.scss";
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    相关资源
    最近更新 更多