"""Offline Playwright Chromium verification for Vue 3 Ch10 (directory-structure)."""
from fnmatch import fnmatch
from hashlib import sha256
import json
from playwright.sync_api import sync_playwright
def launch_browser(p):
for kwargs in (
dict(headless=True),
dict(headless=True, channel="chrome"),
dict(headless=True, channel="msedge"),
):
try:
return p.chromium.launch(**kwargs)
except Exception:
continue
raise RuntimeError("No Chromium/Chrome/Edge browser executable available")
def case_three_layer_project_validator():
root_layer = ["index.html", "vite.config.js", "package.json", ".gitignore", ".env", ".env.production", "README.md"]
public_layer = ["public/favicon.ico"]
src_layer = [
"src/assets/logo.png",
"src/components/CommonHeader.vue",
"src/composables/useFetch.js",
"src/router/index.js",
"src/stores/index.js",
"src/utils/request.js",
"src/views/HomeView.vue",
"src/App.vue",
"src/main.js",
]
enterprise_ext = ["src/layouts/", "src/plugins/", "src/directives/", "src/types/", "tests/", "src/api/"]
actual = {
"layer_counts": [len(root_layer), len(public_layer), len(src_layer)],
"extension_dirs": len(enterprise_ext),
"has_entry_chain": ("index.html" in root_layer) and ("src/main.js" in src_layer) and ("src/App.vue" in src_layer),
}
expected = {
"layer_counts": [7, 1, 9],
"extension_dirs": 6,
"has_entry_chain": True,
}
return dict(name="three_layer_project_validator", expected=expected, actual=actual, passed=(actual == expected))
def case_app_vue_prop_update_chromium(page):
svg_data = (
"data:image/svg+xml;utf8,"
"<svg xmlns='http://www.w3.org/2000/svg' width='40' height='40'>"
"<polygon points='20,36 4,6 36,6' fill='%2342b883'/></svg>"
)
html = f"""<!doctype html><html><head><meta charset="utf-8"><title>Vite App</title></head><body>
<div id="app">
<img id="vue-logo" alt="Vue logo" src="{svg_data}" />
<h1 id="hw-title"></h1>
</div>
<script>
function renderApp(msg) {{
document.getElementById("hw-title").textContent = msg;
}}
renderApp("Hello Vue 3.0 + Vite");
window.__initial = document.getElementById("hw-title").textContent;
renderApp("欢迎来到蓝色天空网 Vue 3 实验室!");
window.__updated = document.getElementById("hw-title").textContent;
</script></body></html>"""
page.set_content(html)
actual = page.evaluate("""() => ({
logoAlt: document.getElementById('vue-logo').alt,
logoLoaded: document.getElementById('vue-logo').complete && document.getElementById('vue-logo').naturalWidth === 40,
initial: window.__initial,
updated: window.__updated
})""")
expected = {
"logoAlt": "Vue logo",
"logoLoaded": True,
"initial": "Hello Vue 3.0 + Vite",
"updated": "欢迎来到蓝色天空网 Vue 3 实验室!",
}
return dict(name="app_vue_prop_update_chromium", expected=expected, actual=actual, passed=(actual == expected))
def case_public_vs_src_assets_pipeline():
raw_favicon = b"ICO-BYTES"
raw_logo = b"<svg>logo</svg>"
public_dist = "dist/favicon.ico"
src_hash = sha256(raw_logo).hexdigest()[:8]
src_dist = f"dist/assets/logo-{src_hash}.svg"
actual = {
"public_copied_verbatim": public_dist == "dist/favicon.ico",
"src_hashed_output": src_dist.startswith("dist/assets/logo-") and len(src_hash) == 8,
"public_ref_path": "/favicon.ico",
}
expected = {
"public_copied_verbatim": True,
"src_hashed_output": True,
"public_ref_path": "/favicon.ico",
}
return dict(name="public_vs_src_assets_pipeline", expected=expected, actual=actual, passed=(actual == expected))
def case_env_and_gitignore_rules():
env_dev = {"VITE_API_BASE": "http://localhost:8080/api"}
env_prod = {"VITE_API_BASE": "https://api.example.com"}
ignore_patterns = ["node_modules", "dist", ".env.local"]
probe = ["node_modules", "dist", ".env.local", "src/App.vue", "vite.config.js"]
ignored = [p for p in probe if any(fnmatch(p, pat) for pat in ignore_patterns)]
tracked = [p for p in probe if p not in ignored]
actual = {
"dev_api": env_dev["VITE_API_BASE"],
"prod_api": env_prod["VITE_API_BASE"],
"ignored": ignored,
"tracked": tracked,
}
expected = {
"dev_api": "http://localhost:8080/api",
"prod_api": "https://api.example.com",
"ignored": ["node_modules", "dist", ".env.local"],
"tracked": ["src/App.vue", "vite.config.js"],
}
return dict(name="env_and_gitignore_rules", expected=expected, actual=actual, passed=(actual == expected))
def main():
with sync_playwright() as p:
browser = launch_browser(p)
try:
page = browser.new_page()
cases = [
case_three_layer_project_validator(),
case_app_vue_prop_update_chromium(page),
case_public_vs_src_assets_pipeline(),
case_env_and_gitignore_rules(),
]
finally:
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-directory-structure-lab/explore-directory-structure.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-directory-structure.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联48章) · 含当前章节或实例 · mbskycom202612a801.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a819.zip