如何配置webpack来处理SCSS文件中的图像路径

时间:2017-11-29 19:50:08

标签: javascript webpack vue.js urlloader vue-loader

我有一个使用Vue CLI和webpack-simple模板的项目。我正在使用我正在处理的提供sass文件的NPM包。一些sass文件在NPM项目中有图像的url。 以下是NPM包中样式的示例。

background: url("../../../assets/icons/icon-close-blue.svg") no-repeat center;

图像icon-close-blue.svg位于相对于SCSS文件的NPM包中。

我正在将scss文件拉到我的项目中

@import "~@chewy/design-system/src/design-system.scss";

当我用纱线启动开发服务器时,我收到此错误。

./node_modules/extract-text-webpack-plugin/dist/loader.js?
{"omit":1,"remove":true}!./node_modules/vue-style-
loader!./node_modules/css-loader!./node_modules/vue-loader/lib/style-
compiler?{"vue":true,"id":"data-v-
7ba5bd90","scoped":false,"hasInlineConfig":false}!./node_modules/sass-
loader/lib/loader.js!./node_modules/vue-loader/lib/selector.js?
type=styles&index=0&bustCache!./src/App.vue
Module build failed: ModuleNotFoundError: Module not found: Error: 
Can't resolve '../../../assets/icons/checkmark-green.svg' in 
'/Users/dsteinberg/Code/chewy-design-platform/src'

这是我的webpack配置

var path = require('path')
var webpack = require('webpack')
var ExtractTextPlugin = require("extract-text-webpack-plugin")

module.exports = {
  entry: {
    src: './src/main.js',
    pages: './src/pages.js'
  },
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: 'http://localhost:8080/dist/',
    filename: '[name].build.js',
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ],
      },
      {
        test: /\.scss$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader'
        ],
      },
      {
        test: /\.sass$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader?indentedSyntax'
        ],
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            scss: ExtractTextPlugin.extract({
              use: ['css-loader', 'sass-loader'],
              fallback: 'vue-style-loader'
            }),
            'sass': [
              'vue-style-loader',
              'css-loader',
              'sass-loader?indentedSyntax'
            ]
          }
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      },
      {
        test: /\.(html)$/,
        use: {
          loader: 'html-loader',
          options: {
            attrs: [':data-src']
          }
        }
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: '/static/img/[name].[hash:7].[ext]'
        }
      },
      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: '/static/media/[name].[hash:7].[ext]'
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: '/static/fonts/[name].[hash:7].[ext]'
        }
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    },
    extensions: ['*', '.js', '.vue', '.json']
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true,
    overlay: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map',
  plugins: [
    new ExtractTextPlugin("[name].style.css")
  ]
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

Here is a screenshot of the NPM folder structure and the scss file with the url.

我能做些什么来让webpack处理这些图像路径吗?

0 个答案:

没有答案