11ty / eleventy

A simpler site generator. Transforms a directory of templates (of varying types) into HTML.
https://www.11ty.dev/
MIT License
17.33k stars 495 forks source link

provided example for skipping templates in "compile" function only works with canary #2724

Closed w0whitaker closed 1 year ago

w0whitaker commented 1 year ago

As written, the example for skipping templates doesn't work in v1.0.2.

Given the following:

// "compile" function within "eleventy.config.addExtension()".

compile: async function (inputContent, inputPath) {
    let parsed = path.parse(inputPath);

    if (parsed.name.startsWith("_")) {
        return;
    }

    let result = sass.compileString(inputContent, {
        loadPaths: [parsed.dir || "."],
    });

    return (data) => {
        return result.css;
    };
}

Running npm start results in:

> npx @11ty/eleventy --serve

[11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] 1. Having trouble rendering scss template ./src/assets/scss/_text.scss (via TemplateContentRenderError)
[11ty] 2. Having trouble compiling template ./src/assets/scss/_text.scss (via TemplateContentCompileError)
[11ty] 3. Cannot read properties of (reading 'bind') (via TypeError)
[11ty]
[11ty] Original error stack trace: TypeError: Cannot read properties of undefined (reading 'bind')
[11ty]     at /Users/william/development/elev-test/node_modules/@11ty/eleventy/src/Engines/Custom.js:173:35
[11ty] Wrote 0 files in 0.08 seconds (v1.0.2)

It does, however, work if using v2.0.0-canary.23:

> npx @11ty/eleventy --serve

[11ty] Writing _site/assets/scss/main.css from ./src/assets/scss/main.scss
[11ty] Writing _site/index.html from ./src/index.md (liquid)
[11ty] Wrote 2 files in 0.08 seconds (v2.0.0-canary.23)
[11ty] Watching…
[11ty] Server at http://localhost:8080/

It will also work in v1.0.2 when the if statement is moved into the return block:

compile: async function (inputContent, inputPath) {
    return (data) => {
            let parsed = path.parse(inputPath);

        if (parsed.name.startsWith("_")) {
        return;
       }

        let result = sass.compileString(inputContent, {
        loadPaths: [parsed.dir || "."],
        });

        return result.css;
    };
}

I'm not sure what changed between the two versions that would cause this, but perhaps the docs should mention the change in behavior. Happy to work on this if that would be helpful.

zachleat commented 1 year ago

You’re absolutely right.

I believe this is a duplicate of https://github.com/11ty/eleventy/issues/2350 and as we’re very close to 2.0 it is unlikely to be backported unless I hear a few more noisemakers on it. I’d recommend leaving a comment on #2350 so we can organize the 1.x backport upvotes