webc icon indicating copy to clipboard operation
webc copied to clipboard

Define component from string

Open dz4k opened this issue 3 years ago • 6 comments

Right now, WebC accepts file paths for component definitions and reads the file during compilation. It would be nice to have a way to pass the component source code directly as a string instead.

webc.defineComponent("my-component", "my-component.webc", "Hello <slot></slot>!")

dz4k avatar Oct 09 '22 14:10 dz4k

You can do the following using the JavaScript API:

let component = new WebC();

component.setContent("Hello <slot></slot>!")

Does that cover your use case?

kleinfreund avatar Oct 11 '22 11:10 kleinfreund

This makes sense to me! Not all components will exist on the file system

zachleat avatar Oct 14 '22 13:10 zachleat

I was looking for this API when I was playing around yesterday. From a consistency perspective it makes sense to offer an API for both files and programmatic definition, like there is for content already.

Would this make building a repl/playground easier since everything can be done in memory, without the assumption of a file system?

WickyNilliams avatar Oct 16 '22 11:10 WickyNilliams

Would a REPL/playground require WebC to be able to be run in a browser environment? Currently, it's pretty tied to a Node.js environment.

kleinfreund avatar Oct 16 '22 14:10 kleinfreund

I'm not familiar enough with the codebase to say. I guess you could create a repl on Stackblitz or something if it does require a node env. I think it'd be a nice goal for it to be able to run in the browser though.

WickyNilliams avatar Oct 16 '22 14:10 WickyNilliams

I agree, it would be nice to run WebC in a browser (I don't even have a good reason for this beyond "it feels right"), but I also think this depends on what WebC wants to accomplish/be.

Should WebC go down the route of (at some arbitrary point the future that is) be a hybrid tool that runs in parts in a browser environment and/or in parts in a Node environment. Some things (e.g. reading from a file system) make only sense in either.

As a good example of a tool/library that is structured as a kind of hybrid between browser and Node environments, I'd like to point to https://mswjs.io/. Notably, it's written in a way that importing from msw imports browser-running code while importing from msw/node imports Node-running code.

kleinfreund avatar Oct 16 '22 15:10 kleinfreund