前端 - 关于webpack项目打包上线后的静态资源路径出错问题

查看:552
本文介绍了前端 - 关于webpack项目打包上线后的静态资源路径出错问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

项目构建完成后,我准备将项目打包上传至静态服务器,线上路径为:/htdocs/test,test为我的项目根目录。但当我上传后,项目所有的图片路径包括外部link标签插入的js库等等,都出现了路径错误。

正确的路径应为:test2.cdgzy.com/test/dist/assets/images/6.png?381dee4ca0a1f99eb852be0cc5403202
但目前的路径却为:test2.cdgzy.com/dist/assets/images/6.png?381dee4ca0a1f99eb852be0cc5403202。

路径中始终缺失根目录。调了半天也没调好。不知有人知道是否是什么原因?以下为我的webpack.config.js的配置:

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

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
          }
        }
      },
      {
              test: /\.css$/,
        loader: 'style-loader!css-loader'
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: 'assets/images/[name].[ext]?[hash]'
        }
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true,
    host: '0.0.0.0',
    port: 8080
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  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
    })
  ])
}

解决方案

output中的publicPath 是不是应该加上test 这层目录?

这篇关于前端 - 关于webpack项目打包上线后的静态资源路径出错问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆