-
Notifications
You must be signed in to change notification settings - Fork 907
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
135 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = function genCustomBlocksCode () { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
const hotReloadAPIPath = require.resolve('vue-hot-reload-api') | ||
|
||
module.exports = function genStyleInjectionCode ( | ||
loaderContext, | ||
styles, | ||
id, | ||
resourcePath, | ||
stringifyRequest, | ||
getLangQuery, | ||
needsHotReload, | ||
needsExplicitInjection | ||
) { | ||
let styleImportsCode = `` | ||
let styleInjectionCode = `` | ||
let cssModulesHotReloadCode = `` | ||
|
||
const hasCSSModules = false | ||
const cssModuleNames = new Map() | ||
|
||
function genStyleRequest (style, i) { | ||
const src = style.src || resourcePath | ||
const langQuery = getLangQuery(style, 'css') | ||
const scopedQuery = style.scoped ? `&scoped&id=${id}` : `` | ||
const query = `?vue&type=style&index=${i}${langQuery}${scopedQuery}` | ||
return stringifyRequest(src + query) | ||
} | ||
|
||
function genCSSModulesCode (style, request, i) { | ||
const moduleName = style.module === true ? '$style' : style.module | ||
if (cssModuleNames.has(moduleName)) { | ||
loaderContext.emitError(`CSS module name ${moduleName} is not unique!`) | ||
} | ||
cssModuleNames.set(moduleName, true) | ||
|
||
// `(vue-)style-loader` exports the name-to-hash map directly | ||
// `css-loader` exports it in `.locals` | ||
const locals = `(style${i}.locals || style${i})` | ||
const name = JSON.stringify(moduleName) | ||
|
||
if (!needsHotReload) { | ||
styleInjectionCode += `this[${name}] = ${locals}` | ||
} else { | ||
styleInjectionCode += ` | ||
cssModules[${name}] = ${locals} | ||
Object.defineProperty(this, ${name}, { | ||
get: function () { | ||
return cssModules[${name}] | ||
} | ||
}) | ||
` | ||
cssModulesHotReloadCode += ` | ||
module.hot && module.hot.accept([${request}], function () { | ||
var oldLocals = cssModules[${name}] | ||
if (oldLocals) { | ||
var newLocals = require(${request}) | ||
if (JSON.stringify(newLocals) !== JSON.stringify(oldLocals)) { | ||
cssModules[${name}] = newLocals | ||
require("${hotReloadAPIPath}").rerender("${id}") | ||
} | ||
} | ||
}) | ||
` | ||
} | ||
} | ||
|
||
// explicit injection is needed in SSR (for critical CSS collection) | ||
// or in Shadow Mode (for injection into shadow root) | ||
// In these modes, vue-style-loader exports objects with the __inject__ | ||
// method; otherwise we simply import the styles. | ||
if (!needsExplicitInjection) { | ||
styles.forEach((style, i) => { | ||
const request = genStyleRequest(style, i) | ||
styleImportsCode += `import style${i} from ${request}\n` | ||
if (style.module) genCSSModulesCode(style, request, i) | ||
}) | ||
} else { | ||
styles.forEach((style, i) => { | ||
const request = genStyleRequest(style, i) | ||
styleInjectionCode += ( | ||
`var style${i} = require(${request})\n` + | ||
`if (style${i}.__inject__) style${i}.__inject__(context)\n` | ||
) | ||
if (style.module) genCSSModulesCode(style, request, i) | ||
}) | ||
} | ||
|
||
if (!needsExplicitInjection && !hasCSSModules) { | ||
return styleImportsCode | ||
} | ||
|
||
return ` | ||
${styleImportsCode} | ||
${hasCSSModules && needsHotReload ? `var cssModules = {}` : ``} | ||
${needsHotReload ? `var disposed = false` : ``} | ||
function injectStyles (context) { | ||
${needsHotReload ? `if (disposed) return` : ``} | ||
${styleInjectionCode} | ||
} | ||
${needsHotReload ? ` | ||
module.hot && module.hot.dispose(function (data) { | ||
disposed = true | ||
}) | ||
` : ``} | ||
${cssModulesHotReloadCode} | ||
`.trim() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters