Skip to content

Commit 0318207

Browse files
committed
feat: add custom attrs to <style></style>
1 parent 477c25e commit 0318207

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ This is a fork based on [style-loader](https://github.com/webpack/style-loader).
3030

3131
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.
3232

33+
- **attrs** (4.0.1+):
34+
35+
Type: `object`. Add custom attribute to injected `<style>` tags.
36+
3337
## Differences from `style-loader`
3438

3539
### Server-Side Rendering Support

Diff for: lib/addStylesClient.js

+18-9
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ export default function addStylesClient (parentId, list, _isProduction, _options
5353
isProduction = _isProduction
5454

5555
options = _options || {}
56+
options.attrs = typeof options.attrs === "object" ? options.attrs : {}
5657

5758
var styles = listToStyles(parentId, list)
58-
addStylesToDom(styles)
59+
addStylesToDom(styles, options)
5960

6061
return function update (newList) {
6162
var mayRemove = []
@@ -67,7 +68,7 @@ export default function addStylesClient (parentId, list, _isProduction, _options
6768
}
6869
if (newList) {
6970
styles = listToStyles(parentId, newList)
70-
addStylesToDom(styles)
71+
addStylesToDom(styles, options)
7172
} else {
7273
styles = []
7374
}
@@ -83,7 +84,7 @@ export default function addStylesClient (parentId, list, _isProduction, _options
8384
}
8485
}
8586

86-
function addStylesToDom (styles /* Array<StyleObject> */) {
87+
function addStylesToDom (styles /* Array<StyleObject> */, options) {
8788
for (var i = 0; i < styles.length; i++) {
8889
var item = styles[i]
8990
var domStyle = stylesInDom[item.id]
@@ -93,29 +94,37 @@ function addStylesToDom (styles /* Array<StyleObject> */) {
9394
domStyle.parts[j](item.parts[j])
9495
}
9596
for (; j < item.parts.length; j++) {
96-
domStyle.parts.push(addStyle(item.parts[j]))
97+
domStyle.parts.push(addStyle(item.parts[j], options))
9798
}
9899
if (domStyle.parts.length > item.parts.length) {
99100
domStyle.parts.length = item.parts.length
100101
}
101102
} else {
102103
var parts = []
103104
for (var j = 0; j < item.parts.length; j++) {
104-
parts.push(addStyle(item.parts[j]))
105+
parts.push(addStyle(item.parts[j], options))
105106
}
106107
stylesInDom[item.id] = { id: item.id, refs: 1, parts: parts }
107108
}
108109
}
109110
}
110111

111-
function createStyleElement () {
112+
function createStyleElement (options) {
112113
var styleElement = document.createElement('style')
113114
styleElement.type = 'text/css'
115+
addAttrs(styleElement, options.attrs);
114116
head.appendChild(styleElement)
115117
return styleElement
116118
}
117119

118-
function addStyle (obj /* StyleObjectPart */) {
120+
function addAttrs (el, attrs) {
121+
Object.keys(attrs).forEach(function (key) {
122+
el.setAttribute(key, attrs[key]);
123+
});
124+
}
125+
126+
function addStyle (obj /* StyleObjectPart */, options) {
127+
119128
var update, remove
120129
var styleElement = document.querySelector('style[' + ssrIdKey + '~="' + obj.id + '"]')
121130

@@ -137,12 +146,12 @@ function addStyle (obj /* StyleObjectPart */) {
137146
if (isOldIE) {
138147
// use singleton mode for IE9.
139148
var styleIndex = singletonCounter++
140-
styleElement = singletonElement || (singletonElement = createStyleElement())
149+
styleElement = singletonElement || (singletonElement = createStyleElement(options))
141150
update = applyToSingletonTag.bind(null, styleElement, styleIndex, false)
142151
remove = applyToSingletonTag.bind(null, styleElement, styleIndex, true)
143152
} else {
144153
// use multi-style-tag mode in all other cases
145-
styleElement = createStyleElement()
154+
styleElement = createStyleElement(options)
146155
update = applyToTag.bind(null, styleElement)
147156
remove = function () {
148157
styleElement.parentNode.removeChild(styleElement)

0 commit comments

Comments
 (0)