@@ -26,9 +26,11 @@ export interface AssembleResults {
26
26
}
27
27
28
28
export interface AssembleOptions {
29
+ isWebComponent ?: boolean
29
30
normalizer ?: string
30
31
styleInjector ?: string
31
32
styleInjectorSSR ?: string
33
+ styleInjectorShadow ?: string
32
34
}
33
35
34
36
export function assemble (
@@ -214,11 +216,52 @@ export function assembleFromSource(
214
216
? createImport ( '__vue_create_injector_ssr__' , options . styleInjectorSSR )
215
217
: inlineCreateInjectorSSR
216
218
219
+ const inlineCreateInjectorShadow = `function __vue_create_injector_shadow__(__, shadowRoot) {
220
+ function createStyleElement(shadowRoot) {
221
+ var styleElement = document.createElement('style')
222
+ styleElement.type = 'text/css'
223
+ shadowRoot.appendChild(styleElement)
224
+
225
+ return styleElement
226
+ }
227
+
228
+ return function addStyle(id, css) {
229
+ const styleElement = createStyleElement(shadowRoot)
230
+ if (css.media) styleElement.setAttribute('media', css.media)
231
+
232
+ let code = css.source
233
+
234
+ if (${ e ( compiler . template . isProduction ) } && css.map) {
235
+ // https://developer.chrome.com/devtools/docs/javascript-debugging
236
+ // this makes source maps inside style tags work properly in Chrome
237
+ code += '\\n/*# sourceURL=' + css.map.sources[0] + ' */'
238
+ // http://stackoverflow.com/a/26603875
239
+ code +=
240
+ '\\n/*# sourceMappingURL=data:application/json;base64,' +
241
+ btoa(unescape(encodeURIComponent(JSON.stringify(css.map)))) +
242
+ ' */'
243
+ }
244
+
245
+ if ('styleSheet' in styleElement) {
246
+ styleElement.styleSheet.cssText = code
247
+ } else {
248
+ while (styleElement.firstChild) {
249
+ styleElement.removeChild(styleElement.firstChild)
250
+ }
251
+ styleElement.appendChild(document.createTextNode(code))
252
+ }
253
+ }
254
+ }`
255
+
256
+ const createInjectorShadow = options . styleInjectorShadow
257
+ ? createImport ( '__vue_create_injector_shadow__' , options . styleInjectorShadow )
258
+ : inlineCreateInjectorShadow
259
+
217
260
// language=JavaScript
218
261
const inlineNormalizeComponent = `function __vue_normalize__(
219
262
template, style, script,
220
- scope, functional, moduleIdentifier,
221
- createInjector, createInjectorSSR
263
+ scope, functional, moduleIdentifier, shadowMode,
264
+ createInjector, createInjectorSSR, createInjectorShadow
222
265
) {
223
266
const component = (typeof script === 'function' ? script.options : script) || {}
224
267
@@ -263,9 +306,13 @@ export function assembleFromSource(
263
306
component._ssrRegister = hook
264
307
}
265
308
else if (style) {
266
- hook = function(context) {
267
- style.call(this, createInjector(context))
268
- }
309
+ hook = shadowMode
310
+ ? function(context) {
311
+ style.call(this, createInjectorShadow(context, this.$root.$options.shadowRoot))
312
+ }
313
+ : function(context) {
314
+ style.call(this, createInjector(context))
315
+ }
269
316
}
270
317
271
318
if (hook !== undefined) {
@@ -345,9 +392,11 @@ export function assembleFromSource(
345
392
/* component normalizer */
346
393
${ normalizeComponent }
347
394
/* style inject */
348
- ${ hasStyle && ! compiler . template . optimizeSSR ? createInjector : '' }
395
+ ${ hasStyle && ! compiler . template . optimizeSSR && ! options . isWebComponent ? createInjector : '' }
349
396
/* style inject SSR */
350
397
${ hasStyle && compiler . template . optimizeSSR ? createInjectorSSR : '' }
398
+ /* style inject shadow dom */
399
+ ${ hasStyle && options . isWebComponent ? createInjectorShadow : '' }
351
400
352
401
`
353
402
@@ -403,6 +452,7 @@ export function assembleFromSource(
403
452
__vue_scope_id__,
404
453
__vue_is_functional_template__,
405
454
__vue_module_identifier__,
455
+ ${ options . isWebComponent ? 'true' : 'false' } ,
406
456
${
407
457
code . indexOf ( '__vue_create_injector__' ) > - 1
408
458
? '__vue_create_injector__'
@@ -412,6 +462,11 @@ export function assembleFromSource(
412
462
code . indexOf ( '__vue_create_injector_ssr__' ) > - 1
413
463
? '__vue_create_injector_ssr__'
414
464
: 'undefined'
465
+ } ,
466
+ ${
467
+ code . indexOf ( '__vue_create_injector_shadow__' ) > - 1
468
+ ? '__vue_create_injector_shadow__'
469
+ : 'undefined'
415
470
}
416
471
)`
417
472
0 commit comments