|
| 1 | +const hotReloadAPIPath = require.resolve('vue-hot-reload-api') |
| 2 | + |
| 3 | +module.exports = function genStyleInjectionCode ( |
| 4 | + loaderContext, |
| 5 | + styles, |
| 6 | + id, |
| 7 | + resourcePath, |
| 8 | + stringifyRequest, |
| 9 | + getLangQuery, |
| 10 | + needsHotReload, |
| 11 | + needsExplicitInjection |
| 12 | +) { |
| 13 | + let styleImportsCode = `` |
| 14 | + let styleInjectionCode = `` |
| 15 | + let cssModulesHotReloadCode = `` |
| 16 | + |
| 17 | + const hasCSSModules = false |
| 18 | + const cssModuleNames = new Map() |
| 19 | + |
| 20 | + function genStyleRequest (style, i) { |
| 21 | + const src = style.src || resourcePath |
| 22 | + const langQuery = getLangQuery(style, 'css') |
| 23 | + const scopedQuery = style.scoped ? `&scoped&id=${id}` : `` |
| 24 | + const query = `?vue&type=style&index=${i}${langQuery}${scopedQuery}` |
| 25 | + return stringifyRequest(src + query) |
| 26 | + } |
| 27 | + |
| 28 | + function genCSSModulesCode (style, request, i) { |
| 29 | + const moduleName = style.module === true ? '$style' : style.module |
| 30 | + if (cssModuleNames.has(moduleName)) { |
| 31 | + loaderContext.emitError(`CSS module name ${moduleName} is not unique!`) |
| 32 | + } |
| 33 | + cssModuleNames.set(moduleName, true) |
| 34 | + |
| 35 | + // `(vue-)style-loader` exports the name-to-hash map directly |
| 36 | + // `css-loader` exports it in `.locals` |
| 37 | + const locals = `(style${i}.locals || style${i})` |
| 38 | + const name = JSON.stringify(moduleName) |
| 39 | + |
| 40 | + if (!needsHotReload) { |
| 41 | + styleInjectionCode += `this[${name}] = ${locals}` |
| 42 | + } else { |
| 43 | + styleInjectionCode += ` |
| 44 | + cssModules[${name}] = ${locals} |
| 45 | + Object.defineProperty(this, ${name}, { |
| 46 | + get: function () { |
| 47 | + return cssModules[${name}] |
| 48 | + } |
| 49 | + }) |
| 50 | + ` |
| 51 | + cssModulesHotReloadCode += ` |
| 52 | + module.hot && module.hot.accept([${request}], function () { |
| 53 | + var oldLocals = cssModules[${name}] |
| 54 | + if (oldLocals) { |
| 55 | + var newLocals = require(${request}) |
| 56 | + if (JSON.stringify(newLocals) !== JSON.stringify(oldLocals)) { |
| 57 | + cssModules[${name}] = newLocals |
| 58 | + require("${hotReloadAPIPath}").rerender("${id}") |
| 59 | + } |
| 60 | + } |
| 61 | + }) |
| 62 | + ` |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // explicit injection is needed in SSR (for critical CSS collection) |
| 67 | + // or in Shadow Mode (for injection into shadow root) |
| 68 | + // In these modes, vue-style-loader exports objects with the __inject__ |
| 69 | + // method; otherwise we simply import the styles. |
| 70 | + if (!needsExplicitInjection) { |
| 71 | + styles.forEach((style, i) => { |
| 72 | + const request = genStyleRequest(style, i) |
| 73 | + styleImportsCode += `import style${i} from ${request}\n` |
| 74 | + if (style.module) genCSSModulesCode(style, request, i) |
| 75 | + }) |
| 76 | + } else { |
| 77 | + styles.forEach((style, i) => { |
| 78 | + const request = genStyleRequest(style, i) |
| 79 | + styleInjectionCode += ( |
| 80 | + `var style${i} = require(${request})\n` + |
| 81 | + `if (style${i}.__inject__) style${i}.__inject__(context)\n` |
| 82 | + ) |
| 83 | + if (style.module) genCSSModulesCode(style, request, i) |
| 84 | + }) |
| 85 | + } |
| 86 | + |
| 87 | + if (!needsExplicitInjection && !hasCSSModules) { |
| 88 | + return styleImportsCode |
| 89 | + } |
| 90 | + |
| 91 | + return ` |
| 92 | +${styleImportsCode} |
| 93 | +${hasCSSModules && needsHotReload ? `var cssModules = {}` : ``} |
| 94 | +${needsHotReload ? `var disposed = false` : ``} |
| 95 | +
|
| 96 | +function injectStyles (context) { |
| 97 | + ${needsHotReload ? `if (disposed) return` : ``} |
| 98 | + ${styleInjectionCode} |
| 99 | +} |
| 100 | +
|
| 101 | +${needsHotReload ? ` |
| 102 | + module.hot && module.hot.dispose(function (data) { |
| 103 | + disposed = true |
| 104 | + }) |
| 105 | +` : ``} |
| 106 | +
|
| 107 | +${cssModulesHotReloadCode} |
| 108 | + `.trim() |
| 109 | +} |
0 commit comments