frctl / fractal

A tool to help you build and document website component libraries and design systems.

Home Page:https://fractal.build

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Package/Nunjucks bug when using path filter and compiling

websaid opened this issue · comments

Based on a chat on the fractal discord here's the bug report:

This bug occurs trying to use a nunjucks macro with a parameter for an image source. If you apply the "| path" filter to the source string passed by the parameter to generate a relative path, you'll get an error on pages that call that macro. This just happens if build, on dev everything works as expected.

I have created a reproducible environment for you to test: https://github.com/Westsaid/fractalNunjucksPathBug
Inspect the following files:
image

  • /components/img-viewer/image-viewer.nunj: This is the macro where "| path" is applied to the props.src
    image
  • /components/pages/index.nunj: In here we are trying to call the macro, we pass the img src as an argument. Here is where the error occurs when build:
    image

Steps to reproduce the issue

  1. git clone https://github.com/Westsaid/fractalNunjucksPathBug
  2. npm i
  3. build using: npm run build
  4. Open the /build/index.html
  5. npm run start and open localhost:4000
  6. Open component: Pages/Index on both build and dev: The build version won't render...
  7. Expected: The build version renders like the dev version.

Error:
error.txt

Reproduces how often:
All the time.

Context

  • Fractal version: {
    "@frctl/fractal": "^1.5.2",
    "@frctl/nunjucks": "^2.0.6"
    }
  • Node version: v14.11.0
  • OS: Windows 10, (Bug also present on Mac OS)

Feel free to write on discord in the "adapter-nunjucks" channel anytime for further questions! Thank you!

@Westsaid I noticed that the image_viewer.nunj calls the macro itself: https://github.com/Westsaid/fractalNunjucksPathBug/blob/main/components/img-viewer/image-viewer.nunj#L8

In the index.nunj view context, when importing the macro, that macro call is evaluated with a undefined props object, so the path filter will get called with an undefined path, which results in the build error. If I remove the macro call in image_viewer.nunj, everything works correctly. Maybe add an if clause around that, to check if props is defined?

In this case, the difference between server/builder mode is expected, since the path filter returns early for server mode. In builder mode, it will try to determine if the path is relative/absolute etc and that throws the error, since it's not meant to be undefined.

I don't think the macro call in the component template is something Fractal is doing specifically, so I would try to see if the error persists if beind used in a non-Fractal environment (plain Nunjucks) and report it upstream if it also happens there. If it turns out it is something caused by Fractal magic, ping me and I can reopen.