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

What are the limitations of webc:type="js" #116

Closed
solution-loisir opened this issue Feb 18, 2023 · 3 comments
Closed

What are the limitations of webc:type="js" #116

solution-loisir opened this issue Feb 18, 2023 · 3 comments
Labels
bug Something isn't working duplicate This issue or pull request already exists
Milestone

Comments

@solution-loisir
Copy link

solution-loisir commented Feb 18, 2023

From the documentation I was expecting to be able to run any arbitrary server side (Node scripts) JavaScript inside webc:type="js".

Run any arbitrary server JavaScript in WebC. Source

Yet, I'm getting errors when trying to use JS built-in methods and constructors. For example, the following code.

comp-onent.webc

<script webc:type="js" webc:is="template">
  new Date().toString();
</script>

Will yield this error:

[11ty] 1. Having trouble rendering webc template ./src/pages/home.webc (via TemplateContentRenderError)
[11ty] 2. Date is not a constructor (via TypeError)
[11ty] 
[11ty] Original error stack trace: evalmachine.<anonymous>:2
[11ty]   new Date().toString();
[11ty]   ^
[11ty] 
[11ty] TypeError: Date is not a constructor
[11ty]     at evalmachine.<anonymous>:2:3
[11ty]     at Script.runInContext (node:vm:139:12)
[11ty]     at Script.runInNewContext (node:vm:144:17)
[11ty]     at Object.runInNewContext (node:vm:298:38)
[11ty]     at Function.evaluateScript (/home/mathieu/webc-starter-kit/node_modules/@11ty/webc/src/moduleScript.cjs:62:25)
[11ty]     at Object.<anonymous> (file:///home/mathieu/webc-starter-kit/node_modules/@11ty/webc/src/ast.js:130:24)
[11ty]     at AstSerializer.transformContent (file:///home/mathieu/webc-starter-kit/node_modules/@11ty/webc/src/ast.js:802:42)
[11ty]     at AstSerializer.compileNode (file:///home/mathieu/webc-starter-kit/node_modules/@11ty/webc/src/ast.js:1216:20)
[11ty]     at AstSerializer.getChildContent (file:///home/mathieu/webc-starter-kit/node_modules/@11ty/webc/src/ast.js:646:40)
[11ty]     at AstSerializer.compileNode (file:///home/mathieu/webc-starter-kit/node_modules/@11ty/webc/src/ast.js:1353:46)

Also, trying to use parseInt like so:

comp-onent.webc

<script webc:type="js" webc:is="template">
  const num = parseInt("1");
  `${2 + num}`;
</script>

Will throw:

[11ty] 1. Having trouble rendering webc template ./src/pages/home.webc (via TemplateContentRenderError)
[11ty] 2. parseInt is not a function (via TypeError)
[11ty] 
[11ty] Original error stack trace: evalmachine.<anonymous>:2
[11ty]   const num = parseInt("1");
[11ty]               ^
[11ty] 
[11ty] TypeError: parseInt is not a function
[11ty]     at evalmachine.<anonymous>:2:15
[11ty]     at Script.runInContext (node:vm:139:12)
[11ty]     at Script.runInNewContext (node:vm:144:17)
[11ty]     at Object.runInNewContext (node:vm:298:38)
[11ty]     at Function.evaluateScript (/home/mathieu/webc-starter-kit/node_modules/@11ty/webc/src/moduleScript.cjs:62:25)
[11ty]     at Object.<anonymous> (file:///home/mathieu/webc-starter-kit/node_modules/@11ty/webc/src/ast.js:130:24)
[11ty]     at AstSerializer.transformContent (file:///home/mathieu/webc-starter-kit/node_modules/@11ty/webc/src/ast.js:802:42)
[11ty]     at AstSerializer.compileNode (file:///home/mathieu/webc-starter-kit/node_modules/@11ty/webc/src/ast.js:1216:20)
[11ty]     at AstSerializer.getChildContent (file:///home/mathieu/webc-starter-kit/node_modules/@11ty/webc/src/ast.js:646:40)
[11ty]     at AstSerializer.compileNode (file:///home/mathieu/webc-starter-kit/node_modules/@11ty/webc/src/ast.js:1353:46)

I'm using Eleventy v2.0.0 with webc v0.8.1. I will investigate more and report here.

@solution-loisir solution-loisir changed the title What are the limitation of webc:type="js" What are the limitations of webc:type="js" Feb 18, 2023
@solution-loisir
Copy link
Author

It's possible to work around it by using an helper function. So for example:

eleventy config file

config.addFilter("number", function(value) {
  return parseInt(value, 10);
});

myComponent.webc

<div @html="number('3') - 2"></div>

<!-- Outputs <div>1</div> -->

@solution-loisir
Copy link
Author

I think I'm duplicating #100

@zachleat
Copy link
Member

Yeah! And #99 both fixed in the upcoming WebC v0.9.0

@zachleat zachleat transferred this issue from 11ty/eleventy-plugin-webc Feb 22, 2023
@zachleat zachleat added bug Something isn't working duplicate This issue or pull request already exists labels Feb 22, 2023
@zachleat zachleat added this to the WebC v0.9.0 milestone Feb 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants