{"id":13672751,"url":"https://github.com/web-widget/vite-plugin-shadow-dom-css","last_synced_at":"2025-04-28T03:32:57.199Z","repository":{"id":43401942,"uuid":"442096093","full_name":"web-widget/vite-plugin-shadow-dom-css","owner":"web-widget","description":"Use CSS files in shadow dom","archived":false,"fork":false,"pushed_at":"2022-10-28T05:05:31.000Z","size":210,"stargazers_count":22,"open_issues_count":6,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-23T22:38:33.665Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/web-widget.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-12-27T08:18:40.000Z","updated_at":"2025-04-18T03:07:02.000Z","dependencies_parsed_at":"2022-09-12T19:10:28.597Z","dependency_job_id":null,"html_url":"https://github.com/web-widget/vite-plugin-shadow-dom-css","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/web-widget%2Fvite-plugin-shadow-dom-css","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/web-widget%2Fvite-plugin-shadow-dom-css/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/web-widget%2Fvite-plugin-shadow-dom-css/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/web-widget%2Fvite-plugin-shadow-dom-css/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/web-widget","download_url":"https://codeload.github.com/web-widget/vite-plugin-shadow-dom-css/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251246394,"owners_count":21558762,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T09:01:46.302Z","updated_at":"2025-04-28T03:32:52.190Z","avatar_url":"https://github.com/web-widget.png","language":"TypeScript","readme":"# vite-plugin-shadow-dom-css\n\nVite Shadow DOM CSS 插件。\n\n虽然 Web Components 充满了希望，但如今要在工程中使用它将会面临非非常多的问题，而样式的工程化首当其冲。Vite 与其他流行构建工具并没有很好的解决这样的问题，而开发社区中只有零星的文章讨论到它，所以这个插件是一次解决问题的尝试。\n\n- 能够将 CSS 插入到 Shadow DOM 中\n- 开发环境支持热更新\n- 可与 Vite 内置的 CSS 插件配合工作\n\n## Web Components 技术下的 CSS 工程化问题\n\n当你试图使用 Vite 作为构建工具来编写和 Web Components 相关的组件的时候，你会发现样式永远无法生效： \n\n```js\nimport myStyle from './my-style.css';\nexport class MyElement extends HTMLElement {\n  constructor() {\n    this.attachShadow({ mode: 'open' });\n    myStyle(this.shadowRoot).mount();\n    this.shadowRoot.innerHTML = `\n      \u003ch1\u003eHello world\u003c/h1\u003e\n    `;\n  }\n}\ncustomElements.define('my-element', MyElement);\n```\n\n```html\n\u003chtml\u003e\n  \u003chead\u003e\n    \u003cstyle\u003e\n      /*...*/\n    \u003c/style\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003cmy-element\u003e\n      #shadow-root\n      \t\u003ch1\u003eHello world\u003c/h1\u003e\n    \u003c/my-element\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n在开发模式中，我们会在 `\u003chead\u003e` 中得到由 Vite 生成的 `\u003cstyle\u003e`；在生产模式中，我们将得到 .css 文件，这些 .css 文件需要有我们额外通过 `\u003clink\u003e` 标签输出。这两种模式都不能让 CSS 工作在 Shadow DOM 中，因为 Shadow DOM 是一个隔离的样式环境。\n\n使用这个插件你可以让 CSS 按预期插入到 Shadow DOM 中：\n\n```html\n\u003chtml\u003e\n  \u003chead\u003e\u003c/head\u003e\n  \u003cbody\u003e\n    \u003cmy-element\u003e\n      #shadow-root\n      \t\u003ch1\u003eHello world\u003c/h1\u003e\n        \u003cstyle\u003e\n          /*...*/\n        \u003c/style\u003e\n    \u003c/my-element\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n## 基本示例\n\n### 添加插件\n\n```js\n// vite.config.js\nimport { defineConfig } from 'vite'\nimport shadowDomCss from 'vite-plugin-shadow-dom-css'\n\nexport default defineConfig({\n  plugins: [shadowDomCss()]\n})\n```\n\n### 引入 CSS 文件\n\n引入 CSS，并且增加查询参数 `style-provider`。\n\n```js\nimport myStyle from './my-style.css?style-provider';\n\nexport class MyElement extends HTMLElement {\n  constructor() {\n    this.attachShadow({ mode: 'open' });\n    this.shadowRoot.innerHTML = '\u003ch1\u003eHello world\u003c/h1\u003e';\n    myStyle(this.shadowRoot).mount();\n  }\n}\ncustomElements.define('my-element', MyElement);\n```\n\n### 引入 Vue 文件的 Style\n\n在 `.vue` 单文件中，在 `\u003cstyle\u003e` 上增加 `style-provider` 属性即可。\n\n```html\n\u003ctemplate\u003e\n  \u003cdiv class=\"index\"\u003e\n    \u003ch1\u003estyle-provider demo\u003c/h1\u003e\n  \u003c/div\u003e\n\u003c/template\u003e\n\n\u003cscript\u003e\u003c/script\u003e\n\n\u003cstyle style-provider\u003e\n  .index h1 {\n    font-weight: normal;\n  }\n\u003c/style\u003e\n```\n\n### 高级功能：查找间接引用的 CSS\n\n支持使用虚拟模块 `virtual:style-provider?query=${query}` 查询 CSS：\n\n`${query}` 支持的语法：\n\n* `*`: 通配符\n* `,`: 分割符\n* `~`: 当前包的根目录\n\n示例：\n\n```js\n// 所有样式\nimport allStyle from `virtual:style-provider?query=*`;\n// 当前包下的所有样式\nimport packageAllStyle from `virtual:style-provider?query=~/*`;\n// 指定多个样式\nimport selectStyle from `virtual:style-provider?query=@ui/dialog/*,@ui/button/*`;\n\nallStyle(document.head).mount();\npackageAllStyle(document.head).mount();\nselectStyle(document.head).mount();\n```\n\n\u003e 只能查询之前被导入的 CSS，请确保 CSS 引入的顺序是正确的。\n\n","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweb-widget%2Fvite-plugin-shadow-dom-css","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fweb-widget%2Fvite-plugin-shadow-dom-css","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweb-widget%2Fvite-plugin-shadow-dom-css/lists"}