Skip to content

added a parameter to transform URLs without adding assets to webpack pip... #25

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

Closed
wants to merge 1 commit into from
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ The result is:

* `url(/image.png)` => `require("./image.png")`

### 'Skip require and prepend' urls

If you don't want to process the images but rather want to transform the URLs use this parameter

If a `skipRequireAndAddPath` query parameter is set, it will be prepended to the url and assets won't be added to webpack bundle:

With a config like:

``` javascript
loaders: [
{ test: /\.css$/, loader: "style-loader!css-loader?skipRequireAndAddPath=app/images" },
...
]
```

The result is:

* `url(/image.png)` => `require("app/images/image.png")`

### SourceMaps

To include SourceMaps set the `sourceMap` query param.
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = function(content, map) {
var query = loaderUtils.parseQuery(this.query);
var root = query.root;
var forceMinimize = query.minimize;
var skipRequireAndAddPath = query.skipRequireAndAddPath;
var importLoaders = parseInt(query.importLoaders, 10) || 0;
var minimize = typeof forceMinimize !== "undefined" ? !!forceMinimize : (this && this.minimize);
var tree = csso.parse(content, "stylesheet");
Expand Down Expand Up @@ -46,6 +47,9 @@ module.exports = function(content, map) {
var match = /^%CSSURL\[%(["']?(.*?)["']?)%\]CSSURL%$/.exec(JSON.parse('"' + str + '"'));
var url = loaderUtils.parseString(match[2]);
if(!loaderUtils.isUrlRequest(match[2], root)) return JSON.stringify(match[1]).replace(/^"|"$/g, "");
if(skipRequireAndAddPath) {
return skipRequireAndAddPath + url;
}
var idx = url.indexOf("?#");
if(idx < 0) idx = url.indexOf("#");
if(idx > 0) {
Expand Down
3 changes: 3 additions & 0 deletions test/urlTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ describe("url", function() {
test("background img absolute with root", ".class { background: green url(/img.png) xyz }", [
[1, ".class { background: green url({./img.png}) xyz }", ""]
], "?root=.");
test("background img with prepended path", ".class { background: green url(img.png) xyz }", [
[1, ".class { background: green url(app/images/img.png) xyz }", ""]
], "?skipRequireAndAddPath=app/images/");
test("background img external",
".class { background: green url(data:image/png;base64,AAA) url(http://example.com/image.jpg) url(//example.com/image.png) xyz }", [
[1, ".class { background: green url(data:image/png;base64,AAA) url(http://example.com/image.jpg) url(//example.com/image.png) xyz }", ""]
Expand Down