file-loader
Advanced tools
+27
-13
@@ -5,16 +5,30 @@ /* | ||
| */ | ||
| module.exports = function(postfix, prefix) { | ||
| postfix = postfix || ""; | ||
| prefix = prefix || ""; | ||
| return function(content) { | ||
| this.cacheable && this.cacheable(); | ||
| if(!this.emitFile) throw new Error("emitFile is required from module system"); | ||
| if(this.buffers) content = this.buffers[0]; | ||
| hash = new (require("crypto").Hash)("md5"); | ||
| var loaderUtils = require("loader-utils"); | ||
| module.exports = function(content) { | ||
| this.cacheable && this.cacheable(); | ||
| if(!this.emitFile) throw new Error("emitFile is required from module system"); | ||
| var query = loaderUtils.parseQuery(this.query); | ||
| var url; | ||
| if(query.name) { | ||
| url = query.name; | ||
| } else { | ||
| var prefix = query.prefix || ""; | ||
| var postfix = query.postfix; | ||
| if(typeof postfix != "string") { | ||
| if(this.resource && typeof postfix != "boolean") { | ||
| var idx = this.resource.lastIndexOf("."); | ||
| if(idx > 0) postfix = this.resource.substr(idx); | ||
| else postfix = ""; | ||
| } else postfix = ""; | ||
| } | ||
| var digest = query.hash || "md5"; | ||
| var digestSize = query.size || 9999; | ||
| hash = new (require("crypto").Hash)(digest); | ||
| hash.update(content); | ||
| var hash = hash.digest("hex"); | ||
| var url = prefix + hash + postfix; | ||
| this.emitFile(url, content); | ||
| return "module.exports = " + require("webpack/api/getPublicPrefix") + " + " + JSON.stringify(url); | ||
| var hash = hash.digest("hex").substr(0, digestSize); | ||
| url = prefix + hash + postfix; | ||
| } | ||
| } | ||
| this.emitFile(url, content); | ||
| return "module.exports = __webpack_public_path__ + " + JSON.stringify(url); | ||
| } | ||
| module.exports.raw = true; |
+5
-3
| { | ||
| "name": "file-loader", | ||
| "version": "0.1.2", | ||
| "version": "0.5.0", | ||
| "author": "Tobias Koppers @sokra", | ||
| "description": "file loader module for webpack", | ||
| "dependencies": { | ||
| "loader-utils": "0.2.x" | ||
| }, | ||
| "licenses": [ | ||
@@ -11,4 +14,3 @@ { | ||
| } | ||
| ], | ||
| "license": "MIT" | ||
| ] | ||
| } |
+18
-10
@@ -10,18 +10,26 @@ # file loader for webpack | ||
| If you want to have a custom extension on your file, I have some prepared: | ||
| The filename is the md5 hash of the file. The extension of the required resource is appended. | ||
| If you want to have custom pre- and postfix of your file: | ||
| ``` javascript | ||
| require("file/js!./javascript.js"); | ||
| require("file/html!./page.html"); | ||
| require("file/txt!./flash.txt"); | ||
| require("file/png!./image.png"); | ||
| require("file/jpg!./image.jpg"); | ||
| require("file/jpeg!./image.jpeg"); | ||
| require("file/swf!./flash.swf"); | ||
| require("file?prefix=js/&postfix=.script.js!./javascript.js"); | ||
| // => js/0dcbbaa701328a3c262cfd45869e351f.script.js | ||
| require("file?prefix=html-&size=6!./page.html"); | ||
| // => html-109fa8.html | ||
| require("file?postfix!./flash.txt"); | ||
| // => c31e9820c001c9c4a86bce33ce43b679 | ||
| require("file?hash=sha512!./image.png"); | ||
| // => a720b9f140d13...781f1f78344.png | ||
| // use sha512 hash instead of md5 | ||
| require("file?name=picture.png!./myself.png"); | ||
| // => picture.png | ||
| ``` | ||
| Look at the source to write your own. Pre- and postfix is possible. | ||
| ## License | ||
| MIT (http://www.opensource.org/licenses/mit-license.php) |
-16
| /* | ||
| MIT License http://www.opensource.org/licenses/mit-license.php | ||
| Author Tobias Koppers @sokra | ||
| */ | ||
| module.exports = function(content) { | ||
| var request = this && this.request || ""; | ||
| var loader = require("./index.loader.js"); | ||
| if(/\.html$/.test(request)) loader = require("./html"); | ||
| else if(/\.jpeg$/.test(request)) loader = require("./jpeg"); | ||
| else if(/\.jpg$/.test(request)) loader = require("./jpg"); | ||
| else if(/\.js$/.test(request)) loader = require("./js"); | ||
| else if(/\.png$/.test(request)) loader = require("./png"); | ||
| else if(/\.swf$/.test(request)) loader = require("./swf"); | ||
| else if(/\.txt$/.test(request)) loader = require("./txt"); | ||
| return loader.call(this, content); | ||
| } |
-5
| /* | ||
| MIT License http://www.opensource.org/licenses/mit-license.php | ||
| Author Tobias Koppers @sokra | ||
| */ | ||
| module.exports = require("./index.js")(".html"); |
| /* | ||
| MIT License http://www.opensource.org/licenses/mit-license.php | ||
| Author Tobias Koppers @sokra | ||
| */ | ||
| module.exports = require("./index.js")(""); |
-5
| /* | ||
| MIT License http://www.opensource.org/licenses/mit-license.php | ||
| Author Tobias Koppers @sokra | ||
| */ | ||
| module.exports = require("./index.js")(".jpeg"); |
-5
| /* | ||
| MIT License http://www.opensource.org/licenses/mit-license.php | ||
| Author Tobias Koppers @sokra | ||
| */ | ||
| module.exports = require("./index.js")(".jpg"); |
-5
| /* | ||
| MIT License http://www.opensource.org/licenses/mit-license.php | ||
| Author Tobias Koppers @sokra | ||
| */ | ||
| module.exports = require("./index.js")(".js"); |
-5
| /* | ||
| MIT License http://www.opensource.org/licenses/mit-license.php | ||
| Author Tobias Koppers @sokra | ||
| */ | ||
| module.exports = require("./index.js")(".png"); |
-5
| /* | ||
| MIT License http://www.opensource.org/licenses/mit-license.php | ||
| Author Tobias Koppers @sokra | ||
| */ | ||
| module.exports = require("./index.js")(".swf"); |
-5
| /* | ||
| MIT License http://www.opensource.org/licenses/mit-license.php | ||
| Author Tobias Koppers @sokra | ||
| */ | ||
| module.exports = require("./index.js")(".txt"); |
34
30.77%2312
-33.12%1
Infinity%3
-75%32
-50.77%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added