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

Make urls absolute in blob #96

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Expand Up @@ -70,8 +70,6 @@ So the recommended configuration for webpack is:
}
```

**Note** about source maps support and assets referenced with `url`: when style loader is used with ?sourceMap option, the CSS modules will be generated as `Blob`s, so relative paths don't work (they would be relative to `chrome:blob` or `chrome:devtools`). In order for assets to maintain correct paths setting `output.publicPath` property of webpack configuration must be set, so that absolute paths are generated.

## Install

`npm install style-loader`
Expand Down
28 changes: 28 additions & 0 deletions addStyles.js
Expand Up @@ -231,6 +231,34 @@ function updateLink(linkElement, obj) {
var css = obj.css;
var media = obj.media;
var sourceMap = obj.sourceMap;
var absPrefix = window.location.protocol + '//' + window.location.host;

css = css
.replace(
new RegExp('url\\(//', 'g'),
'url(' + window.location.protocol + '//'
)
.replace(
new RegExp('url\\(\'//', 'g'),
'url(\'' + window.location.protocol + '//'
)
.replace(
new RegExp('url\\(\"/', 'g'),
'url(\"' + window.location.protocol + '//'
)
.replace(
new RegExp('url\\(/', 'g'),
'url(' + absPrefix + '/'
)
.replace(
new RegExp('url\\(\'/', 'g'),
'url(\'' + absPrefix + '/'
)
.replace(
new RegExp('url\\(\"/', 'g'),
'url(\"' + absPrefix + '/'
)
;

if(sourceMap) {
// http://stackoverflow.com/a/26603875
Expand Down