lit parcel模板
mkdir myproject/src -p
cd myproject
pnpm i lit typescript
tsconfig.json
:
{
"compilerOptions": {
"target": "ES2020",
"experimentalDecorators": true,
"useDefineForClassFields": false,
"module": "ESNext",
"lib": [
"ES2020",
"DOM",
"DOM.Iterable"
],
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"isolatedModules": true,
"moduleDetection": "force",
"noEmit": true,
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": [
"src"
]
}
src/my.counter.ts
:
import { LitElement, css, html } from "lit";
import { customElement, property } from "lit/decorators.js";
import { styleMap } from "lit/directives/style-map.js";
@customElement("my-counter")
export class MyCounter extends LitElement {
@property({ type: Number })
count = 0;
render() {
const styles = { color: "" };
if (this.count >= 0) {
styles.color = "green";
} else {
styles.color = "red";
}
return html`
<div>
<button @click=${this._onDec} type="button"> - </button>
count is: <span style=${styleMap(styles)}>${this.count}</span>
<button @click=${this._onInc} type="button"> + </button>
</div>
`;
}
private _onDec() {
this.count--;
}
private _onInc() {
this.count++;
}
static styles = css`
button {
border-radius: 4px;
border: 1px solid transparent;
background-color: limegreen;
}
`;
}
declare global {
interface HTMLElementTagNameMap {
"my-counter": MyCounter;
}
}
src/index.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
Index
<my-counter></my-counter>
<script type="module" src="./my.counter.ts"></script>
</body>
</html>
src/about.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
About
<my-counter></my-counter>
<script type="module" src="./my.counter.ts"></script>
</body>
</html>
dev
:
parcel ./src/**.html
publish
:
parcel build ./src/**.html --no-source-maps --public-url .
justfile
:
publish:
#!/usr/bin/env bash
rm -rf dist
parcel build ./src/**.html --no-source-maps --public-url .
for js in dist/**.js
do
# closure-compiler "$js" --js_output_file new.js --charset UTF-8 --compilation_level SIMPLE --isolation_mode NONE
# mv new.js "$js"
terser "$js" --comments false --compress -o "$js"
done
# once more
for js in dist/**.js
do
# closure-compiler "$js" --js_output_file new.js --charset UTF-8 --compilation_level SIMPLE --isolation_mode NONE
# mv new.js "$js"
terser "$js" --comments false --compress -o "$js"
done
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话