Skip to content

Commit 5464138

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

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This is a fork based on [style-loader](https://github.com/webpack/style-loader).
88

99
Type: `boolean`. When importing the style from a non-vue-file, by default the style is injected as a side effect of the import. When `manualInject` is true, the imported style object exposes a `__inject__` method, which can then be called manually at appropriate timing. If called on the server, the method expects one argument which is the `ssrContext` to attach styles to.
1010

11-
``` js
11+
```js
1212
import styles from 'styles.scss'
1313

1414
export default {
@@ -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+
- **attributes** (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

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.attributes = typeof options.attributes === "object" ? options.attributes : {}
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+
addAttributes(styleElement, options.attributes);
114116
head.appendChild(styleElement)
115117
return styleElement
116118
}
117119

118-
function addStyle (obj /* StyleObjectPart */) {
120+
function addAttributes (el, attributes) {
121+
Object.keys(attributes).forEach(function (key) {
122+
el.setAttribute(key, attributes[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)