-
-
Notifications
You must be signed in to change notification settings - Fork 205
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
Including html partials within html partials without the interpolate option #291
Comments
I ended up reverting to 0.5.5, which works without any problems. I'm unsure why this breaking change was included without a way to replace it? |
Can you provide example how you want to include? You have JS, so you can include anything |
Including already works:
The problem is that without the Why not bring back how it used to work?:
If I include from JS it will be added client side, which is of course not great for SEO. I need Webpack to build all my files into a single component. |
It is out of scope HTML spec, HTML doesn't support |
https://github.com/webpack-contrib/html-loader#preprocessor
|
The
Which are not out of scope |
We have examples for
No |
It is just luck that you did not receive an error while parsing. |
I strongly recommend migrate on a template system, using |
Would it be a problem if I spun The demand is there and people clearly prefer using this rather than learning some other templating system. It's also the suggested way by webpack-html-plugin to load partial HTML files, so it seems a bit closeminded to say
As far as I see it there's not really enough information on how to use preprocessor. It would probably serve the function I'm asking fairly well, it's just unclear how I could set preprocessor to use a normal loader rather than another library to process my files. On top of that the changelog states:
But doesn't offer much explanation on how to do so. |
Yep, you can do it temporary, but I would advise speed up migration on a template system
We should remove it from examples
Yep, we can improve docs about it.
I provide examples for |
The crucial part for me to have the included file processed by webpack again before being included. I can include the file with PostHTML, but it only loads the raw file. If there is a way to achieve this could you please explain how? |
What do you mean? |
I set up posthtml and posthtml-include created a file file1.html with includes Including the old way processed the content of file2. |
@philippdieter Maybe you can provide small example of configuration? I want to make sure that I understand you correctly |
I made the mistake and mixed up the endings of my files in the post above, I have updated them there, I am sorry. I build an example located at The resulting output file is The output possible with include was |
@philippdieter Thanks, I will look at this tomorrow |
Yes, the same problem, here other problems most if loaders for HTML preprocessing returns JS content instead HTML, it's a little wrong, for example postcss-loader return CSS content, not JS, so css-loader + postcss-loader work fine |
Ideally In theory we can add some magic to <div>My Custom Text</div>
<link rel="webpack-import" href="./index.html" />
<div>My Custom Text</div> Will be compiled to: import __HTML_LOADER_LINK_IMPORT_1__ from './index.html';
export `<div>My Custom Text</div>` +__HTML_LOADER_LINK_IMPORT_1__ + '<div>My Custom Text</div>'; And extracted to <div>My Custom Text</div>
<div><img alt="Image" src="./path/to/img.png"><span>Foo</span></div>
<div>My Custom Text</div> It is HTML compatibility, no problems with |
I would like to hear your opinion. |
Other good idea WICG/webcomponents#645 |
JS has imports/exports, CSS only imports, HTML has not imports/exports 😄 |
Just to be sure you looked at the right module: In my example it's pug-plain-loder, not pug-loader. I remember vaguely about having used this loader because it returns html(?). I like the idea of the link tag very much. As far as I understand WICG/webcomponents#645 it's about using includes on client side, right? |
Yes, I mean
Yes 👍 I would not want to make this part as a separate option or an official solution, as this is still not standardized. But we can do something small and simple. We just need improve the
You can:
|
Having the option to specify a tag even sounds better than having a fixed one. I like this idea very much! |
With v2 we can implement using |
Hi, i tried all solutions here and nothing: One:
Two:
Three:
Four:
Five (it include the file but images inside header.html not show):
Package.json:
Webpack (i tried all options on thread):
How can i make it work? |
i downgrade to "html-loader": "^0.5.5" and it works fine for me |
webpack-import pull了暂时没有发布 |
Hi @laozei6401, I tried:
And my webpack .js
And my directory is:
|
|
I changed index.html and partials/header.html to "ejs". The file was included, but the images are not showed:
And at last i need use "http://localhost:8000/index.ejs" and not "http://localhost:8000/index.html". There is any way to change it? |
It was published? |
No, it is WIP |
Same here! Did you have any luck figuring this out? |
In our roadmap, we are working on new release css-loader + style-loader/mini-css-extract-plugin, then we improve it |
My case too. I don't understand how to use webpack to embed SVG in HTML templates. Webpack documentation does not provide a clear answer. |
Just to chime in here, some people are not looking for "templating" as such. Sometimes i have projects which have tons of content and the HTML file ends up being huge and hard to maintain. This interpolate option really helped me "break" the content down into smaller manageable html chunks and was a really really nice feature. I purely used it for this functionality. I do not understand why, if you have a really good feature in your package which clearly lots of people are using and want back, youd shrug it off and tell them tough. Please consider reinstating it. Give the people what they want :D |
We heavily use this for other HTML files, SVGs and also css class names generated by the style-loader, e.g.: <div class="${require('./style.scss').myClass}">
${require('./some.html')}
${require('./some.svg')}
</div> So an upgrade for us is not possible without a replacement. Will the |
Another option to replace Added directory and root path support #291 (comment) Unfortunately index.html<include src="./path-to-root-html.html"/>
<include src="path-to-html.html"/> webpack.config.jsfunction processNestedHtml(content, loaderContext, resourcePath = "") {
let fileDir = (resourcePath === "")? path.dirname(loaderContext.resourcePath) : path.dirname(resourcePath)
const INCLUDE_PATTERN = /\<include src=\"(\.\/)?(.+)\"\/?\>(?:\<\/include\>)?/gi;
function replaceHtml(match, pathRule, src) {
if(pathRule === "./"){
fileDir = loaderContext.context
}
const filePath = path.resolve(fileDir, src)
loaderContext.dependency(filePath)
const html = fs.readFileSync(filePath, 'utf8')
return processNestedHtml(html, loaderContext, filePath)
}
if (!INCLUDE_PATTERN.test(content)) {
return content
} else {
return content.replace(INCLUDE_PATTERN, replaceHtml);
}
}
function processHtmlLoader(content, loaderContext){
let newContent = processNestedHtml(content, loaderContext)
return newContent
} {
test: /\.html$/i,
use: [
{
loader: 'html-loader',
options: {
sources: false,
minimize: false,
esModule: false,
preprocessor: processHtmlLoader
}
}],
}, |
Делюсь своим временным решением поддержки style url():
Создаем у тега атрибут "data-src" с url background-image. На выходе: Таким образом изображение обрабатывается webpack. Уже можно было бы закончить, но если вас как и меня не устраивает лишний data атрибут, то можно воспользоваться плагином
Итог: Этот вариант не будет работать с изображениями которые имеют хеш в имени. |
I'm currently using something like this:
Unfortunately, using |
Thanks, I got the idea. I'll try as the time will be. It is better than inserting svg content manually into the code. |
Our case is using Aliases for easier management of html files.
in dozens of our html files which is kinda interpolating and was working fine until 0.5.5. Is there any easy workaround to keep this structure in our code without the need to modify hundreds of lines in our html files ? |
Try this
|
I solved it... at least for me and my pug-related problem from the start of this thread. Or maybe you will call it a workaround or a hack. Whatever, I found a setup which is working fine for me by throwing ejs-loader-compiled into the mix, maybe somebody will get some use out of it too. I published my stack testing environment at: To be able to process plain pug files the ejs-loader-compiled could be simple configured in the webpack.config.js. This was not working for pug in vue single-file components as the vue-loader expects plain html in the component files and ejs-loader-compiler returns module code. This is why I put together https://github.com/cjel/ejs-pug-vue-resource-loader/ , which just calls other loaders beforehand. An example how to include a file and pass a parameter can be found at: If there are any questions please let me know. |
Now you can do it using https://github.com/webpack-contrib/html-loader?tab=readme-ov-file#postprocessor |
Just simple function so you can implement any logic |
Documentation Is:
Please Explain in Detail...
At some point in the past html-loader had an
interpolate
option. This allowed to load partials within partials in templates. As far as understand it this recently got deprecated and replaced withpreprocessor
.So, answers like this one aren't working anymore.
A couple of questions like this one asking how to achieve it popped up, but they've gone without answers.
The documentation seems unclear as if this is still possible as the only example mentions handlebars and only shows this implementation within the webpack config.
Your Proposal for Changes
Is there any chance an official explanation of how to achieve this could be provided in the docs or even in this thread? I've tried everything possible and I'm absolutely lost.
The text was updated successfully, but these errors were encountered: