@canonical/summon-component 0.16.0-experimental.1 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/README.md +160 -1
- package/package.json +5 -5
- package/src/index.ts +6 -1
- package/src/shared/types.ts +1 -1
- package/src/templates/webcomponents/component.ts.ejs +30 -0
- package/src/templates/webcomponents/index.ts.ejs +4 -0
- package/src/templates/webcomponents/stories.ts.ejs +19 -0
- package/src/templates/webcomponents/styles.css.ejs +16 -0
- package/src/templates/webcomponents/tests.ts.ejs +29 -0
- package/src/templates/webcomponents/types.ts.ejs +8 -0
- package/src/webcomponents/generator.ts +184 -0
- package/src/webcomponents/index.ts +6 -0
- package/src/webcomponents/types.ts +8 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,25 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [0.17.0](https://github.com/canonical/pragma/compare/v0.16.0...v0.17.0) (2026-03-04)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* **summon:** summon component webcomponents ([#448](https://github.com/canonical/pragma/issues/448)) ([ae5d33d](https://github.com/canonical/pragma/commit/ae5d33d052f7c05be292e6a565cf371f47868274))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# [0.16.0](https://github.com/canonical/pragma/compare/v0.16.0-experimental.1...v0.16.0) (2026-03-03)
|
|
18
|
+
|
|
19
|
+
**Note:** Version bump only for package @canonical/summon-component
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
6
25
|
# [0.16.0-experimental.1](https://github.com/canonical/pragma/compare/v0.16.0-experimental.0...v0.16.0-experimental.1) (2026-03-03)
|
|
7
26
|
|
|
8
27
|
**Note:** Version bump only for package @canonical/summon-component
|
package/README.md
CHANGED
|
@@ -62,6 +62,19 @@ summon component svelte --component-path=src/lib/components/Card
|
|
|
62
62
|
summon component svelte --component-path=src/lib/components/Card --use-ts-stories
|
|
63
63
|
```
|
|
64
64
|
|
|
65
|
+
### Web Component (Lit)
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
# Interactive — prompts guide you
|
|
69
|
+
summon component webcomponents
|
|
70
|
+
|
|
71
|
+
# Direct — specify path
|
|
72
|
+
summon component webcomponents --component-path=src/lib/Button
|
|
73
|
+
|
|
74
|
+
# Preview — see what would be created
|
|
75
|
+
summon component webcomponents --component-path=src/lib/Button --dry-run
|
|
76
|
+
```
|
|
77
|
+
|
|
65
78
|
---
|
|
66
79
|
|
|
67
80
|
## What Gets Generated
|
|
@@ -182,6 +195,142 @@ Uses Svelte 5 runes (`$props()`) and render tags (`@render`).
|
|
|
182
195
|
|
|
183
196
|
---
|
|
184
197
|
|
|
198
|
+
### Web Components (Lit)
|
|
199
|
+
|
|
200
|
+
For `summon component webcomponents --component-path=src/lib/Button`:
|
|
201
|
+
|
|
202
|
+
```
|
|
203
|
+
src/lib/Button/
|
|
204
|
+
├── Button.ts # Lit element implementation
|
|
205
|
+
├── types.ts # Props interface
|
|
206
|
+
├── index.ts # Barrel export
|
|
207
|
+
├── Button.tests.ts # Unit tests (Vitest + shadow DOM)
|
|
208
|
+
├── Button.stories.ts # Storybook stories (optional)
|
|
209
|
+
└── styles.css # Component styles as CSSResult (optional)
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
And appends to `src/lib/index.ts`:
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
export * from "./Button/index.js";
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
#### Generated Component
|
|
219
|
+
|
|
220
|
+
```typescript
|
|
221
|
+
// Button.ts
|
|
222
|
+
import { html, LitElement } from "lit";
|
|
223
|
+
import { customElement } from "lit/decorators.js";
|
|
224
|
+
import styles from "./styles.css";
|
|
225
|
+
import type { ButtonProps } from "./types.js";
|
|
226
|
+
|
|
227
|
+
const componentCssClassName = "ds button";
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Button web component.
|
|
231
|
+
*
|
|
232
|
+
* @implements ds:global.component.button
|
|
233
|
+
*/
|
|
234
|
+
@customElement("ds-button")
|
|
235
|
+
export default class Button extends LitElement implements ButtonProps {
|
|
236
|
+
static styles = styles;
|
|
237
|
+
|
|
238
|
+
render() {
|
|
239
|
+
return html`
|
|
240
|
+
<div class="${componentCssClassName}">
|
|
241
|
+
<slot></slot>
|
|
242
|
+
</div>
|
|
243
|
+
`;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
#### Generated Types
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
// types.ts
|
|
252
|
+
export interface ButtonProps {
|
|
253
|
+
// Define props specific to Button here
|
|
254
|
+
}
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
#### Generated Styles
|
|
258
|
+
|
|
259
|
+
```css
|
|
260
|
+
/* styles.css */
|
|
261
|
+
:host {
|
|
262
|
+
/*
|
|
263
|
+
* Declare component-scoped CSS variables here.
|
|
264
|
+
* Variables must be on :host to be accessible inside the shadow tree.
|
|
265
|
+
*
|
|
266
|
+
* Example:
|
|
267
|
+
* --button-color-background: var(--color-background-default);
|
|
268
|
+
* --button-color-text: var(--color-text-default);
|
|
269
|
+
*/
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
.ds.button {
|
|
273
|
+
/* Component styles */
|
|
274
|
+
}
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
#### Generated Test
|
|
278
|
+
|
|
279
|
+
```typescript
|
|
280
|
+
// Button.tests.ts
|
|
281
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
282
|
+
import "./Button.js";
|
|
283
|
+
import type Button from "./Button.js";
|
|
284
|
+
|
|
285
|
+
describe("Button component", () => {
|
|
286
|
+
let elem: Button;
|
|
287
|
+
|
|
288
|
+
beforeEach(() => {
|
|
289
|
+
elem = document.createElement("ds-button") as Button;
|
|
290
|
+
document.body.appendChild(elem);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
afterEach(() => {
|
|
294
|
+
elem.remove();
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("should render component", async () => {
|
|
298
|
+
await customElements.whenDefined("ds-button");
|
|
299
|
+
const container = elem.shadowRoot?.querySelector(".ds.button");
|
|
300
|
+
expect(container).toBeTruthy();
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it("should have correct tag name", () => {
|
|
304
|
+
expect(elem.tagName.toLowerCase()).toBe("ds-button");
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
#### Generated Stories
|
|
310
|
+
|
|
311
|
+
```typescript
|
|
312
|
+
// Button.stories.ts
|
|
313
|
+
import type { Meta, StoryObj } from "@storybook/web-components-vite";
|
|
314
|
+
import { html } from "lit";
|
|
315
|
+
|
|
316
|
+
import "./Button.js";
|
|
317
|
+
import type Button from "./Button.js";
|
|
318
|
+
|
|
319
|
+
const meta = {
|
|
320
|
+
title: "Web Components/Button",
|
|
321
|
+
tags: ["autodocs"],
|
|
322
|
+
} satisfies Meta<Button>;
|
|
323
|
+
|
|
324
|
+
export default meta;
|
|
325
|
+
type Story = StoryObj<Button>;
|
|
326
|
+
|
|
327
|
+
export const Default: Story = {
|
|
328
|
+
render: () => html`<ds-button></ds-button>`,
|
|
329
|
+
};
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
---
|
|
333
|
+
|
|
185
334
|
## Options Reference
|
|
186
335
|
|
|
187
336
|
### React Options
|
|
@@ -218,6 +367,16 @@ Uses Svelte 5 runes (`$props()`) and render tags (`@render`).
|
|
|
218
367
|
| `--no-preview` | Skip the file preview step |
|
|
219
368
|
| `--help` | Show all options |
|
|
220
369
|
|
|
370
|
+
### Web Components Options
|
|
371
|
+
|
|
372
|
+
| Flag | Description | Default |
|
|
373
|
+
|------|-------------|---------|
|
|
374
|
+
| `--component-path` | Full path for the component (e.g., `src/lib/Button`) | Interactive prompt |
|
|
375
|
+
| `--with-styles` | Include `styles.css` file | `true` |
|
|
376
|
+
| `--no-with-styles` | Skip styles file | — |
|
|
377
|
+
| `--with-stories` | Include Storybook stories | `true` |
|
|
378
|
+
| `--no-with-stories` | Skip stories file | — |
|
|
379
|
+
|
|
221
380
|
---
|
|
222
381
|
|
|
223
382
|
## Component Path Convention
|
|
@@ -420,7 +579,7 @@ summon
|
|
|
420
579
|
|
|
421
580
|
# Should show:
|
|
422
581
|
# component [pkg] (has subtopics)
|
|
423
|
-
# └─ react, svelte
|
|
582
|
+
# └─ react, svelte, webcomponents
|
|
424
583
|
```
|
|
425
584
|
|
|
426
585
|
If not, verify installation:
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@canonical/summon-component",
|
|
3
3
|
"description": "Component generators for Summon - React and Svelte component scaffolding",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.17.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
7
7
|
"author": {
|
|
@@ -30,9 +30,9 @@
|
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@biomejs/biome": "2.4.5",
|
|
33
|
-
"@canonical/biome-config": "^0.
|
|
34
|
-
"@canonical/typescript-config": "^0.
|
|
35
|
-
"@canonical/webarchitect": "^0.
|
|
33
|
+
"@canonical/biome-config": "^0.17.0",
|
|
34
|
+
"@canonical/typescript-config": "^0.17.0",
|
|
35
|
+
"@canonical/webarchitect": "^0.17.0",
|
|
36
36
|
"@types/bun": "^1.3.8",
|
|
37
37
|
"@types/node": "^24.10.13",
|
|
38
38
|
"typescript": "^5.9.3",
|
|
@@ -41,5 +41,5 @@
|
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"@canonical/summon": "^0.1.0"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "7797c6d0bc70e440c183e8a3f2b33c6d4f03e234"
|
|
45
45
|
}
|
package/src/index.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Component generators for Summon CLI.
|
|
5
5
|
*
|
|
6
|
-
* This package provides generators for creating React and
|
|
6
|
+
* This package provides generators for creating React, Svelte, and Web Components
|
|
7
7
|
* with TypeScript, tests, stories, and styles.
|
|
8
8
|
*
|
|
9
9
|
* @example
|
|
@@ -13,6 +13,9 @@
|
|
|
13
13
|
*
|
|
14
14
|
* # Generate a Svelte component
|
|
15
15
|
* summon component svelte --component-path=src/lib/components/Button
|
|
16
|
+
*
|
|
17
|
+
* # Generate a Web Component
|
|
18
|
+
* summon component webcomponents --component-path=src/lib/components/Button
|
|
16
19
|
* ```
|
|
17
20
|
*
|
|
18
21
|
* @packageDocumentation
|
|
@@ -21,6 +24,7 @@
|
|
|
21
24
|
import type { AnyGenerator } from "@canonical/summon";
|
|
22
25
|
import { generator as reactGenerator } from "./react/index.js";
|
|
23
26
|
import { generator as svelteGenerator } from "./svelte/index.js";
|
|
27
|
+
import { generator as webComponentsGenerator } from "./webcomponents/index.js";
|
|
24
28
|
|
|
25
29
|
/**
|
|
26
30
|
* Registry of all component generators.
|
|
@@ -31,6 +35,7 @@ import { generator as svelteGenerator } from "./svelte/index.js";
|
|
|
31
35
|
export const generators = {
|
|
32
36
|
"component/react": reactGenerator,
|
|
33
37
|
"component/svelte": svelteGenerator,
|
|
38
|
+
"component/webcomponents": webComponentsGenerator,
|
|
34
39
|
} as const satisfies Record<string, AnyGenerator>;
|
|
35
40
|
|
|
36
41
|
export default generators;
|
package/src/shared/types.ts
CHANGED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Generated by <%= generatorName %> v<%= version %>
|
|
2
|
+
|
|
3
|
+
import { html, LitElement } from "lit";
|
|
4
|
+
import { customElement } from "lit/decorators.js";
|
|
5
|
+
<% if (withStyles) { -%>
|
|
6
|
+
import styles from "./styles.css";
|
|
7
|
+
<% } -%>
|
|
8
|
+
import type { <%= name %>Props } from "./types.js";
|
|
9
|
+
|
|
10
|
+
const componentCssClassName = "ds <%= kebabName %>";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* <%= name %> web component.
|
|
14
|
+
*
|
|
15
|
+
* @implements ds:global.component.<%= kebabName %>
|
|
16
|
+
*/
|
|
17
|
+
@customElement("ds-<%= kebabName %>")
|
|
18
|
+
export default class <%= name %> extends LitElement implements <%= name %>Props {
|
|
19
|
+
<% if (withStyles) { -%>
|
|
20
|
+
static styles = styles;
|
|
21
|
+
|
|
22
|
+
<% } -%>
|
|
23
|
+
render() {
|
|
24
|
+
return html`
|
|
25
|
+
<div class="${componentCssClassName}">
|
|
26
|
+
<slot></slot>
|
|
27
|
+
</div>
|
|
28
|
+
`;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Generated by <%= generatorName %> v<%= version %>
|
|
2
|
+
|
|
3
|
+
import type { Meta, StoryObj } from "@storybook/web-components-vite";
|
|
4
|
+
import { html } from "lit";
|
|
5
|
+
|
|
6
|
+
import "./<%= name %>.js";
|
|
7
|
+
import type <%= name %> from "./<%= name %>.js";
|
|
8
|
+
|
|
9
|
+
const meta = {
|
|
10
|
+
title: "Web Components/<%= name %>",
|
|
11
|
+
tags: ["autodocs"],
|
|
12
|
+
} satisfies Meta<<%= name %>>;
|
|
13
|
+
|
|
14
|
+
export default meta;
|
|
15
|
+
type Story = StoryObj<<%= name %>>;
|
|
16
|
+
|
|
17
|
+
export const Default: Story = {
|
|
18
|
+
render: () => html`<ds-<%= kebabName %>></ds-<%= kebabName %>>`,
|
|
19
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/* Generated by <%= generatorName %> v<%= version %> */
|
|
2
|
+
|
|
3
|
+
:host {
|
|
4
|
+
/*
|
|
5
|
+
* Declare component-scoped CSS variables here.
|
|
6
|
+
* Variables must be on :host to be accessible inside the shadow tree.
|
|
7
|
+
*
|
|
8
|
+
* Example:
|
|
9
|
+
* --<%= kebabName %>-color-background: var(--color-background-default);
|
|
10
|
+
* --<%= kebabName %>-color-text: var(--color-text-default);
|
|
11
|
+
*/
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
.ds.<%= kebabName %> {
|
|
15
|
+
/* Component styles */
|
|
16
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Generated by <%= generatorName %> v<%= version %>
|
|
2
|
+
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
4
|
+
import "./<%= name %>.js";
|
|
5
|
+
import type <%= name %> from "./<%= name %>.js";
|
|
6
|
+
|
|
7
|
+
describe("<%= name %> component", () => {
|
|
8
|
+
let elem: <%= name %>;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
elem = document.createElement("ds-<%= kebabName %>") as <%= name %>;
|
|
12
|
+
document.body.appendChild(elem);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
elem.remove();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should render component", async () => {
|
|
20
|
+
await customElements.whenDefined("ds-<%= kebabName %>");
|
|
21
|
+
|
|
22
|
+
const container = elem.shadowRoot?.querySelector(".ds.<%= kebabName %>");
|
|
23
|
+
expect(container).toBeTruthy();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should have correct tag name", () => {
|
|
27
|
+
expect(elem.tagName.toLowerCase()).toBe("ds-<%= kebabName %>");
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web Components Generator
|
|
3
|
+
*
|
|
4
|
+
* Generates a Lit web component with a consistent file structure:
|
|
5
|
+
* - `ComponentName.ts` - Main component file (Lit element)
|
|
6
|
+
* - `index.ts` - Barrel export
|
|
7
|
+
* - `ComponentName.tests.ts` - Unit tests with Vitest
|
|
8
|
+
* - `ComponentName.stories.ts` - Storybook stories (optional)
|
|
9
|
+
* - `styles.css` - CSS styles (optional)
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```bash
|
|
13
|
+
* summon component webcomponents --component-path=src/lib/components/Button
|
|
14
|
+
* summon component webcomponents --component-path=src/lib/components/Card --with-styles --with-stories
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @module
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import * as path from "node:path";
|
|
21
|
+
import { fileURLToPath } from "node:url";
|
|
22
|
+
import {
|
|
23
|
+
debug,
|
|
24
|
+
type GeneratorDefinition,
|
|
25
|
+
info,
|
|
26
|
+
mkdir,
|
|
27
|
+
sequence_,
|
|
28
|
+
template,
|
|
29
|
+
when,
|
|
30
|
+
} from "@canonical/summon";
|
|
31
|
+
|
|
32
|
+
import {
|
|
33
|
+
appendExportToParentIndex,
|
|
34
|
+
createComponentPathPrompt,
|
|
35
|
+
createTemplateContext,
|
|
36
|
+
getComponentName,
|
|
37
|
+
getParentDir,
|
|
38
|
+
} from "../shared/index.js";
|
|
39
|
+
import type { WebComponentAnswers } from "./types.js";
|
|
40
|
+
|
|
41
|
+
// =============================================================================
|
|
42
|
+
// Template Paths
|
|
43
|
+
// =============================================================================
|
|
44
|
+
|
|
45
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
46
|
+
const templatesDir = path.join(__dirname, "..", "templates");
|
|
47
|
+
|
|
48
|
+
const webComponentTemplates = {
|
|
49
|
+
component: path.join(templatesDir, "webcomponents", "component.ts.ejs"),
|
|
50
|
+
index: path.join(templatesDir, "webcomponents", "index.ts.ejs"),
|
|
51
|
+
types: path.join(templatesDir, "webcomponents", "types.ts.ejs"),
|
|
52
|
+
tests: path.join(templatesDir, "webcomponents", "tests.ts.ejs"),
|
|
53
|
+
stories: path.join(templatesDir, "webcomponents", "stories.ts.ejs"),
|
|
54
|
+
styles: path.join(templatesDir, "webcomponents", "styles.css.ejs"),
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// =============================================================================
|
|
58
|
+
// Custom Prompts (without SSR tests)
|
|
59
|
+
// =============================================================================
|
|
60
|
+
|
|
61
|
+
const webComponentPrompts = [
|
|
62
|
+
{
|
|
63
|
+
name: "withStyles",
|
|
64
|
+
type: "confirm",
|
|
65
|
+
message: "Include styles?",
|
|
66
|
+
default: true,
|
|
67
|
+
group: "Options",
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
name: "withStories",
|
|
71
|
+
type: "confirm",
|
|
72
|
+
message: "Include Storybook stories?",
|
|
73
|
+
default: true,
|
|
74
|
+
group: "Options",
|
|
75
|
+
},
|
|
76
|
+
] as const;
|
|
77
|
+
|
|
78
|
+
// =============================================================================
|
|
79
|
+
// Generator Definition
|
|
80
|
+
// =============================================================================
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Web components generator definition.
|
|
84
|
+
*
|
|
85
|
+
* This generator creates a complete Lit web component with TypeScript support,
|
|
86
|
+
* testing infrastructure, and optional Storybook stories and CSS styles.
|
|
87
|
+
*
|
|
88
|
+
* @see {@link https://summon.dev/generators/component/webcomponents} for documentation
|
|
89
|
+
*/
|
|
90
|
+
export const generator = {
|
|
91
|
+
meta: {
|
|
92
|
+
name: "component/webcomponents",
|
|
93
|
+
description:
|
|
94
|
+
"Generate a Lit web component with TypeScript, tests, stories, and styles",
|
|
95
|
+
version: "0.1.0",
|
|
96
|
+
help: `Generate a Lit web component with TypeScript, tests, stories, and styles.
|
|
97
|
+
|
|
98
|
+
FEATURES:
|
|
99
|
+
- Lit element with TypeScript decorators
|
|
100
|
+
- Unit tests with Vitest and shadow DOM testing
|
|
101
|
+
- Storybook integration with @storybook/web-components-vite (optional)
|
|
102
|
+
- CSS styles (optional)
|
|
103
|
+
- Auto-export from parent index.ts
|
|
104
|
+
|
|
105
|
+
The component name is extracted from the path and must be PascalCase.
|
|
106
|
+
For example, 'src/lib/components/Button' creates a 'Button' component
|
|
107
|
+
with the custom element tag 'button'.`,
|
|
108
|
+
examples: [
|
|
109
|
+
"summon component webcomponents --component-path=src/lib/components/Button",
|
|
110
|
+
"summon component webcomponents --component-path=src/lib/components/Card --with-styles --with-stories",
|
|
111
|
+
"summon component webcomponents --component-path=src/lib/components/Modal --no-with-styles",
|
|
112
|
+
"summon component webcomponents --component-path=src/lib/components/Button --dry-run",
|
|
113
|
+
],
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
prompts: [createComponentPathPrompt("webcomponents"), ...webComponentPrompts],
|
|
117
|
+
|
|
118
|
+
generate: (answers) => {
|
|
119
|
+
const componentName = getComponentName(answers.componentPath);
|
|
120
|
+
const componentDir = answers.componentPath;
|
|
121
|
+
const parentDir = getParentDir(answers.componentPath);
|
|
122
|
+
const ctx = createTemplateContext(answers, "webcomponents");
|
|
123
|
+
|
|
124
|
+
return sequence_([
|
|
125
|
+
info(`Generating Lit web component: ${componentName}`),
|
|
126
|
+
|
|
127
|
+
debug("Creating component directory"),
|
|
128
|
+
mkdir(componentDir),
|
|
129
|
+
|
|
130
|
+
debug("Creating main component file"),
|
|
131
|
+
template({
|
|
132
|
+
source: webComponentTemplates.component,
|
|
133
|
+
dest: path.join(componentDir, `${componentName}.ts`),
|
|
134
|
+
vars: ctx,
|
|
135
|
+
}),
|
|
136
|
+
|
|
137
|
+
debug("Creating index barrel file"),
|
|
138
|
+
template({
|
|
139
|
+
source: webComponentTemplates.index,
|
|
140
|
+
dest: path.join(componentDir, "index.ts"),
|
|
141
|
+
vars: ctx,
|
|
142
|
+
}),
|
|
143
|
+
|
|
144
|
+
debug("Creating types file"),
|
|
145
|
+
template({
|
|
146
|
+
source: webComponentTemplates.types,
|
|
147
|
+
dest: path.join(componentDir, "types.ts"),
|
|
148
|
+
vars: ctx,
|
|
149
|
+
}),
|
|
150
|
+
|
|
151
|
+
debug("Creating test file"),
|
|
152
|
+
template({
|
|
153
|
+
source: webComponentTemplates.tests,
|
|
154
|
+
dest: path.join(componentDir, `${componentName}.tests.ts`),
|
|
155
|
+
vars: ctx,
|
|
156
|
+
}),
|
|
157
|
+
|
|
158
|
+
when(answers.withStories, debug("Creating stories file")),
|
|
159
|
+
when(
|
|
160
|
+
answers.withStories,
|
|
161
|
+
template({
|
|
162
|
+
source: webComponentTemplates.stories,
|
|
163
|
+
dest: path.join(componentDir, `${componentName}.stories.ts`),
|
|
164
|
+
vars: ctx,
|
|
165
|
+
}),
|
|
166
|
+
),
|
|
167
|
+
|
|
168
|
+
when(answers.withStyles, debug("Creating styles file")),
|
|
169
|
+
when(
|
|
170
|
+
answers.withStyles,
|
|
171
|
+
template({
|
|
172
|
+
source: webComponentTemplates.styles,
|
|
173
|
+
dest: path.join(componentDir, "styles.css"),
|
|
174
|
+
vars: ctx,
|
|
175
|
+
}),
|
|
176
|
+
),
|
|
177
|
+
|
|
178
|
+
debug("Updating parent index.ts with export"),
|
|
179
|
+
appendExportToParentIndex(parentDir, componentName),
|
|
180
|
+
|
|
181
|
+
info(`Created ${componentName} component at ${componentDir}`),
|
|
182
|
+
]);
|
|
183
|
+
},
|
|
184
|
+
} as const satisfies GeneratorDefinition<WebComponentAnswers>;
|