产品生产(webpack)上的公共资产为404

时间:2019-09-28 19:11:46

标签: javascript webpack assets production-environment pixi.js

我对Webpack不太熟悉,并且遇到了无法访问产品上的资产的问题-它返回404

我有这样的结构:

- repo
 - public
   - index.html
   - images
     - animals
       - dog.png
   - audio
     - growl.mp3

  - src
    - index.js
    ... other files

现在在我的index.js(或其他文件)上,我可以通过以下方式访问图像:

import { Loader, utils, Sprite, AnimatedSprite } from 'pixi.js'

loader
    .add('images/animals/dog.png' )
    .load(async () => {})
... other codes

这在本地运行良好,但是当我在生产环境中部署时会抛出

Failed to load resource: the server responded with a status of 404 () - /images/animals/dog.png:1 Failed to load resource: the server responded with

我的webpack看起来像这样:

const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const CircularDependencyPlugin = require('circular-dependency-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')

const isProd = process.env.NODE_ENV === 'production'

module.exports = {
  mode: isProd ? 'production' : 'development',
  entry: './src/index.js',
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'build'),
    publicPath: '/',
  },
  resolve: {
    modules: [path.resolve(__dirname, 'src'), 'node_modules'],
  },
  devtool: isProd ? 'cheap-module-eval-source-map' : 'source-map',
  devServer: {
    hot: true,
    watchContentBase: true,
    contentBase: [
      path.resolve(__dirname, 'public'),
      path.resolve(__dirname, 'build'),
    ],
    publicPath: '/',
  },
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'],
          },
        },
      },
    ],
  },
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      template: 'public/index.html',
    }),
    new CircularDependencyPlugin({
      exclude: /a\.js|node_modules/,
      failOnError: false,
      cwd: process.cwd(),
    }),
    new CopyWebpackPlugin([
      { from: 'public/audio', to: 'audio' },
      { from: 'public/images', to: 'images' },
    ]),
  ],
}

1 个答案:

答案 0 :(得分:0)

好,所以我知道了。

事实证明,我只需要将所有publicPath''(空字符串)