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

Commit

Permalink
feat(resources): specify custom public file name
Browse files Browse the repository at this point in the history
  • Loading branch information
moughxyz authored and joshwiens committed Jan 28, 2017
1 parent 0727efe commit 6833c70
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var url = require("file-loader!./file.png");
// => returns i. e. "/public-path/0dcbbaa701328a3c262cfd45869e351f.png"
```

By default the filename of the resulting file is the MD5 hash of the file's contents
By default the filename of the resulting file is the MD5 hash of the file's contents
with the original extension of the required resource.

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

## Filename templates

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]`.
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]`.

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.

You can specify custom output and public paths by using the `outputPath` and `publicPath` query name parameters:

```
loader: "file-loader?name=[name].[ext]&publicPath=assets/foo/&outputPath=app/images/"
```

### Filename template placeholders

Expand Down
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,28 @@ module.exports = function(content) {
regExp: config.regExp
});

var outputPath = url;

var publicPath = "__webpack_public_path__ + " + JSON.stringify(url);

if (config.outputPath) {
// support functions as outputPath to generate them dynamically
outputPath = typeof config.outputPath === "function"
? config.outputPath(url)
: config.outputPath + url
}

if (config.publicPath) {
// support functions as publicPath to generate them dynamically
publicPath = JSON.stringify(
typeof config.publicPath === "function"
? config.publicPath(url)
typeof config.publicPath === "function"
? config.publicPath(url)
: config.publicPath + url
);
}

if (query.emitFile === undefined || query.emitFile) {
this.emitFile(url, content);
this.emitFile(outputPath, content);
}

return "module.exports = " + publicPath + ";";
Expand Down

0 comments on commit 6833c70

Please sign in to comment.