0

我有一个使用带有 webpack-simple 模板的 Vue CLI 的项目。我正在引入一个我正在开发的提供 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
    })
  ])
}

这是 NPM 文件夹结构和带有 url 的 scss 文件的屏幕截图。

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

4

0 回答 0