Skip to content

How to resolve an asset path when using HTML template in JavaScript #19

Closed Answered by webdiscus
hqw567 asked this question in Q&A
Discussion options

You must be logged in to vote

Hello @hqw567,

to resolve the source file in JavaScript use the require or import:

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 publicPath as "/" or "" (undefined or auto does not work):

module.exports = {
  output: {
    publicPath: '', // <== very important when using resolving of files in JavaScript
  }
  module: {
    r…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by hqw567
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested
2 participants
Converted from issue

This discussion was converted from issue #18 on June 21, 2023 09:01.