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

Can't use variables in static tag (dynamic assets) #87

Closed
alsolovyev opened this issue Jul 23, 2020 · 8 comments · Fixed by #90 or #117
Closed

Can't use variables in static tag (dynamic assets) #87

alsolovyev opened this issue Jul 23, 2020 · 8 comments · Fixed by #90 or #117
Assignees
Labels
bug Something isn't working pinned Go away stale bot

Comments

@alsolovyev
Copy link

Hi, first of all, I would like to thank you. Nice repo!
Is this a bug or am I doing something wrong?

Can't use variables in static tag (dynamic assets)

{% for image in images %}
  <img src="{% static image %}">
{% endfor %}
Error: Child compilation failed:
  Module build failed (from ../node_modules/simple-nunjucks-loader/lib/cjs/loader.js):
  Error: Asset "image" not found

or

{% for image in images %}
  <img src="{% static './' + image %}">
{% endfor %}
Error: Expected html to be a string but got null
  
  - index.js:417 HtmlWebpackPlugin.postProcessHtml
    [website]/[html-webpack-plugin]/index.js:417:29
  
  - index.js:247 
    [website]/[html-webpack-plugin]/index.js:247:25
  
  - task_queues.js:97 processTicksAndRejections
    internal/process/task_queues.js:97:5

Expected behavior

We have a list of images:

new HTMLWebpackPlugin({
    template: 'template.njk',
    templateParameters: {
        images: ['first.jpg', 'second.jpg', 'third.jpg']
    }
})

And I want to use them in the template:

{% for image in images %}
  <img src="{% static image %}">
{% endfor %}

Config

  • Windows 10
  • NodeJS - 12.16.3
  • file-loader - 6.0.0
  • nunjucks - 3.2.2
  • simple-nunjucks-loader - 2.0.2

Plugin

new HTMLWebpackPlugin({
    template: 'template.njk',
    templateParameters: {
        images
    }
})

Loader

{
  test: /\.(njk|nunjucks|html)$/i,
  include: src,
  use: [
    'cache-loader',
    {
      loader: 'thread-loader',
      options: { workerParallelJobs: 100, poolRespawn: false, name: 'nunjucks' }
    },
    {
      loader: 'simple-nunjucks-loader',
      options: {
        searchPaths: ['src'],
        assetsPaths: ['src/images']
      }
    }
  ]
},
{
  test: /\.(png|jpe?g|gif)$/i,
  use: ['file-loader']
}
@alsolovyev alsolovyev added the bug Something isn't working label Jul 23, 2020
@ogonkov
Copy link
Owner

ogonkov commented Jul 23, 2020

Thank you for filling a bug!

Does it work when static part got some string in it? Like

{% static 'asssets/' + image %}

@alsolovyev
Copy link
Author

alsolovyev commented Jul 23, 2020

That was my second thought:

{% for image in images %}
  <img src="{% static './' + image %}">
{% endfor %}
Error: Expected html to be a string but got null

and tried to put images in a subfolder:

{% for image in ['first.jpg', 'second.jpg'] %}
  <img src="{% static 'images/' + image %}">
{% endfor %}
Error: Cannot find module './imagesfirst.jpg'

or

{% for image in ['/first.jpg', '/second.jpg'] %}
  <img src="{% static 'images/' + image %}">
{% endfor %}
Error: Cannot find module './images/first.jpg'

I can create a repository to reproduce this error if needed.

@ogonkov
Copy link
Owner

ogonkov commented Jul 23, 2020

That weird. I will debug it after 19.00 (Moscow time)

Looks like there bugs in both AST parsing and StaticExtension.

Can you create repo to reproduce both cases?

@alsolovyev
Copy link
Author

alsolovyev commented Jul 23, 2020

Can you create repo to reproduce both cases?

Of course, want to help as much as I can
https://github.com/alsolovyev/nunjucks-loader-error

@ogonkov
Copy link
Owner

ogonkov commented Jul 23, 2020

I'm afraid loader lack of support for "dynamic-only" assets. I need to change assets output format and the way i examine that it is actually dynamic and not a static path. I guess i will make path on weekend.

I will fix a trailing slash problem for now (Cannot find module './imagesfirst.jpg'), i miss that case when write tests for assets.

@ogonkov ogonkov self-assigned this Jul 24, 2020
@ogonkov
Copy link
Owner

ogonkov commented Jul 24, 2020

My tests didn't catch trailing slash problem. Patch for problem is ready, but I should to reproduce it in tests first. I will continue with patch today, or on the weekend.

@alsolovyev
Copy link
Author

Great job, thank you so much for such a quick response!

@ogonkov
Copy link
Owner

ogonkov commented Jul 27, 2020

I will reopen this issue because i fix only one problem.

@ogonkov ogonkov reopened this Jul 27, 2020
@ogonkov ogonkov pinned this issue Jul 27, 2020
@ogonkov ogonkov added the pinned Go away stale bot label Nov 24, 2020
@ogonkov ogonkov unpinned this issue Jul 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pinned Go away stale bot
Projects
None yet
2 participants