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

[eleventy-plugin-compress] Empty brotli file #42

Closed
eitchtee opened this issue Apr 5, 2023 · 2 comments
Closed

[eleventy-plugin-compress] Empty brotli file #42

eitchtee opened this issue Apr 5, 2023 · 2 comments

Comments

@eitchtee
Copy link

eitchtee commented Apr 5, 2023

Hi there,

I've stumbled upon a very weird bug.

In my setup I have a .njk file that includes a JS script so I'm able to minify these files within Eleventy using a transform, without needing other tools

---
permalink: /js/testing.js
---
{% include "scripts/testing.js" %}

And these javascript files are them added to my layout based on a condition set on the front matter

{% if enable.testing %}
  <script src="/js/testing.js"></script>
{% endif %}
---
layout: "default"
enable:
    testing: true

permalink: /bug/
---

I've not been able to pinpoint exactly what causes the problem, but when building my site I get a broken brotli file with ; as it's only content.

I've setup a minimal example to help debug.
demo.zip


A few things worth noting:

  • A second build results in a working file (probably because the js files already exists on the public folder)
  • Adding the script conditionally or not, results in the same error
  • On the demo I get an error: Error: ENOENT: no such file or directory, open '[.....]\demo\public\js\testing.js' 💥 on the first run, but the plugin is already the last thing on the .eleventy.js file.
  • On my real project I have a bunch of other scripts setup the same way that somehow get compressed just fine, the problem only occurs on one script, although it doesn't seem to be related to the contents of the file.
@Olezhka-web
Copy link
Contributor

Hello @eitchtee . Thank you for contacting us.

The problem is that you create the js script through eleventy, and eleventy writes the files after all the plugins have worked.

That is, the situation arises, the first time you build, the file has not yet been created and an error occurs. And for the second build, the file will already be there - that's why there is no error for the second build. Accordingly, in relation to this information, the content will also be recorded incorrectly.

Do you really need to create a js file through njk as an eleventy template ?

If you are not critical, then you can not use templates for generating files that will be compressed yet.

@eitchtee
Copy link
Author

eitchtee commented Apr 6, 2023

I suppose this is a precedence problem, something like the layout that uses the js file is built, the plugins tries to compress the scripts mentioned in this layout, which doesn't exist at the time, and them 11ty builds the js file from the template (too late now), right?

The hack for building the js files from a njk template came from here 11ty/eleventy#344 (comment), as at the time I needed a way for 11ty to process the files for a custom transform that minified it, as a PassthroughCopy wouldn't allow me to do it. After some research I found a way of doing it within the addPassthroughCopy function.

  eleventyConfig.addPassthroughCopy(
    "./src/js/*.js",
    {
      transform: (src, dest, stats) => {
        return new Transform({
          transform(chunk, enc, done) {
            done(null, minjs(chunk.toString()));
          }, }); }, } );

Which, at least from my tests, seems to have completely fixed the problem.


Not sure if possible, but perhaps the plugin could attach to eleventy.after, running only after everything is properly built. Food for thought.

Thank you very much for taking the time to reply and great work!

@eitchtee eitchtee closed this as completed Apr 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants