I'm trying to set up a modern vanilla web starter with Webpack.
I got stuck when I added webpack-html-loader
and html-loader
. Here's what happens...
img
tag in the entry index.html
file, with an src
attribute like this (./imgs/image.png
), the src
attribute in the dist
folder is replaced with [object Module]
. But when I remove the dot before the uri
(./imgs/image.png
to /imgs/image.png
), the src
output in the dist
folder is exactly the same as that of the src
. This doesn't reference the image I want to include.src
value of the webpack output is exactly the same as the source, I try to be crude and use the uri
to the image in the output folder like so /dist/img/image.png
. This works, but it's not a great experience. I would love to use a uri
to my image in the src
folder and have webpack replace it with dist
at build time. Is this possible?Here's what my file structure looks like after npm run build
- dist
- imgs
- image.png
- index.html
- main.js
- node_modules
- src
- index.html
- imgs
- image.png
- package.json
- package-lock.json
- webpack.config.js
Here's my webpack.config.js
const HTMLWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const path = require("path");
module.exports = {
module: {
rules: [
{
test: /\.(html)$/,
use: [
{
loader: "html-loader",
options: {
minimize: false
}
}
]
},
{
test: /\.(png|jpe?g)$/,
use: [
{
loader: "file-loader",
options: {
outputPath: "imgs",
publicPath: "imgs",
name: "[name].[ext]"
}
}
]
}
]
},
plugins: [
new HTMLWebpackPlugin({
template: path.resolve(__dirname, "src/index.html")
}),
new CleanWebpackPlugin()
]
};
I made a repo of it here
Thanks a lot for your time
Update (02-01-2020)
Someone in the comment pointed out a solution to the problem. (When i using file-loader and html-loader in webpack. The src attr of image gonna be like '[object Module]')
The solution is to set the esModules
object to false
in the file-loader
rule like so
{
test: /\.(png|jpe?g)$/,
use: [
{
loader: "file-loader",
options: {
// Here!!!
esModule: false,
outputPath: "images",
publicPath: "images",
name: "[name].[ext]"
}
}
]
}
The top-level output key contains a set of options instructing webpack on how and where it should output your bundles, assets, and anything else you bundle or load with webpack.
This is according to their documentation: "libraryTarget: "umd" - This exposes your library under all the module definitions, allowing it to work with CommonJS, AMD and as global variable." Also, I built the exact same code with Webpack 3 and it produced a proper bundle.
Essentially, there is not much in Webpack to include your desired images for your web application. First, put your image files into one folder of your projects application. For instance, your src/ folder may have a folder assets/ which has a folder images/.
Webpack will generate the files and put them in the /dist folder for you, but it doesn't keep track of which files are actually in use by your project. In general it's good practice to clean the /dist folder before each build, so that only used files will be generated.
Please, take a look at this post:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With