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

Commit 6833c70

Browse files
moughxyzjoshwiens
authored andcommittedJan 28, 2017
feat(resources): specify custom public file name
1 parent 0727efe commit 6833c70

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed
 

‎README.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var url = require("file-loader!./file.png");
1010
// => returns i. e. "/public-path/0dcbbaa701328a3c262cfd45869e351f.png"
1111
```
1212

13-
By default the filename of the resulting file is the MD5 hash of the file's contents
13+
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

1616
By default a file is emitted, however this can be disabled if required (e.g. for server
@@ -24,10 +24,15 @@ var url = require("file-loader?emitFile=false!./file.png");
2424

2525
## Filename templates
2626

27-
You can configure a custom filename template for your file using the query
28-
parameter `name`. For instance, to copy a file from your `context` directory
29-
into the output directory retaining the full directory structure, you might
30-
use `?name=[path][name].[ext]`.
27+
You can configure a custom filename template for your file using the query parameter `name`. For instance, to copy a file from your `context` directory into the output directory retaining the full directory structure, you might use `?name=[path][name].[ext]`.
28+
29+
By default, the path and name you specify will output the file in that same directory and will also use that same URL path to access the file.
30+
31+
You can specify custom output and public paths by using the `outputPath` and `publicPath` query name parameters:
32+
33+
```
34+
loader: "file-loader?name=[name].[ext]&publicPath=assets/foo/&outputPath=app/images/"
35+
```
3136

3237
### Filename template placeholders
3338

‎index.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,28 @@ module.exports = function(content) {
3333
regExp: config.regExp
3434
});
3535

36+
var outputPath = url;
37+
3638
var publicPath = "__webpack_public_path__ + " + JSON.stringify(url);
3739

40+
if (config.outputPath) {
41+
// support functions as outputPath to generate them dynamically
42+
outputPath = typeof config.outputPath === "function"
43+
? config.outputPath(url)
44+
: config.outputPath + url
45+
}
46+
3847
if (config.publicPath) {
3948
// support functions as publicPath to generate them dynamically
4049
publicPath = JSON.stringify(
41-
typeof config.publicPath === "function"
42-
? config.publicPath(url)
50+
typeof config.publicPath === "function"
51+
? config.publicPath(url)
4352
: config.publicPath + url
4453
);
4554
}
4655

4756
if (query.emitFile === undefined || query.emitFile) {
48-
this.emitFile(url, content);
57+
this.emitFile(outputPath, content);
4958
}
5059

5160
return "module.exports = " + publicPath + ";";

0 commit comments

Comments
 (0)
This repository has been archived.