|
| 1 | +```bash |
| 2 | +mkdir start-solution |
| 3 | +``` |
| 4 | + |
| 5 | +```bash |
| 6 | +cd start-solution |
| 7 | +``` |
| 8 | + |
| 9 | +```bash |
| 10 | +nvm use 22 |
| 11 | +``` |
| 12 | + |
| 13 | +```bash |
| 14 | +npm init -y |
| 15 | +``` |
| 16 | + |
| 17 | +Update `package.json` |
| 18 | + |
| 19 | +```diff |
| 20 | +{ |
| 21 | + "name": "start-solution", |
| 22 | + "version": "1.0.0", |
| 23 | + "main": "index.js", |
| 24 | ++ "type": "module", |
| 25 | + "scripts": { |
| 26 | +... |
| 27 | +``` |
| 28 | + |
| 29 | +```bash |
| 30 | +npm i lit |
| 31 | +``` |
| 32 | + |
| 33 | +```bash |
| 34 | +npm i typescript rimraf @web/dev-server -D |
| 35 | +``` |
| 36 | + |
| 37 | +Create `tsconfig.json` |
| 38 | + |
| 39 | +```json |
| 40 | +{ |
| 41 | + "compilerOptions": { |
| 42 | + "target": "es2021", |
| 43 | + "module": "es2020", |
| 44 | + "lib": ["es2021", "DOM", "DOM.Iterable"], |
| 45 | + "declaration": true, |
| 46 | + "declarationMap": true, |
| 47 | + "sourceMap": true, |
| 48 | + "inlineSources": true, |
| 49 | + "outDir": "./", |
| 50 | + "rootDir": "./src", |
| 51 | + "strict": true, |
| 52 | + "noUnusedLocals": true, |
| 53 | + "noUnusedParameters": true, |
| 54 | + "noImplicitReturns": true, |
| 55 | + "noFallthroughCasesInSwitch": true, |
| 56 | + "noImplicitAny": true, |
| 57 | + "noImplicitThis": true, |
| 58 | + "moduleResolution": "node", |
| 59 | + "allowSyntheticDefaultImports": true, |
| 60 | + "experimentalDecorators": true, |
| 61 | + "forceConsistentCasingInFileNames": true, |
| 62 | + "noImplicitOverride": true, |
| 63 | + "plugins": [ |
| 64 | + { |
| 65 | + "name": "ts-lit-plugin", |
| 66 | + "strict": true |
| 67 | + } |
| 68 | + ], |
| 69 | + "types": ["mocha"] |
| 70 | + }, |
| 71 | + "include": ["src/**/*.ts"], |
| 72 | + "exclude": [] |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +Create `src/my-element.ts` |
| 77 | + |
| 78 | +```ts |
| 79 | +import { LitElement, html, css } from "lit"; |
| 80 | +import { customElement, property } from "lit/decorators.js"; |
| 81 | + |
| 82 | +@customElement("my-element") |
| 83 | +export class MyElement extends LitElement { |
| 84 | + static override styles = css` |
| 85 | + :host { |
| 86 | + display: block; |
| 87 | + border: solid 1px gray; |
| 88 | + padding: 16px; |
| 89 | + max-width: 800px; |
| 90 | + } |
| 91 | + `; |
| 92 | + |
| 93 | + @property() |
| 94 | + name = "World"; |
| 95 | + |
| 96 | + @property({ type: Number }) |
| 97 | + count = 0; |
| 98 | + |
| 99 | + override render() { |
| 100 | + return html` |
| 101 | + <h1>${this.sayHello(this.name)}!</h1> |
| 102 | + <button @click=${this._onClick} part="button"> |
| 103 | + Click Count: ${this.count} |
| 104 | + </button> |
| 105 | + <slot></slot> |
| 106 | + `; |
| 107 | + } |
| 108 | + |
| 109 | + private _onClick() { |
| 110 | + this.count++; |
| 111 | + this.dispatchEvent(new CustomEvent("count-changed")); |
| 112 | + } |
| 113 | + |
| 114 | + sayHello(name: string): string { |
| 115 | + return `Hello, ${name}`; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +declare global { |
| 120 | + interface HTMLElementTagNameMap { |
| 121 | + "my-element": MyElement; |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +Update `package.json` |
| 127 | + |
| 128 | +```diff |
| 129 | +.... |
| 130 | + "scripts": { |
| 131 | ++ "build": "tsc", |
| 132 | ++ "clean": "rimraf my-element.{d.ts,d.ts.map,js,js.map}", |
| 133 | + "test": "echo \"Error: no test specified\" && exit 1" |
| 134 | + }, |
| 135 | +.... |
| 136 | +``` |
| 137 | + |
| 138 | +```bash |
| 139 | +npm run build |
| 140 | +``` |
| 141 | + |
| 142 | +```bash |
| 143 | +npm run clean |
| 144 | +``` |
| 145 | + |
| 146 | +Create `web-dev-server.config.js` |
| 147 | + |
| 148 | +```js |
| 149 | +const mode = process.env.MODE || "dev"; |
| 150 | + |
| 151 | +if (!["dev", "prod"].includes(mode)) { |
| 152 | + throw new Error(`MODE must be "dev" or "prod" was "${mode}"`); |
| 153 | +} |
| 154 | + |
| 155 | +export default { |
| 156 | + nodeResolve: { exportConditions: mode === "dev" ? ["development"] : [] }, |
| 157 | + preserveSymlinks: true, |
| 158 | +}; |
| 159 | +``` |
| 160 | + |
| 161 | +Create `index.html` |
| 162 | + |
| 163 | +```html |
| 164 | +<!DOCTYPE html> |
| 165 | +<html lang="en"> |
| 166 | + <head> |
| 167 | + <meta charset="UTF-8" /> |
| 168 | + <title>Lit Starter Kit</title> |
| 169 | + </head> |
| 170 | + <body> |
| 171 | + <a href="/dev/index.html">Component Demo</a> |
| 172 | + </body> |
| 173 | +</html> |
| 174 | +``` |
| 175 | + |
| 176 | +Create `/dev/index.html` |
| 177 | + |
| 178 | +```html |
| 179 | +<!DOCTYPE html> |
| 180 | +<html lang="en"> |
| 181 | + <head> |
| 182 | + <meta charset="UTF-8" /> |
| 183 | + <title><my-element> Demo</title> |
| 184 | + <script type="module" src="../my-element.js"></script> |
| 185 | + <style> |
| 186 | + p { |
| 187 | + border: solid 1px solid; |
| 188 | + padding: 8px; |
| 189 | + } |
| 190 | + </style> |
| 191 | + </head> |
| 192 | + <body> |
| 193 | + <my-element> |
| 194 | + <p>This is a child content</p> |
| 195 | + </my-element> |
| 196 | + </body> |
| 197 | +</html> |
| 198 | +``` |
| 199 | + |
| 200 | +Update `package.json` |
| 201 | + |
| 202 | +```diff |
| 203 | +.... |
| 204 | + "scripts": { |
| 205 | + "build": "tsc", |
| 206 | ++ "build:watch": "tsc --watch", |
| 207 | + "clean": "rimraf my-element.{d.ts,d.ts.map,js,js.map}", |
| 208 | ++ "serve": "wds --watch", |
| 209 | ++ "serve:prod": "MODE=prod npm run serve", |
| 210 | + "test": "echo \"Error: no test specified\" && exit 1" |
| 211 | + }, |
| 212 | +.... |
| 213 | +``` |
| 214 | + |
| 215 | +```bash |
| 216 | +npm run build |
| 217 | +``` |
| 218 | + |
| 219 | +```bash |
| 220 | +npm run serve |
| 221 | +``` |
| 222 | + |
| 223 | +## Updating build assets |
| 224 | + |
| 225 | +Ok, to make our lives a little bit easier, lets change the ouput directory and use the reference on the index.html that we are serving: |
| 226 | + |
| 227 | +Update `tsconfig.json` |
| 228 | + |
| 229 | +```diff |
| 230 | +-"outDir": "./", |
| 231 | ++"outDir": "./dist", |
| 232 | +``` |
| 233 | + |
| 234 | +Update `/dev/index.html` |
| 235 | + |
| 236 | +```diff |
| 237 | +-<script type="module" src="../my-element.js"></script> |
| 238 | ++<script type="module" src="../dist/my-element.js"></script> |
| 239 | +``` |
| 240 | + |
| 241 | +Update `package.json` |
| 242 | + |
| 243 | +```diff |
| 244 | +.... |
| 245 | + "scripts": { |
| 246 | ++ "prebuild": "npm run clean:dist", |
| 247 | + "build": "tsc", |
| 248 | + "build:watch": "tsc --watch", |
| 249 | + "clean": "rimraf my-element.{d.ts,d.ts.map,js,js.map}", |
| 250 | ++ "clean:dist": "rimraf dist", |
| 251 | + "serve": "wds --watch", |
| 252 | + "serve:prod": "MODE=prod npm run serve", |
| 253 | + "test": "echo \"Error: no test specified\" && exit 1" |
| 254 | + }, |
| 255 | +.... |
| 256 | +``` |
| 257 | + |
| 258 | +```bash |
| 259 | +npm run build |
| 260 | +``` |
| 261 | + |
| 262 | +```bash |
| 263 | +npm run serve |
| 264 | +``` |
0 commit comments