diff --git a/README.md b/README.md index 58b01b1..d23b881 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/index.js b/index.js index 99ff6af..bb95093 100644 --- a/index.js +++ b/index.js @@ -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 + ";";