Skip to content

Commit

Permalink
Full shadow DOM support
Browse files Browse the repository at this point in the history
  • Loading branch information
cezaraugusto committed Dec 3, 2024
1 parent fb1b9e1 commit 754e91b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 52 deletions.
93 changes: 51 additions & 42 deletions programs/develop/webpack/plugin-css/common-style-loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,59 @@ export interface StyleLoaderOptions {
mode: DevOptions['mode']
useMiniCssExtractPlugin: boolean
loader?: string
useShadowDom: boolean
}

// function whereToInsertStyleTag(element: HTMLElement) {
// // Function to check if the shadow root exists
// const insertElement = () => {
// // @ts-expect-error - global reference.
// const shadowRoot = window.__EXTENSION_SHADOW_ROOT__
function whereToInsertStyleTag(element: HTMLElement) {
// Function to insert the style tag
const insertElement = () => {
// @ts-expect-error - global reference.
const shadowRoot = window.__EXTENSION_SHADOW_ROOT__

// if (shadowRoot) {
// shadowRoot.appendChild(element)
if (shadowRoot) {
shadowRoot.appendChild(element)
console.log('Element inserted into shadowRoot')
} else {
document.head.appendChild(element)
console.log('Element inserted into document.head')
}
}

// If the shadowRoot is already available, insert immediately
// @ts-expect-error - global reference.
if (window.__EXTENSION_SHADOW_ROOT__) {
insertElement()
return
}

// if (process.env.EXTENSION_ENV === 'development') {
// console.log('Element inserted into shadowRoot')
// }
// } else {
// document.head.appendChild(element)
// if (process.env.EXTENSION_ENV === 'development') {
// console.log('Element inserted into document.head')
// }
// }
// }
// Default behavior if MutationObserver is not required
if (!MutationObserver) {
document.head.appendChild(element)
return
}

// // If the shadowRoot is already available, insert immediately
// // @ts-expect-error - global reference.
// if (window.__EXTENSION_SHADOW_ROOT__) {
// insertElement()
// return
// }
// Use MutationObserver to wait for the shadow root to be available
const observer = new MutationObserver(() => {
// @ts-expect-error - global reference.
if (window.__EXTENSION_SHADOW_ROOT__) {
insertElement()
observer.disconnect() // Stop observing once the shadow root is found
}
})

// // Use MutationObserver to wait for the shadow root to be available
// const observer = new MutationObserver(() => {
// // @ts-expect-error - global reference.
// if (window.__EXTENSION_SHADOW_ROOT__) {
// insertElement()
// observer.disconnect() // Stop observing once the shadow root is found
// } else {
// // Disconnect the observer if the shadow root is not found after 5 seconds
// setTimeout(() => {
// observer.disconnect()
// }, 5000)
// }
// })
// Observe changes to the `document.body` or `document.head`
observer.observe(document.body, {childList: true, subtree: true})

// // Observe changes to the `document.body` or `document.head`
// observer.observe(document.body, {childList: true, subtree: true})
// }
// Set a timeout to fallback to `document.head` after 5 seconds
setTimeout(() => {
observer.disconnect() // Stop observing after timeout
// @ts-expect-error - global reference.
if (!window.__EXTENSION_SHADOW_ROOT__) {
document.head.appendChild(element)
console.log('Fallback: Element inserted into document.head after timeout')
}
}, 3000)
}

export async function commonStyleLoaders(
projectPath: string,
Expand All @@ -67,9 +74,11 @@ export async function commonStyleLoaders(
? miniCssLoader
: {
loader: require.resolve('style-loader'),
// options: {
// insert: whereToInsertStyleTag
// }
options:
(opts.useShadowDom && {
insert: whereToInsertStyleTag
}) ||
undefined
},
{
loader: require.resolve('css-loader'),
Expand Down
6 changes: 4 additions & 2 deletions programs/develop/webpack/plugin-css/css-tools/less.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ export async function maybeUseLess(
use: await commonStyleLoaders(projectPath, {
loader: 'less-loader',
mode,
useMiniCssExtractPlugin: false
useMiniCssExtractPlugin: false,
useShadowDom: true
})
},
{
use: await commonStyleLoaders(projectPath, {
loader: 'less-loader',
mode,
useMiniCssExtractPlugin: mode === 'production'
useMiniCssExtractPlugin: mode === 'production',
useShadowDom: false
})
}
]
Expand Down
12 changes: 8 additions & 4 deletions programs/develop/webpack/plugin-css/css-tools/sass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,16 @@ export async function maybeUseSass(
use: await commonStyleLoaders(projectPath, {
loader: 'sass-loader',
mode,
useMiniCssExtractPlugin: false
useMiniCssExtractPlugin: false,
useShadowDom: true
})
},
{
use: await commonStyleLoaders(projectPath, {
loader: 'sass-loader',
mode,
useMiniCssExtractPlugin: mode === 'production'
useMiniCssExtractPlugin: mode === 'production',
useShadowDom: true
})
}
]
Expand All @@ -94,14 +96,16 @@ export async function maybeUseSass(
use: await commonStyleLoaders(projectPath, {
loader: 'sass-loader',
mode,
useMiniCssExtractPlugin: false
useMiniCssExtractPlugin: false,
useShadowDom: true
})
},
{
use: await commonStyleLoaders(projectPath, {
loader: 'sass-loader',
mode,
useMiniCssExtractPlugin: mode === 'production'
useMiniCssExtractPlugin: mode === 'production',
useShadowDom: true
})
}
]
Expand Down
12 changes: 8 additions & 4 deletions programs/develop/webpack/plugin-css/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ export class CssPlugin {
resourceQuery: /inline_style/,
use: await commonStyleLoaders(projectPath, {
mode: mode as 'development' | 'production',
useMiniCssExtractPlugin: false
useMiniCssExtractPlugin: false,
useShadowDom: true
})
},
{
use: await commonStyleLoaders(projectPath, {
mode: mode as 'development' | 'production',
useMiniCssExtractPlugin: mode === 'production'
useMiniCssExtractPlugin: mode === 'production',
useShadowDom: false
})
}
]
Expand All @@ -58,13 +60,15 @@ export class CssPlugin {
resourceQuery: /inline_style/,
use: await commonStyleLoaders(projectPath, {
mode: mode as 'development' | 'production',
useMiniCssExtractPlugin: false
useMiniCssExtractPlugin: false,
useShadowDom: true
})
},
{
use: await commonStyleLoaders(projectPath, {
mode: mode as 'development' | 'production',
useMiniCssExtractPlugin: mode === 'production'
useMiniCssExtractPlugin: mode === 'production',
useShadowDom: false
})
}
]
Expand Down

0 comments on commit 754e91b

Please sign in to comment.