Skip to content

Commit

Permalink
feat: Inject sfc style to csui root when using vue(PlasmoHQ#675) (Pla…
Browse files Browse the repository at this point in the history
…smoHQ#686)

* Add style-raw:*.vue pipeline
* Add getSfcStyleContent to PlasmoCSUI and define it at mount time
* Add SfcStyleContent to getStyle argument
* Override getStyle when it is not defined
  • Loading branch information
udotree authored Jul 17, 2023
1 parent f42b9b0 commit 462e804
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
8 changes: 7 additions & 1 deletion cli/plasmo/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ export type PlasmoMountShadowHost = (

export type PlasmoGetShadowHostId = Getter<string, PlasmoCSUIAnchor>

export type PlasmoGetStyle = Getter<HTMLStyleElement, PlasmoCSUIAnchor>
export type PlasmoGetStyle = Getter<
HTMLStyleElement,
PlasmoCSUIAnchor & { sfcStyleContent?: string }
>

export type PlasmoGetSfcStyleContent = Getter<string>

/**
* @return a cleanup unwatch function that will be run when unmounted
Expand Down Expand Up @@ -113,6 +118,7 @@ export type PlasmoCSUIWatch = (props: {
export type PlasmoCSUI<T> = {
default: any
getStyle: PlasmoGetStyle
getSfcStyleContent: PlasmoGetSfcStyleContent
getShadowHostId: PlasmoGetShadowHostId

getOverlayAnchor: PlasmoGetOverlayAnchor
Expand Down
6 changes: 5 additions & 1 deletion cli/plasmo/templates/static/common/csui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ async function injectAnchor<T>(
mountState?: PlasmoCSUIMountState
) {
if (typeof Mount.getStyle === "function") {
shadowRoot.prepend(await Mount.getStyle(anchor))
const sfcStyleContent =
typeof Mount.getSfcStyleContent === "function"
? await Mount.getSfcStyleContent()
: ""
shadowRoot.prepend(await Mount.getStyle({ ...anchor, sfcStyleContent }))
}

if (typeof Mount.getShadowHostId === "function") {
Expand Down
18 changes: 17 additions & 1 deletion cli/plasmo/templates/static/vue3/content-script-ui-mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,30 @@ import { createApp } from "vue"
// @ts-ignore
import RawMount from "__plasmo_mount_content_script__"

// prettier-sort-ignore
// @ts-ignore
import SfcStyleContent from "style-raw:__plasmo_mount_content_script__"

import type {
PlasmoCSUI,
PlasmoCSUIAnchor,
PlasmoCSUIHTMLContainer
} from "~type"

// Escape parcel's static analyzer
const Mount = RawMount.plasmo as PlasmoCSUI<PlasmoCSUIHTMLContainer>
const Mount = (RawMount.plasmo || {}) as PlasmoCSUI<PlasmoCSUIHTMLContainer>

if (typeof SfcStyleContent === "string") {
Mount.getSfcStyleContent = () => SfcStyleContent

if (typeof Mount.getStyle !== "function") {
Mount.getStyle = ({ sfcStyleContent }) => {
const element = document.createElement("style")
element.textContent = sfcStyleContent
return element
}
}
}

const observer = createAnchorObserver(Mount)

Expand Down
1 change: 1 addition & 0 deletions core/parcel-config/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"template:*.vue": ["@plasmohq/parcel-transformer-vue"],
"script:*.vue": ["@plasmohq/parcel-transformer-vue"],
"style:*.vue": ["@plasmohq/parcel-transformer-vue"],
"style-raw:*.vue": ["@plasmohq/parcel-transformer-vue"],
"custom:*.vue": ["@plasmohq/parcel-transformer-vue"],

"*.svelte": ["@plasmohq/parcel-transformer-svelte"],
Expand Down
31 changes: 23 additions & 8 deletions core/parcel-transformer-vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@ async function processPipeline({

return [scriptAsset]
}
case "style": {
case "style":
case "style-raw": {
let cssModules = {}
let assets = await Promise.all(
styles.map(async (style, i) => {
Expand Down Expand Up @@ -408,11 +409,12 @@ async function processPipeline({
return styleAsset
})
)
if (Object.keys(cssModules).length !== 0) {
assets.push({
type: "js",
uniqueKey: asset.id + "-cssModules",
content: `
if (asset.pipeline == "style") {
if (Object.keys(cssModules).length !== 0) {
assets.push({
type: "js",
uniqueKey: asset.id + "-cssModules",
content: `
import {render} from 'template:./${basePath}';
let cssModules = ${JSON.stringify(cssModules)};
${
Expand All @@ -425,9 +427,22 @@ async function processPipeline({
: ""
}
export default cssModules;`
})
})
}
return assets
} else if (asset.pipeline == "style-raw") {
const styleRawString = assets.map((a) => a.content).join("\n")
return [
{
type: "js",
uniqueKey: asset.id + "-cssRawString",
content: `
const styleRawString = \`${styleRawString}\`
export default styleRawString
`
}
]
}
return assets
}
case "custom": {
let toCall = []
Expand Down

0 comments on commit 462e804

Please sign in to comment.