Skip to content

Commit fa004d7

Browse files
committed
added a parameter to transform URLs without adding assets to webpack pipeline
1 parent 7b50d4f commit fa004d7

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@ The result is:
5858

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

61+
### 'Skip require and prepend' urls
62+
63+
If you don't want to process the images but rather want to transform the URLs use this parameter
64+
65+
If a `skipRequireAndAddPath` query parameter is set, it will be prepended to the url and assets won't be added to webpack bundle:
66+
67+
With a config like:
68+
69+
``` javascript
70+
loaders: [
71+
{ test: /\.css$/, loader: "style-loader!css-loader?skipRequireAndAddPath=app/images" },
72+
...
73+
]
74+
```
75+
76+
The result is:
77+
78+
* `url(/image.png)` => `require("app/images/image.png")`
79+
6180
### SourceMaps
6281

6382
To include SourceMaps set the `sourceMap` query param.

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = function(content, map) {
1313
var query = loaderUtils.parseQuery(this.query);
1414
var root = query.root;
1515
var forceMinimize = query.minimize;
16+
var skipRequireAndAddPath = query.skipRequireAndAddPath;
1617
var importLoaders = parseInt(query.importLoaders, 10) || 0;
1718
var minimize = typeof forceMinimize !== "undefined" ? !!forceMinimize : (this && this.minimize);
1819
var tree = csso.parse(content, "stylesheet");
@@ -46,6 +47,9 @@ module.exports = function(content, map) {
4647
var match = /^%CSSURL\[%(["']?(.*?)["']?)%\]CSSURL%$/.exec(JSON.parse('"' + str + '"'));
4748
var url = loaderUtils.parseString(match[2]);
4849
if(!loaderUtils.isUrlRequest(match[2], root)) return JSON.stringify(match[1]).replace(/^"|"$/g, "");
50+
if(skipRequireAndAddPath) {
51+
return skipRequireAndAddPath + url;
52+
}
4953
var idx = url.indexOf("?#");
5054
if(idx < 0) idx = url.indexOf("#");
5155
if(idx > 0) {

test/urlTest.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ describe("url", function() {
107107
test("background img absolute with root", ".class { background: green url(/img.png) xyz }", [
108108
[1, ".class { background: green url({./img.png}) xyz }", ""]
109109
], "?root=.");
110+
test("background img with prepended path", ".class { background: green url(img.png) xyz }", [
111+
[1, ".class { background: green url(app/images/img.png) xyz }", ""]
112+
], "?skipRequireAndAddPath=app/images/");
110113
test("background img external",
111114
".class { background: green url(data:image/png;base64,AAA) url(http://example.com/image.jpg) url(//example.com/image.png) xyz }", [
112115
[1, ".class { background: green url(data:image/png;base64,AAA) url(http://example.com/image.jpg) url(//example.com/image.png) xyz }", ""]

0 commit comments

Comments
 (0)