colbyfayock / html-webpack-partials-plugin

🛠 Easy HTML partials for Webpack without a custom index!
MIT License
68 stars 20 forks source link

Partials break when you add data-attributes #30

Closed sudo-vaibhav closed 3 years ago

sudo-vaibhav commented 3 years ago

I love using this piece of software. It saves me loads of time. Amazing work! I recently noticed this curious little behaviour recently. I was using the same navbar partial across multiple pages. But in one of the many pages, I had to add a data-attribute to the body tag like this:

<body data-spy="scroll">

And this broke the partial. The navbar shows up just fine on every other page which does not have data attributes. Here's how I was inserting the navbar partial:

new HtmlWebpackPartialsPlugin({ path: path.join(__dirname, "./src/partials/navbar/navbar.html"), location: "body", priority: "high", template_filename: [ "fileWithDataAttributeInBodyTag.html", // 1 "fileWithoutDataAttributeInBodyTag.html", // 2 ] })

The partial works just fine on the second file but doesn't even get inserted in the DOM for the first file.

colbyfayock commented 3 years ago

hey @sudo-vaibhav my guess is that the regex isn't liking that, i dont think I wrote it to handle attributes which i didn't think of at the time

https://github.com/colbyfayock/html-webpack-partials-plugin/blob/master/lib/util.js

I'll try to look into getting a fix in for this unless you would like to make a PR, feel free :)

i'm thinking we'll want a new rule in the injectPartial function

colbyfayock commented 3 years ago

hey @sudo-vaibhav i just pushed out a commit and new release that i think addresses this for oyu

https://github.com/colbyfayock/html-webpack-partials-plugin/commit/610461181d19cba54a19b643b744703e31737318

its in 0.7.0

want to give it a shot and see if it works?

sudo-vaibhav commented 3 years ago

Thanks! I will try it out.

sudo-vaibhav commented 3 years ago

It would be great if we could just specify the CSS selector of the tag we wanna insert the partial in. for example I have a blog signup section that I wanna reuse in multiple pages. But all of those pages have many section tags. My hack for making it work was putting a partial in the aside tag and another partial in the article tag but this way I will quickly run out of tags. If we can develop a way of just targeting DOM elements by their CSS selector(more preferable) or by XPATH, it would highly increase the scope of this plugin.

colbyfayock commented 3 years ago

i think that's a great idea

would you mind creating a separate issue with a feature request? that way we can leave this issue for the bug i fixed (also did you confirm it fixed it?)

if you're up for the work, i'd love for you to give it a shot. i have a few things going on so im not sure how quickly i can get in and do it, but definitely interested!

sudo-vaibhav commented 3 years ago

no I checked and its not working... at least its not working with my use case. To be precise, my body tag is this:

<body data-spy="scroll" data-target="#list-example" cz-shortcut-listen="true">

The cz-shortcut-listen is a chrome extension specific thing (colorzilla) and we can ignore it since it gets inserted once the browser is done loading the page. But even with the new changes it does not work for data-spy and data-target when I use them together which is necessary for my use case. That's how bootstrap scrollspy works.

sudo-vaibhav commented 3 years ago

As for the CSS selector thing, I will open a new issue. I would be glad to help you in doing it but I have never contributed to open source projects before so I am not sure how to exactly go about it. Do I need to follow some guidelines? I will try studying your code.

sudo-vaibhav commented 3 years ago

wait it works! The problem last time was that the version of the plugin did not get updated. When I uninstalled the plugin and installed it again. It started working. The version 0.7.0 fixes my issue. Thanks!

colbyfayock commented 3 years ago

oh great news about the fix! ill follow up on the other Issue :)