如何在.vue文件中将选项传递给SASS加载器以获取样式?

时间:2016-12-30 13:48:36

标签: sass webpack vue.js

我尝试配置vue-loader以使其在@import语句中包含node_modules。 scss文件的加载器配置工作正常如下:

{
  test: /\.(sass|scss)$/,
  use: [
    'style-loader',
    'css-loader',
    {
      loader: 'sass-loader',
      options: {
        includePaths: [path.resolve(__dirname, 'node_modules')],
      },
    },
  ],
},

所以现在我试图在.vue文件中使用它。 我想到了这样的事情:

{
  test: /\.vue$/,
  loader: 'vue-loader',
  options: {
    loaders: {
      // ?{"includePaths":["C:/Users/samuel/Code/school/tinf/sem03/proj01/node_modules"]}
      sass: 'vue-style-loader!css-loader!sass-loader?' +
        `includePaths[]=${path.resolve(__dirname, 'node_modules').replace(/\\/g, '/')}`,
    },
  },
},

我收到错误消息:

  错误在./~/css-loader!./~/vue-loader/lib/style-rewriter.js?id=data-v-1ec85c08!./~/sass-loader?includePaths[]=C :!?/用户/塞缪尔/代码/学校/ TINF / sem03 / proj01 / node_modules ./〜/ VUE装载机/ LIB / selector.js类型=样式和安培;索引= 0 ./ SRC /组件/ PageHeader.vue <! / p>

我已尝试从堆叠中删除vue-style-loadercss-loader,但仍然出现错误。

但是当我直接将选项传递给样式标记时,它可以正常工作:

<style lang="sass?{"includePaths":["C:/Users/samuel/Code/school/tinf/sem03/proj01/node_modules"]}">

如何修改加载器以使其正常工作?

3 个答案:

答案 0 :(得分:1)

您可以将其作为includePaths传递给module.exports = { ... module: { loaders: [ { test: /\.scss$/, loaders: ["sass-loader"] } ] }, sassLoader: { includePaths: [path.resolve(__dirname, "./node_modules")] } }; ,如下所示:

export RBENV_ROOT=/home/YOUR_USER_PATH/.rbenv
export PATH=$RBENV_ROOT/shims:$RBENV_ROOT/bin:$PATH

答案 1 :(得分:0)

糟糕!

给定的代码工作正常。

我收到错误File to import not found or unreadable: @material/typography,因为该文件不存在。我应该导入@material/typography/mdc-typography

这是一个愚蠢的问题,我很抱歉。

无论如何,这是我固定的webpack配置:

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const nodeModules = path.resolve(__dirname, 'node_modules');

module.exports = {
  devtool: 'source-map',
  entry: path.resolve(__dirname, './src/index.js'),
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            // IMPORTANT for scss (lang="sass") in .vue files
            sass: 'style-loader!css-loader!sass-loader?' +
              `includePaths[]=${nodeModules}`,
          },
        },
      },
      {
        test: /\.(sass|scss)$/,
        use: [
          'style-loader',
          'css-loader',
          // IMPORTANT for scss files
          { loader: 'sass-loader', options: { includePaths: [nodeModules] } },
        ],
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader'],
      },
    ],
  },
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  resolve: {
    extensions: ['.js', '.vue'],
    alias: {
      vue: 'vue/dist/vue.js',
    },
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'src/index.html',
    }),
  ],
};

答案 2 :(得分:0)

{
  test: /\.vue$/,
  loader: 'vue-loader',
  options: {
    loaders: {
      scss: [
        { 
          loader: 'sass-loader', options: {
              // here
          }
      ]
    }
  }
}

https://vue-loader.vuejs.org/en/options.html#loaders