Webpack为使用文件加载器包含的图像生成错误的URL

时间:2018-01-12 21:22:21

标签: image webpack vue.js

我使用webpack file-loader在我的构建中包含图像。 Webpack编译得很好并将图像从dev/assets复制到dist/images

但生成的HTML中的标记是错误的:

<img src="/dist/images/triangle.svg">

应该是:

<img src="./images/triangle.svg">

我的webpack配置文件加载器错误了吗?

.vue文件:

<div>
     <img src='./images/thing.png'>
</div>

.style {
   background: url('./images/anotherthing.png');
}

Webpack配置(简化)

module.exports = {
  entry: './src/index.ts',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]',
          outputPath: 'images/'
        }
      }
    ]
   }

webpack配置(完整)

var path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: './src/index.ts',
  output: {
    path: path.resolve(__dirname, './dist/'),
    publicPath: './',
    filename: 'js/build.js'
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            'scss': 'vue-style-loader!css-loader!sass-loader',
            'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax',
          }
        }
      },
      {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        exclude: /node_modules/,
        options: {
          appendTsSuffixTo: [/\.vue$/],
        }
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]',
          outputPath:'./images/'
        }
      },
      {
        test: /\.(json)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]',
          outputPath: './data/'
        }
      }
    ]
  },
  resolve: {
    extensions: ['.ts', '.js', '.vue', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  },
  devServer: {
    contentBase: './dist',
    historyApiFallback: true,
    noInfo: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

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.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

1 个答案:

答案 0 :(得分:0)

您可能只想设置{ "_id" : 2, "title" : "apple", "buyer_id" : 1, "buyerInfo" : [ { "_id" : 1, "name" : "abc", "age" : 20, "gender" : "M" } ] } { "_id" : 1, "title" : "banana", "buyer_id" : 2, "buyerInfo" : [ ] } >

publicPath: '/'视为资产的目录,相对于面向公众的域的根目录。如果您的服务器正在提供从publicPath/dist的文件,则意味着example.com中的资源可在/dist/images公开访问(example.com/images已添加{file-loader 1}}如你见过的那样。)