Skip to content

Commit 63f8671

Browse files
committedJan 28, 2017
use sourceURL for inline style tag source maps.
1 parent cb3c0b6 commit 63f8671

File tree

1 file changed

+11
-42
lines changed

1 file changed

+11
-42
lines changed
 

Diff for: ‎addStyles.js

+11-42
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,6 @@ function createStyleElement(options) {
133133
return styleElement;
134134
}
135135

136-
function createLinkElement(options) {
137-
var linkElement = document.createElement("link");
138-
linkElement.rel = "stylesheet";
139-
insertStyleElement(options, linkElement);
140-
return linkElement;
141-
}
142-
143136
function addStyle(obj, options) {
144137
var styleElement, update, remove;
145138

@@ -148,19 +141,6 @@ function addStyle(obj, options) {
148141
styleElement = singletonElement || (singletonElement = createStyleElement(options));
149142
update = applyToSingletonTag.bind(null, styleElement, styleIndex, false);
150143
remove = applyToSingletonTag.bind(null, styleElement, styleIndex, true);
151-
} else if(obj.sourceMap &&
152-
typeof URL === "function" &&
153-
typeof URL.createObjectURL === "function" &&
154-
typeof URL.revokeObjectURL === "function" &&
155-
typeof Blob === "function" &&
156-
typeof btoa === "function") {
157-
styleElement = createLinkElement(options);
158-
update = updateLink.bind(null, styleElement);
159-
remove = function() {
160-
removeStyleElement(styleElement);
161-
if(styleElement.href)
162-
URL.revokeObjectURL(styleElement.href);
163-
};
164144
} else {
165145
styleElement = createStyleElement(options);
166146
update = applyToTag.bind(null, styleElement);
@@ -212,11 +192,19 @@ function applyToTag(styleElement, obj) {
212192
var css = obj.css;
213193
var media = obj.media;
214194

215-
if(media) {
216-
styleElement.setAttribute("media", media)
195+
if (media) {
196+
styleElement.setAttribute("media", media);
217197
}
218198

219-
if(styleElement.styleSheet) {
199+
if (sourceMap) {
200+
// https://developer.chrome.com/devtools/docs/javascript-debugging
201+
// this makes source maps inside style tags work properly in Chrome
202+
css += '\n/*# sourceURL=' + sourceMap.sources[0] + ' */';
203+
// http://stackoverflow.com/a/26603875
204+
css += "\n/*# sourceMappingURL=data:application/json;base64," + btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap)))) + " */";
205+
}
206+
207+
if (styleElement.styleSheet) {
220208
styleElement.styleSheet.cssText = css;
221209
} else {
222210
while(styleElement.firstChild) {
@@ -225,22 +213,3 @@ function applyToTag(styleElement, obj) {
225213
styleElement.appendChild(document.createTextNode(css));
226214
}
227215
}
228-
229-
function updateLink(linkElement, obj) {
230-
var css = obj.css;
231-
var sourceMap = obj.sourceMap;
232-
233-
if(sourceMap) {
234-
// http://stackoverflow.com/a/26603875
235-
css += "\n/*# sourceMappingURL=data:application/json;base64," + btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap)))) + " */";
236-
}
237-
238-
var blob = new Blob([css], { type: "text/css" });
239-
240-
var oldSrc = linkElement.href;
241-
242-
linkElement.href = URL.createObjectURL(blob);
243-
244-
if(oldSrc)
245-
URL.revokeObjectURL(oldSrc);
246-
}

0 commit comments

Comments
 (0)