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

Addition of IMG tag processing #23

Closed
yerzhant opened this issue Nov 15, 2017 · 2 comments
Closed

Addition of IMG tag processing #23

yerzhant opened this issue Nov 15, 2017 · 2 comments
Labels

Comments

@yerzhant
Copy link

Could you please add processing of img tags in templates, so that they appear in the dist and be watchable by webpack dev server.

Thanks in advance.

@sagold
Copy link
Owner

sagold commented Nov 16, 2017

Hi yerzhant.

You can require images from your html if you use the loader-version of this plugin, found here: https://github.com/sueddeutsche/handlebars-render-loader. Within your loader chain, prepend the html loader to the handlebars-render-loader, which will ensure that images are put into your build.

Your configuration then might look something along theese lines:

{
    test: [
        path.join(__dirname, "app", "index.hbs")
    ],
    use: [
        {
            loader: "file-loader",
            options: {
                name: "[name].html"
            }
        },
        {
            loader: "extract-loader"
        },
        {
            loader: "html-loader",
            options: LOADER_CONFIG.html
        },
        {
            loader: "handlebars-render-loader",
            options: LOADER_CONFIG.handlebarsRender
        }
    ]
}

@yerzhant
Copy link
Author

Hi Sascha,

Thank you for the reply. I've hooked to the onBeforeSave like this:

    onBeforeSave(h, html, t) {
        const images = htmlParser(html, (tag, attr) => {
            return tag === "img" && attr === "src";
        });
        images.forEach(img => {
            const src = __dirname + "/src/" + img.value.replace(/\.\.\//g, "");
            const tgt = module.exports.output.path + "/" + img.value.replace(/\.\.\//g, "");
            fs.mkdirpSync(path.dirname(tgt));
            fs.copyFileSync(src, tgt);
            handlebarsPlugin.registerGeneratedFile(tgt, fs.readFileSync(tgt));
        });
        return html;
    }

Appreciate you for your solution.

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

2 participants