WebC

Single File Web Components

README

WebC is for Single File Web Components


Features


Framework-independent standalone HTML serializer for generating markup for Web Components.
Expand any HTML element (including custom elements and web components) to HTML with defined conventions from web standards.
This means that Web Components created with WebC are compatible with server-side rendering (without duplicating author-written markup).
Compilation tools to aggregate component-level assets (CSS or JS) for critical CSS or client JavaScript.
Opt-in to scope your component CSS using WebC’s built-in CSS prefixer.
Or, use browser-native Shadow DOM style scoping (for future-compatibility when Declarative Shadow DOM browser support is ubiquitous)
Progressive-enhancement friendly.
Streaming friendly.
Shadow DOM friendly.
Async friendly.
The .webc file extension is recommended (not a requirement)—you _can_ use .html.
Tip for Visual Studio Code users: go to Preferences -> Settings -> Files: Associations to add a mapping for *.webc to html.

Integrations/Plugins


[@11ty/eleventy-plugin-webc](https://www.11ty.dev/docs/languages/webc/) adds WebC to Eleventy
[express-webc](https://github.com/NickColley/express-webc) by @NickColley adds WebC to Express

Testimonials


“javascript frameworks are dead to me”—Andy Bell


“The DX and authoring model you landed on here looks fantastic”—Addy Osmani


“Really like the programmatic API approach over using a bundler to pre-compile and then serve.”—Harminder Virk


Similar Works


Folks doing similar things with Web Components: check them out!

Lit SSR with plugins for Eleventy, Astro, and Rocket

Installation


Note: if you’re not building a plugin or integration for WebC, you can probably skip this section!

It’s available on [npm as @11ty/webc](https://www.npmjs.com/package/@11ty/webc):

  1. ```
  2. npm install @11ty/webc
  3. ```

This is an ESM project and as such requires a "type": "module" in your package.json (or use the .mjs file extension).

  1. ```js
  2. import { WebC } from "@11ty/webc";
  3. ```

You _can_ use this in a CommonJS file via dynamic import:

  1. ```js
  2. (async function() {
  3. const { WebC } = await import("@11ty/webc");
  4. })();
  5. ```

Examples


JavaScript API


  1. ```js
  2. import { WebC } from "@11ty/webc";

  3. let page = new WebC();

  4. // This enables aggregation of CSS and JS
  5. // As of 0.4.0+ this is disabled by default
  6. page.setBundlerMode(true);

  7. // File
  8. page.setInputPath("page.webc");

  9. // Or, a String
  10. // page.setContent(`

    Hello!

    `);

  11. let { html, css, js, components } = await page.compile();

  12. // Or, Readable Streams for each
  13. let { html, css, js } = await page.stream();
  14. ```

It’s HTML


If WebC looks familiar, that’s because WebC *is* HTML. These are single file HTML components but don’t require any special element conventions (for example Vue’s single file component uses a top-level `