Skip to content

Commit 995f3de

Browse files
felixmoshjoshwiens
authored andcommitted
feat(tag-attribute): Add support for custom tag attribute
1 parent 2a2f261 commit 995f3de

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ By default, the style-loader appends `<style>` elements to the end of the `<head
7272

7373
If defined, the style-loader will re-use a single `<style>` element, instead of adding/removing individual elements for each required module. **Note:** this option is on by default in IE9, which has strict limitations on the number of style tags allowed on a page. You can enable or disable it with the singleton query parameter (`?singleton` or `?-singleton`).
7474

75+
#### `attrs`
76+
77+
If defined, style-loader will attach given attributes with their values on `<style>` / `<link>` element.
78+
Usage:
79+
```javascript
80+
require('style-loader?{attrs:{id: "style-tag-id"}}!style.scss');
81+
82+
// will create style tag <style id="style-tag-id">
83+
```
84+
7585
### Recommended configuration
7686

7787
By convention the reference-counted API should be bound to `.useable.css` and the simple API to `.css` (similar to other file types, i.e. `.useable.less` and `.less`).
@@ -92,7 +102,7 @@ So the recommended configuration for webpack is:
92102
{
93103
test: /\.useable\.css$/,
94104
use: [
95-
{
105+
{
96106
loader: "style-loader",
97107
options: {
98108
useable: true

addStyles.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ module.exports = function(list, options) {
2626
}
2727

2828
options = options || {};
29+
options.attrs = typeof options.attrs === "object" ? options.attrs : {};
30+
2931
// Force single-tag solution on IE6-9, which has a hard limit on the # of <style>
3032
// tags it will allow on a page
3133
if (typeof options.singleton === "undefined") options.singleton = isOldIE();
@@ -128,19 +130,29 @@ function removeStyleElement(styleElement) {
128130

129131
function createStyleElement(options) {
130132
var styleElement = document.createElement("style");
131-
styleElement.type = "text/css";
133+
options.attrs.type = "text/css";
134+
135+
attachTagAttrs(styleElement, options.attrs);
132136
insertStyleElement(options, styleElement);
133137
return styleElement;
134138
}
135139

136140
function createLinkElement(options) {
137141
var linkElement = document.createElement("link");
138-
linkElement.rel = "stylesheet";
139-
linkElement.type = "text/css";
142+
options.attrs.type = "text/css";
143+
options.attrs.rel = "stylesheet";
144+
145+
attachTagAttrs(linkElement, options.attrs);
140146
insertStyleElement(options, linkElement);
141147
return linkElement;
142148
}
143149

150+
function attachTagAttrs(element, attrs) {
151+
Object.keys(attrs).forEach(function (key) {
152+
element.setAttribute(key, attrs[key]);
153+
});
154+
}
155+
144156
function addStyle(obj, options) {
145157
var styleElement, update, remove;
146158

test/basicTest.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe("basic tests", function() {
4545
},
4646
module: {
4747
rules: [cssRule]
48-
}
48+
}
4949
};
5050

5151
beforeEach(function() {
@@ -102,6 +102,26 @@ describe("basic tests", function() {
102102
runCompilerTest(expected, done);
103103
}); // it singleton
104104

105+
it("attrs", function(done) {
106+
// Setup
107+
styleLoaderOptions.attrs = {id: 'style-tag-id'};
108+
109+
fs.writeFileSync(
110+
rootDir + "main.js",
111+
[
112+
"var a = require('./style.css');"
113+
].join("\n")
114+
);
115+
116+
// Run
117+
let expected = [
118+
existingStyle,
119+
`<style id="${styleLoaderOptions.attrs.id}" type="text/css">${requiredCss}</style>`
120+
].join("\n");
121+
122+
runCompilerTest(expected, done);
123+
}); // it attrs
124+
105125
it("url", function(done) {
106126
cssRule.use = [
107127
{

0 commit comments

Comments
 (0)