简体   繁体   English

如何通过vue-cli 3配置webpack使用sass

[英]How to configure webpack via vue-cli 3 to use sass

I'm building a vue app with vue-cli 3. I'd like to include sass files in my webpack build but the vue-cli abstracts away the webpack.config.js file.我正在使用 vue-cli 3 构建一个 vue 应用程序。我想在我的 webpack 构建中包含 sass 文件,但 vue-cli 抽象出webpack.config.js文件。 Instead we are to create a vue.config.js file and configure it with a set of instructions.相反,我们将创建一个vue.config.js文件并使用一组指令对其进行配置。

If I were configuring webpack.config.js , the following would work:如果我正在配置webpack.config.js ,则以下内容将起作用:

module.exports = {
  module: {
    rules: [
      // ... other rules omitted

      // this will apply to both plain `.scss` files
      // AND `<style lang="scss">` blocks in `.vue` files
      {
        test: /\.scss$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader'
        ]
      }
    ]
  },
  // plugin omitted
}

How should I configure my vue.config.js to achieve the same?我应该如何配置我的vue.config.js来实现同样的目标? Here is my best attempt:这是我最好的尝试:

module.exports = {
    chainWebpack: config => {
        config.module
            .rule('scss')
            .test(/\.scss$/)
            .use('vue-style-loader')
            .loader('vue-style-loader')
            .use('css-loader')
            .loader('css-loader')
            .use('sass-loader')
            .loader('sass-loader')
            .end();
    }
};

This fails with the message这失败并显示消息

ERROR  TypeError: config.module.rule(...).test(...).use(...).loader(...).use is not a function

If you are using vue cli 3 then just run the following in your project root directory:如果您使用的是 vue cli 3,那么只需在您的项目根目录中运行以下命令:

vue add style-resources-loader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM