Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit 20c8fff

Browse files
author
Liam O'Boyle
committed
Make emitting a file optional.
When building server side packages there may be no need to emit a file; this makes file emission optional, while keeping the default behaviour as is.
1 parent 884f22a commit 20c8fff

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ var url = require("file!./file.png");
1313
By default the filename of the resulting file is the MD5 hash of the file's contents
1414
with the original extension of the required resource.
1515

16+
By default a file is emitted, however this can be disabled if required (e.g. for server
17+
side packages).
18+
19+
``` javascript
20+
var url = require("file?emitFile=false!./file.png");
21+
// => returns the public url but does NOT emit a file
22+
// => returns i. e. "/public-path/0dcbbaa701328a3c262cfd45869e351f.png"
23+
```
24+
1625
## Filename templates
1726

1827
You can configure a custom filename template for your file using the query

index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ module.exports = function(content) {
1313
content: content,
1414
regExp: query.regExp
1515
});
16-
this.emitFile(url, content);
16+
17+
if (query.emitFile === undefined || query.emitFile) {
18+
this.emitFile(url, content);
19+
}
1720
return "module.exports = __webpack_public_path__ + " + JSON.stringify(url) + ";";
1821
}
1922
module.exports.raw = true;

test/optional-file-emission.test.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var should = require("should");
2+
var fileLoader = require("../");
3+
4+
function run(resourcePath, query, content) {
5+
content = content || new Buffer("1234");
6+
var result = false;
7+
var context = {
8+
resourcePath: resourcePath,
9+
query: "?" + query,
10+
options: {
11+
context: "/this/is/the/context"
12+
},
13+
emitFile: function(url, content2) {
14+
result = true;
15+
}
16+
};
17+
fileLoader.call(context, content);
18+
return result;
19+
}
20+
21+
describe("optional-emission", function() {
22+
it("should emit a file by default", function() {
23+
run("whatever.txt", "").should.be.true;
24+
});
25+
26+
it("should not emit a file if disabled", function() {
27+
run("whatever.txt", "emitFile=false").should.be.false;
28+
});
29+
});

0 commit comments

Comments
 (0)