我无法使用sass-resources-loader在vuejs中加载配置scss文件

时间:2018-10-14 12:27:57

标签: vue.js webpack sass-loader

webpack.base.conf.js

module: {
rules: [
  {
    test: /\.vue$/,
    loader: 'vue-loader',
    options: {
      loaders: {
        scss: 'vue-style-loader!css-loader!sass-loader',
        sass: 'vue-style-loader!css-loader!sass-loader?indentedSyntax'
      }
    }
  },
  {
    loader: 'sass-resources-loader',
    options: {
      resources: path.resolve(__dirname, '../src/assets/scss/_variables.scss')
    }
  },

我的“变量”文件开始加载,但随后出现此错误:

Module parse failed: Unexpected character '#' (1:8)
You may need an appropriate loader to handle this file type.
| $white: #ffffff;
| 
| // The Vue build version to load with the `import` command

我使用本手册:

https://vue-loader-v14.vuejs.org/en/configurations/pre-processors.html

vue版本:2.93

2 个答案:

答案 0 :(得分:0)

您可能希望将其添加到根目录中的 vue.config.js 文件中。如果该文件不存在,请创建该文件并添加一些内容(我的配置):

module.exports = {
css: {
    loaderOptions: {
      sass: {
        data: `
          @import "@/assets/_variables.scss";
        `
      }
    }
  }
};

答案 1 :(得分:0)

最终我使用vue-cli @ 3从头开始创建了项目 并添加到 vue.config.js 此代码:

const path = require('path');

module.exports = {
  chainWebpack: config => {
    const oneOfsMap = config.module.rule('scss').oneOfs.store
    oneOfsMap.forEach(item => {
      item
        .use('sass-resources-loader')
        .loader('sass-resources-loader')
        .options({
          resources: [
            path.resolve(__dirname, './src/assets/scss/_variables.scss'),
            path.resolve(__dirname, './src/assets/scss/_mixins.scss'),
          ]
        })
        .end()
    })
  }
}