Skip to content

Commit 2c4f427

Browse files
KonstantinKaijoshwiens
authored andcommitted
feat: Set custom attributes for tag in url mode (#198)
* Add ability to set custom attributes for tag in url mode
1 parent 1f68495 commit 2c4f427

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,16 @@ If convertToAbsoluteUrls and sourceMaps are both enabled, relative urls will be
8484
If defined, style-loader will attach given attributes with their values on `<style>` / `<link>` element.
8585
Usage:
8686
```javascript
87-
require('style-loader?{attrs:{id: "style-tag-id"}}!style.scss');
87+
require('style-loader?{attrs:{id: "style-tag-id"}}!style.css');
8888

8989
// will create style tag <style id="style-tag-id">
9090
```
91+
Usage in `url` mode:
92+
```javascript
93+
require('style-loader/url?{attrs:{prop: "value"}}!file-loader!style.css')
94+
95+
// will create link tag <link rel="stylesheet" type="text/css" href="[path]/style.css" prop="value">
96+
```
9197

9298
### Recommended configuration
9399

addStyleUrl.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,28 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
module.exports = function addStyleUrl(cssUrl) {
5+
6+
function attachTagAttrs(element, attrs) {
7+
Object.keys(attrs).forEach(function (key) {
8+
element.setAttribute(key, attrs[key]);
9+
});
10+
}
11+
12+
module.exports = function addStyleUrl(cssUrl, options) {
613
if(typeof DEBUG !== "undefined" && DEBUG) {
714
if(typeof document !== "object") throw new Error("The style-loader cannot be used in a non-browser environment");
815
}
16+
17+
options = options || {};
18+
options.attrs = typeof options.attrs === "object" ? options.attrs : {};
19+
920
var styleElement = document.createElement("link");
1021
styleElement.rel = "stylesheet";
1122
styleElement.type = "text/css";
1223
styleElement.href = cssUrl;
24+
25+
attachTagAttrs(styleElement, options.attrs);
26+
1327
var head = document.getElementsByTagName("head")[0];
1428
head.appendChild(styleElement);
1529
if(module.hot) {

test/basicTest.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,29 @@ describe("basic tests", function() {
155155
runCompilerTest(expected, done);
156156
}); // it url
157157

158+
it("url with attrs", function (done) {
159+
cssRule.use = [
160+
{
161+
loader: "style-loader/url",
162+
options: {
163+
attrs: {
164+
'data-attr-1': 'attr-value-1',
165+
'data-attr-2': 'attr-value-2'
166+
}
167+
}
168+
},
169+
"file-loader"
170+
];
171+
172+
// Run
173+
let expected = [
174+
existingStyle,
175+
'<link rel="stylesheet" type="text/css" href="ec9d4f4f24028c3d51bf1e7728e632ff.css" data-attr-1="attr-value-1" data-attr-2="attr-value-2">'
176+
].join("\n");
177+
178+
runCompilerTest(expected, done);
179+
}); // it url with attrs
180+
158181
it("useable", function(done) {
159182
cssRule.use = [
160183
{

url.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ var loaderUtils = require("loader-utils"),
77
module.exports = function() {};
88
module.exports.pitch = function(remainingRequest) {
99
this.cacheable && this.cacheable();
10+
var query = loaderUtils.getOptions(this) || {};
1011
return [
1112
"// style-loader: Adds some reference to a css file to the DOM by adding a <link> tag",
1213
"var update = require(" + JSON.stringify("!" + path.join(__dirname, "addStyleUrl.js")) + ")(",
1314
"\trequire(" + loaderUtils.stringifyRequest(this, "!!" + remainingRequest) + ")",
14-
");",
15+
", " + JSON.stringify(query) + ");",
1516
"// Hot Module Replacement",
1617
"if(module.hot) {",
1718
"\tmodule.hot.accept(" + loaderUtils.stringifyRequest(this, "!!" + remainingRequest) + ", function() {",

0 commit comments

Comments
 (0)