"""Offline Playwright Chromium lab for Vue3 Ch32: TaskHub Vite + Tailwind v4 setup."""
import json
from playwright.sync_api import sync_playwright
HTML = r"""<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<title>task-hub</title>
<style>
@layer base, utilities;
:root {
--color-brand: #3b82f6;
--radius-xl: 1rem;
}
@layer base {
body {
background-color: #f8fafc;
color: #0f172a;
-webkit-font-smoothing: antialiased;
margin: 0;
font-family: system-ui, sans-serif;
}
}
@layer utilities {
.bg-brand { background-color: var(--color-brand); }
.rounded-xl-token { border-radius: var(--radius-xl); }
}
</style>
</head>
<body>
<div id="app"></div>
<script>
// Simulate cleaned src/App.vue mount into #app (HelloWorld.vue removed)
const appEl = document.getElementById('app');
appEl.innerHTML = '<div><h1>TaskHub</h1><button id="brand-chip" class="bg-brand rounded-xl-token">Ready</button></div>';
// Simulate vite.config.js plugin & @ alias resolution
const viteConfig = {
plugins: ['vue', '@tailwindcss/vite'],
resolve: { alias: { '@': './src' } },
server: { port: 5173 }
};
window.__resolveAlias = function(specifier) {
for (const [k, target] of Object.entries(viteConfig.resolve.alias)) {
if (specifier.startsWith(k + '/')) {
return target.replace(/^\.\//, '') + specifier.slice(k.length);
}
}
return specifier;
};
window.__viteMeta = {
plugins: viteConfig.plugins,
port: viteConfig.server.port,
cliTemplates: {
npm: 'npm create vite@latest task-hub -- --template vue',
yarn: 'yarn create vite task-hub --template vue',
pnpm: 'pnpm create vite task-hub -- --template vue'
},
entryFiles: ['index.html', 'src/main.js', 'src/App.vue', 'vite.config.js', 'src/style.css']
};
</script>
</body>
</html>
"""
def launch_browser(p):
try:
return p.chromium.launch(headless=True)
except Exception:
return p.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page()
page.set_content(HTML, wait_until="domcontentloaded")
# Case 1: Cleaned App.vue mount & starter template removal
act1 = page.evaluate("""() => ({
docTitle: document.title,
h1Text: document.querySelector('#app h1')?.textContent,
hasHelloWorldCounter: document.body.textContent.includes('count is 0')
})""")
exp1 = {"docTitle": "task-hub", "h1Text": "TaskHub", "hasHelloWorldCounter": False}
cases.append({"name": "scaffold_and_entry_mount", "expected": exp1, "actual": act1, "passed": act1 == exp1})
# Case 2: Vite plugins & @ path alias resolution
act2 = page.evaluate("""() => ({
plugins: window.__viteMeta.plugins,
resolvedComponent: window.__resolveAlias('@/components/TaskItem.vue'),
resolvedStore: window.__resolveAlias('@/stores/taskStore.js'),
devPort: window.__viteMeta.port
})""")
exp2 = {
"plugins": ["vue", "@tailwindcss/vite"],
"resolvedComponent": "src/components/TaskItem.vue",
"resolvedStore": "src/stores/taskStore.js",
"devPort": 5173,
}
cases.append({"name": "vite_alias_and_plugin_config", "expected": exp2, "actual": act2, "passed": act2 == exp2})
# Case 3: Tailwind v4 @theme CSS variables & @layer base computed styles
act3 = page.evaluate("""() => {
const rootStyle = getComputedStyle(document.documentElement);
const bodyStyle = getComputedStyle(document.body);
const chipStyle = getComputedStyle(document.getElementById('brand-chip'));
return {
tokenBrand: rootStyle.getPropertyValue('--color-brand').trim(),
tokenRadius: rootStyle.getPropertyValue('--radius-xl').trim(),
bodyBg: bodyStyle.backgroundColor,
bodyColor: bodyStyle.color,
chipBg: chipStyle.backgroundColor,
chipRadius: chipStyle.borderRadius
};
}""")
exp3 = {
"tokenBrand": "#3b82f6",
"tokenRadius": "1rem",
"bodyBg": "rgb(248, 250, 252)",
"bodyColor": "rgb(15, 23, 42)",
"chipBg": "rgb(59, 130, 246)",
"chipRadius": "16px",
}
cases.append({"name": "tailwind_v4_theme_variables", "expected": exp3, "actual": act3, "passed": act3 == exp3})
# Case 4: Scaffold CLI matrix & core project files
act4 = page.evaluate("""() => ({
cliCount: Object.keys(window.__viteMeta.cliTemplates).length,
usesVueTemplate: Object.values(window.__viteMeta.cliTemplates).every(c => c.includes('--template vue')),
entryCount: window.__viteMeta.entryFiles.length,
hasViteConfig: window.__viteMeta.entryFiles.includes('vite.config.js')
})""")
exp4 = {"cliCount": 3, "usesVueTemplate": True, "entryCount": 5, "hasViteConfig": True}
cases.append({"name": "cli_commands_inventory", "expected": exp4, "actual": act4, "passed": act4 == exp4})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/vue3-taskhub-config-lab/explore-taskhub-config.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-taskhub-config.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联48章) · 含当前章节或实例 · mbskycom202612a801.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a833.zip