Skip to content

Commit

Permalink
feat: add custom attrs to <style></style>
Browse files Browse the repository at this point in the history
  • Loading branch information
ywmail committed Mar 14, 2019
1 parent 477c25e commit 0318207
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ This is a fork based on [style-loader](https://github.com/webpack/style-loader).

Type: `boolean`. Add `data-vue-ssr-id` attribute to injected `<style>` tags even when not in Node.js. This can be used with pre-rendering (instead of SSR) to avoid duplicate style injection on hydration.

- **attrs** (4.0.1+):

Type: `object`. Add custom attribute to injected `<style>` tags.

## Differences from `style-loader`

### Server-Side Rendering Support
Expand Down
27 changes: 18 additions & 9 deletions lib/addStylesClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ export default function addStylesClient (parentId, list, _isProduction, _options
isProduction = _isProduction

options = _options || {}
options.attrs = typeof options.attrs === "object" ? options.attrs : {}

var styles = listToStyles(parentId, list)
addStylesToDom(styles)
addStylesToDom(styles, options)

return function update (newList) {
var mayRemove = []
Expand All @@ -67,7 +68,7 @@ export default function addStylesClient (parentId, list, _isProduction, _options
}
if (newList) {
styles = listToStyles(parentId, newList)
addStylesToDom(styles)
addStylesToDom(styles, options)
} else {
styles = []
}
Expand All @@ -83,7 +84,7 @@ export default function addStylesClient (parentId, list, _isProduction, _options
}
}

function addStylesToDom (styles /* Array<StyleObject> */) {
function addStylesToDom (styles /* Array<StyleObject> */, options) {
for (var i = 0; i < styles.length; i++) {
var item = styles[i]
var domStyle = stylesInDom[item.id]
Expand All @@ -93,29 +94,37 @@ function addStylesToDom (styles /* Array<StyleObject> */) {
domStyle.parts[j](item.parts[j])
}
for (; j < item.parts.length; j++) {
domStyle.parts.push(addStyle(item.parts[j]))
domStyle.parts.push(addStyle(item.parts[j], options))
}
if (domStyle.parts.length > item.parts.length) {
domStyle.parts.length = item.parts.length
}
} else {
var parts = []
for (var j = 0; j < item.parts.length; j++) {
parts.push(addStyle(item.parts[j]))
parts.push(addStyle(item.parts[j], options))
}
stylesInDom[item.id] = { id: item.id, refs: 1, parts: parts }
}
}
}

function createStyleElement () {
function createStyleElement (options) {
var styleElement = document.createElement('style')
styleElement.type = 'text/css'
addAttrs(styleElement, options.attrs);
head.appendChild(styleElement)
return styleElement
}

function addStyle (obj /* StyleObjectPart */) {
function addAttrs (el, attrs) {
Object.keys(attrs).forEach(function (key) {
el.setAttribute(key, attrs[key]);
});
}

function addStyle (obj /* StyleObjectPart */, options) {

var update, remove
var styleElement = document.querySelector('style[' + ssrIdKey + '~="' + obj.id + '"]')

Expand All @@ -137,12 +146,12 @@ function addStyle (obj /* StyleObjectPart */) {
if (isOldIE) {
// use singleton mode for IE9.
var styleIndex = singletonCounter++
styleElement = singletonElement || (singletonElement = createStyleElement())
styleElement = singletonElement || (singletonElement = createStyleElement(options))
update = applyToSingletonTag.bind(null, styleElement, styleIndex, false)
remove = applyToSingletonTag.bind(null, styleElement, styleIndex, true)
} else {
// use multi-style-tag mode in all other cases
styleElement = createStyleElement()
styleElement = createStyleElement(options)
update = applyToTag.bind(null, styleElement)
remove = function () {
styleElement.parentNode.removeChild(styleElement)
Expand Down

0 comments on commit 0318207

Please sign in to comment.