0

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:

My dist/build folder

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?

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
  • 1
    Maybe the discussion about [output.publicPath](http://webpack.github.io/docs/configuration.html#output-publicpath) will be of help. Especially the note near the end about setting it at runtime. – Nikos Paraskevopoulos Jan 04 '17 at 10:06
  • @NikosParaskevopoulos thanks, whilst you were commenting I've just fixed it using the very same property. I was specifying a publicPath property as a previous test but that achieved nothing, however in appending `&publicPath=./build/` to the `loader` it now works perfectly on both URL types. – James Donnelly Jan 04 '17 at 10:08

1 Answers1

1

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.

James Donnelly
  • 126,410
  • 34
  • 208
  • 218