I have the following src folder structure:
/src
+-- /app
| +-- index.js
| +-- /templates
| +-- index.html
| +-- /subfolder
| +-- nested.html
|
+-- /images
| +-- webpack.png
|
+-- /styles
| +-- index.scss
I am trying to configure webpack to emit multiple html files using templates from app folder. The final build folder structure should look something like this:
/build
+-- /images
| +-- webpack.png
|
+-- /subfolder
| +-- nested.html
|
+-- index.html
+-- bundle.js
+-- styles.css
The problem is that after building, index.html
and nested.html
have the same path to the image <img src="img/webpack.png">
which is incorrect for nested.html
nested.html
should have the following path - <img src="../img/webpack.png">
.
How can I make webpack set correct path to assets for nested html files ?
Steps to reproduce.
- Download the repo
- Install packages
npm install
- Run
npm run build
According to this thread it is not a problem with html-loader - https://github.com/webpack-contrib/file-loader/issues/272
Webpack docs mention assets sharing between components but provides no examples - https://webpack.js.org/guides/asset-management/#global-assets
html-loader options may have a solution to this but docs doesn't make much sense to me and I can't figure it out - https://webpack.js.org/guides/asset-management/#global-assets
Article I was following to generate htmlWebpackPlugin dynamically - https://extri.co/2017/07/11/generating-multiple-html-pages-with-htmlwebpackplugin/