Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process images in .hbs #54

Open
brightpixels opened this issue Jul 19, 2019 · 3 comments
Open

Process images in .hbs #54

brightpixels opened this issue Jul 19, 2019 · 3 comments
Labels

Comments

@brightpixels
Copy link

Hello,
I need to compile hbs files to html at build time which is plugin is doing. However, it doesn't process the images. Anyway to ensure webpack processes the images founds in the handlebars files? Using the loader version of this plugin isn't an option as I need the hbs to be compiled at build time (this is a similar question to mine #23).

@sagold
Copy link
Owner

sagold commented Aug 1, 2019

Hi brightpixels.

Sorry, im short in spare time right now. Just as a side note: the mentioned loader does compile the hbs-files on build time, too.

@brightpixels
Copy link
Author

Thanks. I didn't realise the other one works at build time as well!

@sagold sagold added the question label Sep 8, 2019
@jakeols
Copy link

jakeols commented Sep 23, 2020

If anyone else is struggling with this, it's pretty straightforward with node-html-parser
Ex:

      onBeforeSave: function (Handlebars, html, f) {
        const root = htmlParser.parse(html);
        let img = root.querySelectorAll("img");

        img.forEach((img) => {
          const str = img.rawAttrs;
          const regex = /src=['"](.*?)['"]/;
          const imgVal = regex.exec(str)[1];

          const src = paths.src + "/" + imgVal.replace(/\.\.\//g, "");
          const tgt = module.exports.output.path + "/" + imgVal.replace(/\.\.\//g, "");
          fs.copyFileSync(src, tgt);
        });
        return html;
      },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants