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

JavaScript template as class can't export any other functions #3359

Closed
pauleveritt opened this issue Jul 11, 2024 · 1 comment
Closed

JavaScript template as class can't export any other functions #3359

pauleveritt opened this issue Jul 11, 2024 · 1 comment

Comments

@pauleveritt
Copy link

Operating system

macOS Sonoma 14.5

Eleventy

3.0 alpha 16

Describe the bug

I'm writing 11ty templates that export a default class. The following works fine:

export default class IndexPage {
  render(data) {
    const name = world();
    return <div>Hello</div>;
  }
}

function world() {
  return "World";
}

It fails though if world is a named export:

export default class IndexPage {
  render(data) {
    const name = world();
    return <div>Hello</div>;
  }
}

export function world() {
  return "World";
}

Here is the traceback:

[11ty] 1. Having trouble rendering 11ty.tsx template ./content/index.11ty.tsx (via TemplateContentRenderError)
[11ty] 2. Class constructor IndexPage cannot be invoked without 'new' (via TypeError)
[11ty] 
[11ty] Original error stack trace: TypeError: Class constructor IndexPage cannot be invoked without 'new'
[11ty]     at JavaScript.<anonymous> (file:///Users/pauleveritt/projects/pauleveritt/www.pauleveritt.org/node_modules/@11ty/eleventy/src/Engines/JavaScript.js:211:39)
[11ty]     at Object.defaultCompilationFn [as defaultRenderer] (file:///Users/pauleveritt/projects/pauleveritt/www.pauleveritt.org/node_modules/@11ty/eleventy/src/Engines/Custom.js:172:12)
[11ty]     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
[11ty]     at async Object.<anonymous> (file:///Users/pauleveritt/projects/pauleveritt/www.pauleveritt.org/eleventy.config.js:40:25)
[11ty]     at async Template._render (file:///Users/pauleveritt/projects/pauleveritt/www.pauleveritt.org/node_modules/@11ty/eleventy/src/TemplateContent.js:588:19)
[11ty]     at async TemplateMap.populateContentDataInMap (file:///Users/pauleveritt/projects/pauleveritt/www.pauleveritt.org/node_modules/@11ty/eleventy/src/TemplateMap.js:537:7)
[11ty]     at async TemplateMap.cache (file:///Users/pauleveritt/projects/pauleveritt/www.pauleveritt.org/node_modules/@11ty/eleventy/src/TemplateMap.js:444:3)
[11ty]     at async TemplateWriter._createTemplateMap (file:///Users/pauleveritt/projects/pauleveritt/www.pauleveritt.org/node_modules/@11ty/eleventy/src/TemplateWriter.js:346:3)
[11ty]     at async TemplateWriter.generateTemplates (file:///Users/pauleveritt/projects/pauleveritt/www.pauleveritt.org/node_modules/@11ty/eleventy/src/TemplateWriter.js:377:3)
[11ty]     at async TemplateWriter.write (file:///Users/pauleveritt/projects/pauleveritt/www.pauleveritt.org/node_modules/@11ty/eleventy/src/TemplateWriter.js:425:21)

(As a note, I'm doing this under tsx as the NodeJS runner.)

Reproduction steps

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See an error

Expected behavior

I'd like my template to allow exported other exported functions so I can import them into a test module for tests.

Reproduction URL

No response

Screenshots

No response

@zachleat
Copy link
Member

Shipping with 3.0.0-alpha.17 thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants