Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add custom attrs to <style></style> #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This is a fork based on [style-loader](https://github.com/webpack/style-loader).

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.

``` js
```js
import styles from 'styles.scss'

export default {
Expand All @@ -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.

- **attributes** (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.attributes = typeof options.attributes === "object" ? options.attributes : {}

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'
addAttributes(styleElement, options.attributes);
head.appendChild(styleElement)
return styleElement
}

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