Skip to content

Commit 2bbdf25

Browse files
committed
feat: adjust shadow injection strategy
1 parent d328131 commit 2bbdf25

File tree

2 files changed

+36
-48
lines changed

2 files changed

+36
-48
lines changed

lib/loader.js

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,12 @@ module.exports = function (content) {
155155
// add requires for styles
156156
let cssModules
157157
if (parts.styles.length) {
158-
const needsLazyInjection = isServer || isShadowMode
158+
const needsExplicitInjection = isServer || isShadowMode
159159
let styleInjectionCode = 'function injectStyle (context) {\n'
160160
if (needsHotReload) {
161161
styleInjectionCode += ` if (disposed) return\n`
162162
}
163-
if (needsLazyInjection) {
163+
if (needsExplicitInjection) {
164164
styleInjectionCode += `var i\n`
165165
}
166166
parts.styles.forEach((style, i) => {
@@ -174,7 +174,7 @@ module.exports = function (content) {
174174
// vue-style-loader exposes inject functions in SSR or shadow mode so they
175175
// are always called
176176
const invokeStyle =
177-
needsLazyInjection && hasVueStyleLoader
177+
needsExplicitInjection && hasVueStyleLoader
178178
? code => `;(i=${code},i.__inject__&&i.__inject__(context),i)\n`
179179
: code => ` ${code}\n`
180180

@@ -241,27 +241,6 @@ module.exports = function (content) {
241241
output += styleInjectionCode
242242
}
243243

244-
// we require the component normalizer function, and call it like so:
245-
// normalizeComponent(
246-
// scriptExports,
247-
// compiledTemplate,
248-
// functionalTemplate,
249-
// injectStyles,
250-
// scopeId,
251-
// moduleIdentifier (server only)
252-
// )
253-
const componentNormalizerRequest = loaderUtils.stringifyRequest(
254-
loaderContext,
255-
'!' + componentNormalizerPath
256-
)
257-
if (!options.esModule) {
258-
output +=
259-
`var normalizeComponent = require(${componentNormalizerRequest}).default\n`
260-
} else {
261-
output +=
262-
`import normalizeComponent from ${componentNormalizerRequest}\n`
263-
}
264-
265244
// <script>
266245
output += '/* script */\n'
267246
const script = parts.script
@@ -322,12 +301,11 @@ module.exports = function (content) {
322301

323302
// style
324303
// the injection function is passed to the normalizer and injected into
325-
// component lifecycle hooks. In shadow mode, we expose the inject function
326-
// directly on the component's options so we don't pass it here.
304+
// component lifecycle hooks.
327305
output += '/* styles */\n'
328306
output +=
329307
'var __vue_styles__ = ' +
330-
(parts.styles.length && !isShadowMode ? 'injectStyle' : 'null') +
308+
(parts.styles.length ? 'injectStyle' : 'null') +
331309
'\n'
332310

333311
// scopeId
@@ -344,7 +322,27 @@ module.exports = function (content) {
344322
(isServer ? JSON.stringify(hash(this.request)) : 'null') +
345323
'\n'
346324

347-
// close normalizeComponent call
325+
// we require the component normalizer function, and call it like so:
326+
// normalizeComponent(
327+
// scriptExports,
328+
// compiledTemplate,
329+
// functionalTemplate,
330+
// injectStyles,
331+
// scopeId,
332+
// moduleIdentifier, (server only)
333+
// isShadowMode (vue-cli only)
334+
// )
335+
const componentNormalizerRequest = loaderUtils.stringifyRequest(
336+
loaderContext,
337+
'!' + componentNormalizerPath
338+
)
339+
if (!options.esModule) {
340+
output +=
341+
`var normalizeComponent = require(${componentNormalizerRequest}).default\n`
342+
} else {
343+
output +=
344+
`import normalizeComponent from ${componentNormalizerRequest}\n`
345+
}
348346
output +=
349347
'var Component = normalizeComponent(\n' +
350348
' __vue_script__,\n' +
@@ -353,26 +351,9 @@ module.exports = function (content) {
353351
' __vue_styles__,\n' +
354352
' __vue_scopeId__,\n' +
355353
' __vue_module_identifier__\n' +
354+
(isShadowMode ? `,true` : ``) +
356355
')\n'
357356

358-
// expose style injection on options
359-
// this will be used by vue-cli's web component entry
360-
if (isShadowMode) {
361-
// the shadow inject id will be the module id of the entry Vue component,
362-
// which is the frist Vue component encountered in this compilation
363-
let shadowInjectId = process.env.VUE_SHADOW_INJECT_ID
364-
if (!shadowInjectId) {
365-
shadowInjectId = process.env.VUE_SHADOW_INJECT_ID =
366-
`__vue_shadow_injector__${moduleId.replace(`data-v-`, '')}`
367-
}
368-
output +=
369-
`var shadowInjectId = Component.options.__shadowInjectId = '${shadowInjectId}'\n` +
370-
`var injectors = window[shadowInjectId] || (window[shadowInjectId] = [])\n`
371-
if (parts.styles.length) {
372-
output += `injectors.push(injectStyle)`
373-
}
374-
}
375-
376357
// development-only code
377358
if (!isProduction) {
378359
// add filename in dev

lib/runtime/component-normalizer.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export default function normalizeComponent (
1010
functionalTemplate,
1111
injectStyles,
1212
scopeId,
13-
moduleIdentifier /* server only */
13+
moduleIdentifier, /* server only */
14+
shadowMode /* vue-cli only */
1415
) {
1516
var esModule
1617
var scriptExports = rawScriptExports = rawScriptExports || {}
@@ -69,7 +70,13 @@ export default function normalizeComponent (
6970
// never gets called
7071
options._ssrRegister = hook
7172
} else if (injectStyles) {
72-
hook = injectStyles
73+
if (shadowMode) {
74+
hook = function () {
75+
injectStyles(this.$root.$options.shadowRoot)
76+
}
77+
} else {
78+
hook = injectStyles
79+
}
7380
}
7481

7582
if (hook) {

0 commit comments

Comments
 (0)