chocolateboy / parcel-plugin-nunjucks

Parcel support for nunjucks templates

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Content inside raw/verbatim block is still processed by PostHTML

gterras opened this issue · comments

Hi,

I noticed HTML content between raw blocks are still processed by PostHTML. So this :

{% raw %}
<img src="{{ data.image }}" >
{% endraw %}

will trigger a build error {{ data.image }}: ENOENT: no such file or directory

I don't know if this issue is out of scope (as writing an html string directly inside Nunjucks tags would work), do you consider this expected behavior ?

Thank you

Yes, it's the expected behavior (in Parcel) for HTML assets to be processed by PostHTML. It doesn't appear to provide a way to disable this.

However, it makes sense to allow further processing to be disabled in parcel-plugin-nunjucks, which could be done by using an njk asset-type, either via the filename, e.g. index.njk.njk, or via the assetType option, e.g.:

$ cat index.txt.njk
<html>
    <body>
        <h1>Hello, {{ name }}!</h1>

        {% raw %}
            <img src="{{ data.image }}" />
        {% endraw %}

        <a href="./foo.html">Foo</a>
    </body>
</html>
$ cat nunjucks.config.js
module.exports = {
    data: { name: 'world' },
    assetType (path) {
        return path.baseExt === '.txt'
            ? 'njk'  // stop after nunjucks processing (plain text)
            : false  // default asset-type i.e. determined by filename
    }
}
$ cat dist/index.txt.njk
<html>
    <body>
        <h1>Hello, world!</h1>
        <img src="{{ data.image }}" />
        <a href="./foo.html">Foo</a>
    </body>
</html>

Note: if you only want to protect a specific section from processing by PostHTML, you'll need to use whatever the equivalent of the raw/verbatim tag is in PostHTML, or request it (probably here).

Hi,

thanks for your response. Indeed this is more a parcel/postml issue. Apparently parcel2 will provide some ways to do so. I will try to write a plugin for this.

By the way it seems that your example triggers a cannot resolves entry "/my/path/index.txt.njk" from "/my/path" . Changing the "root" option does not seem to change this behavior.

The example is not what was implemented. See the PR and the documentation for the final implementation.

Yes I've seen that afterwards, thanks for doing this.

FYI I've opened a Parcel issue for my specific case (src attributes easier control) here : parcel-bundler/parcel#3348