Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic shadow-dom support #226

Merged
merged 6 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ __TEST__
/playwright-report/
/blob-report/
/playwright/.cache/
/e2e-report/
/e2e-report/
_examples
2,928 changes: 2,784 additions & 144 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions programs/cli/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ interface ImportMetaEnv {
interface ImportMeta {
readonly env: ImportMetaEnv
}

interface Window {
__EXTENSION_SHADOW_ROOT__: ShadowRoot
}
55 changes: 51 additions & 4 deletions programs/develop/webpack/plugin-css/common-style-loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,58 @@ import {isUsingTailwind} from './css-tools/tailwind'
import {isUsingSass} from './css-tools/sass'
import {isUsingLess} from './css-tools/less'
import {maybeUsePostCss} from './css-tools/postcss'
import {isUsingVue} from '../plugin-js-frameworks/js-tools/vue'

export interface StyleLoaderOptions {
mode: DevOptions['mode']
useMiniCssExtractPlugin: boolean
loader?: string
}

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__

if (shadowRoot) {
shadowRoot.appendChild(element)

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')
}
}
}

// 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
} 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})
}

export async function commonStyleLoaders(
projectPath: string,
opts: StyleLoaderOptions
Expand All @@ -21,9 +65,12 @@ export async function commonStyleLoaders(
const styleLoaders: RuleSetRule['use'] = [
opts.useMiniCssExtractPlugin
? miniCssLoader
: isUsingVue(projectPath)
? require.resolve('vue-style-loader')
: require.resolve('style-loader'),
: {
loader: require.resolve('style-loader'),
options: {
insert: whereToInsertStyleTag
}
},
{
loader: require.resolve('css-loader'),
options: {
Expand Down
2 changes: 1 addition & 1 deletion programs/develop/webpack/plugin-css/css-tools/less.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export async function maybeUseLess(
test: /\.less$/,
oneOf: [
{
resourceQuery: /is_content_css_import=true/,
resourceQuery: /inline_style/,
use: await commonStyleLoaders(projectPath, {
loader: 'less-loader',
mode,
Expand Down
4 changes: 2 additions & 2 deletions programs/develop/webpack/plugin-css/css-tools/sass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function maybeUseSass(
exclude: /\.module\.(s(a|c)ss)$/,
oneOf: [
{
resourceQuery: /is_content_css_import=true/,
resourceQuery: /inline_style/,
use: await commonStyleLoaders(projectPath, {
loader: 'sass-loader',
mode,
Expand All @@ -90,7 +90,7 @@ export async function maybeUseSass(
test: /\.module\.(s(a|c)ss)$/,
oneOf: [
{
resourceQuery: /is_content_css_import=true/,
resourceQuery: /inline_style/,
use: await commonStyleLoaders(projectPath, {
loader: 'sass-loader',
mode,
Expand Down
14 changes: 14 additions & 0 deletions programs/develop/webpack/plugin-css/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ export class CssPlugin {
test: /\.css$/,
exclude: /\.module\.css$/,
oneOf: [
{
resourceQuery: /inline_style/,
use: await commonStyleLoaders(projectPath, {
mode: mode as 'development' | 'production',
useMiniCssExtractPlugin: false
})
},
{
use: await commonStyleLoaders(projectPath, {
mode: mode as 'development' | 'production',
Expand All @@ -47,6 +54,13 @@ export class CssPlugin {
{
test: /\.module\.css$/,
oneOf: [
{
resourceQuery: /inline_style/,
use: await commonStyleLoaders(projectPath, {
mode: mode as 'development' | 'production',
useMiniCssExtractPlugin: false
})
},
{
use: await commonStyleLoaders(projectPath, {
mode: mode as 'development' | 'production',
Expand Down