-
Current behaviourThe relative paths of src files in HTML templates cannot be resolved correctly in JavaScript. const aiHtml = `
<img
class="w-7 h-7 sm:w-10 sm:h-10 rounded-full mr-2 sm:mr-4 shrink-0"
src="../../../assets/images/chat-ai.png"
alt="" />
`;
// or
const aiHtml = `
<img
class="w-7 h-7 sm:w-10 sm:h-10 rounded-full mr-2 sm:mr-4 shrink-0"
src="@images/chat-ai.png"
alt="" />
`; Environment
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hello @hqw567, to resolve the source file in JavaScript use the const imageFile = require('@images/chat-ai.png');
// OR
import imageFile from '@images/chat-ai.png';
console.log(imageFile); // 'assets/img/chat-ai.abcd1234.png' <= output filename of the image relative to publicPath
const aiHtml = `
<img
class="w-7 h-7 sm:w-10 sm:h-10 rounded-full mr-2 sm:mr-4 shrink-0"
src="${imageFile}"
alt="" />
`; If you will to resolve asset files in JavaScript, you must define the module.exports = {
output: {
publicPath: '', // <== very important when using resolving of files in JavaScript
}
module: {
rules: [
{
test: /\.(png|jpe?g|ico|svg)$/,
type: 'asset/resource',
generator: {
filename: 'assets/img/[name].[hash:8][ext]', // <= output filename of the image relative to publicPath
},
},
]
}
...
} |
Beta Was this translation helpful? Give feedback.
-
Hello @webdiscus , Thank you so much for helping me resolve the issue! I am truly grateful for your assistance. Your solution has been incredibly helpful, and I have successfully resolved the problem. Thank you again for your patience and expertise. 😊 |
Beta Was this translation helpful? Give feedback.
Hello @hqw567,
to resolve the source file in JavaScript use the
require
orimport
:If you will to resolve asset files in JavaScript, you must define the
publicPath
as"/"
or""
(undefined
orauto
does not work):