1
votes

I'm trying to configure vue-loader to make it include node_modules in @import statements. The loader config for scss files that works fine looks like this:

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

So now I'm trying to get this working inside .vue files. I thought of something like this:

{
  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, '/')}`,
    },
  },
},

I get an error message:

ERROR in ./~/css-loader!./~/vue-loader/lib/style-rewriter.js?id=data-v-1ec85c08!./~/sass-loader?includePaths[]=C:/Users/samuel/Code/school/tinf/sem03/proj01/node_modules!./~/vue-loader/lib/selector.js?type=styles&index=0!./src/components/PageHeader.vue

I've tried removing vue-style-loader and css-loader from the stack, and I still get an error.

But when I directly pass the options into the style tag, it works fine:

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

How do I have to modify the loaders to make it work?

3

3 Answers

1
votes

You can pass it as includePaths which is a sass option like following:

module.exports = {
  ...
  module: {
    loaders: [
      {
        test: /\.scss$/,
        loaders: ["sass-loader"]
      }
    ]
  },
  sassLoader: {
    includePaths: [path.resolve(__dirname, "./node_modules")]
  }
};
0
votes

Ooops.

The given code works fine.

I got the error File to import not found or unreadable: @material/typography because the file does not exist. I should have imported @material/typography/mdc-typography.

That was one dumb issue, I'm so sorry.

Anyway, here's my fixed webpack config:

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',
    }),
  ],
};
0
votes
{
  test: /\.vue$/,
  loader: 'vue-loader',
  options: {
    loaders: {
      scss: [
        { 
          loader: 'sass-loader', options: {
              // here
          }
      ]
    }
  }
}

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