@@ -10,52 +10,59 @@ export interface StyleLoaderOptions {
10
10
mode : DevOptions [ 'mode' ]
11
11
useMiniCssExtractPlugin : boolean
12
12
loader ?: string
13
+ useShadowDom : boolean
13
14
}
14
15
15
- // function whereToInsertStyleTag(element: HTMLElement) {
16
- // // Function to check if the shadow root exists
17
- // const insertElement = () => {
18
- // // @ts -expect-error - global reference.
19
- // const shadowRoot = window.__EXTENSION_SHADOW_ROOT__
16
+ function whereToInsertStyleTag ( element : HTMLElement ) {
17
+ // Function to insert the style tag
18
+ const insertElement = ( ) => {
19
+ // @ts -expect-error - global reference.
20
+ const shadowRoot = window . __EXTENSION_SHADOW_ROOT__
20
21
21
- // if (shadowRoot) {
22
- // shadowRoot.appendChild(element)
22
+ if ( shadowRoot ) {
23
+ shadowRoot . appendChild ( element )
24
+ console . log ( 'Element inserted into shadowRoot' )
25
+ } else {
26
+ document . head . appendChild ( element )
27
+ console . log ( 'Element inserted into document.head' )
28
+ }
29
+ }
30
+
31
+ // If the shadowRoot is already available, insert immediately
32
+ // @ts -expect-error - global reference.
33
+ if ( window . __EXTENSION_SHADOW_ROOT__ ) {
34
+ insertElement ( )
35
+ return
36
+ }
23
37
24
- // if (process.env.EXTENSION_ENV === 'development') {
25
- // console.log('Element inserted into shadowRoot')
26
- // }
27
- // } else {
28
- // document.head.appendChild(element)
29
- // if (process.env.EXTENSION_ENV === 'development') {
30
- // console.log('Element inserted into document.head')
31
- // }
32
- // }
33
- // }
38
+ // Default behavior if MutationObserver is not required
39
+ if ( ! MutationObserver ) {
40
+ document . head . appendChild ( element )
41
+ return
42
+ }
34
43
35
- // // If the shadowRoot is already available, insert immediately
36
- // // @ts -expect-error - global reference.
37
- // if (window.__EXTENSION_SHADOW_ROOT__) {
38
- // insertElement()
39
- // return
40
- // }
44
+ // Use MutationObserver to wait for the shadow root to be available
45
+ const observer = new MutationObserver ( ( ) => {
46
+ // @ts -expect-error - global reference.
47
+ if ( window . __EXTENSION_SHADOW_ROOT__ ) {
48
+ insertElement ( )
49
+ observer . disconnect ( ) // Stop observing once the shadow root is found
50
+ }
51
+ } )
41
52
42
- // // Use MutationObserver to wait for the shadow root to be available
43
- // const observer = new MutationObserver(() => {
44
- // // @ts -expect-error - global reference.
45
- // if (window.__EXTENSION_SHADOW_ROOT__) {
46
- // insertElement()
47
- // observer.disconnect() // Stop observing once the shadow root is found
48
- // } else {
49
- // // Disconnect the observer if the shadow root is not found after 5 seconds
50
- // setTimeout(() => {
51
- // observer.disconnect()
52
- // }, 5000)
53
- // }
54
- // })
53
+ // Observe changes to the `document.body` or `document.head`
54
+ observer . observe ( document . body , { childList : true , subtree : true } )
55
55
56
- // // Observe changes to the `document.body` or `document.head`
57
- // observer.observe(document.body, {childList: true, subtree: true})
58
- // }
56
+ // Set a timeout to fallback to `document.head` after 5 seconds
57
+ setTimeout ( ( ) => {
58
+ observer . disconnect ( ) // Stop observing after timeout
59
+ // @ts -expect-error - global reference.
60
+ if ( ! window . __EXTENSION_SHADOW_ROOT__ ) {
61
+ document . head . appendChild ( element )
62
+ console . log ( 'Fallback: Element inserted into document.head after timeout' )
63
+ }
64
+ } , 3000 )
65
+ }
59
66
60
67
export async function commonStyleLoaders (
61
68
projectPath : string ,
@@ -67,9 +74,11 @@ export async function commonStyleLoaders(
67
74
? miniCssLoader
68
75
: {
69
76
loader : require . resolve ( 'style-loader' ) ,
70
- // options: {
71
- // insert: whereToInsertStyleTag
72
- // }
77
+ options :
78
+ ( opts . useShadowDom && {
79
+ insert : whereToInsertStyleTag
80
+ } ) ||
81
+ undefined
73
82
} ,
74
83
{
75
84
loader : require . resolve ( 'css-loader' ) ,
0 commit comments