简体   繁体   中英

Assets only correctly map up when my application isn't hosted on a pre-existing path

I'm developing an application for use on multiple client sites. These are hosted either as a subdomain or as a path (which I have no control over), like so:

  1. https://application.example.com/#/example-page
  2. https://example.com/application/#/example-page

Here is my Webpack 1 config:

module: {
  loaders: [
    {
      ...
    },
    {
      test: /\.(woff2?|eot|svg|ttf|md|jpg|png)$/,
      loader: 'file?name=[hash].[ext]'
    }
  ]
},
output: {
  path: __dirname + "/dist/build/",
  filename: "app.min.js"
}

With this, my assets get compiled into a /build/ folder which also contains the main application JavaScript file:

我的dist / build文件夹

The issue I'm having is that the compiled assets aren't found if the application is hosted on a pre-existing path (URL example 2 above) but load perfectly fine if no path exists. Doing some debugging reveals that for whatever reason the /build directory must be specified for assets to load on the second URL example, but specifying /build breaks the first URL example:

  1. https://application.example.com/compiled-asset.png
  2. https://example.com/application/compiled-asset.png ⇐ 404
  3. https://application.example.com/build/compiled-asset.png ⇐ 404
  4. https://example.com/application/build/compiled-asset.png

What am I doing wrong here?

Seems the answer to this is to add the publicPath property to the loader itself as following:

loader: 'file-loader?name=[hash].[ext]&publicPath=./build/'

I had previously attempted this with a separate publicPath property, but this didn't achieve anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM