Socket
Socket
Sign inDemoInstall

loader-utils

Package Overview
Dependencies
2
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.2.4 to 0.2.5

110

index.js
var JSON5 = require("json5");
var path = require("path");
var baseEncodeTables = {
26: "abcdefghijklmnopqrstuvwxyz",
32: "123456789abcdefghjkmnpqrstuvwxyz", // no 0lio
36: "0123456789abcdefghijklmnopqrstuvwxyz",
49: "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ", // no lIO
52: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
58: "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ", // no 0lIO
62: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
64: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_"
};
function encodeBufferToBase(buffer, base, length) {
var encodeTable = baseEncodeTables[base];
if (!encodeTable) throw new Error("Enknown encoding base" + base);
var readLength = buffer.length;
var Big = require('big.js');
Big.RM = Big.DP = 0;
var b = new Big(0);
for (var i = readLength - 1; i >= 0; i--) {
b = b.times(256).plus(buffer[i]);
}
var output = "";
while (b.gt(0)) {
output = encodeTable[b.mod(base)] + output;
b = b.div(base);
}
Big.DP = 20;
Big.RM = 1;
return output;
}
exports.parseQuery = function parseQuery(query) {

@@ -116,1 +154,73 @@ if(!query) return {};

};
exports.getHashDigest = function getHashDigest(buffer, hashType, digestType, maxLength) {
hashType = hashType || "md5";
maxLength = maxLength || 9999;
var hash = new (require("crypto").Hash)(hashType);
hash.update(buffer);
if (digestType === "base26" || digestType === "base32" || digestType === "base36" ||
digestType === "base49" || digestType === "base52" || digestType === "base58" ||
digestType === "base62" || digestType === "base64") {
return encodeBufferToBase(hash.digest(), digestType.substr(4), maxLength).substr(0, maxLength);
} else {
return hash.digest(digestType || "hex").substr(0, maxLength);
}
};
exports.interpolateName = function interpolateName(loaderContext, name, options) {
var filename = name || "[hash].[ext]";
var context = options.context;
var content = options.content;
var regExp = options.regExp;
var ext = "bin";
var basename = "file";
var directory = "";
if(loaderContext.resourcePath) {
var resourcePath = loaderContext.resourcePath;
var idx = resourcePath.lastIndexOf(".");
var i = resourcePath.lastIndexOf("\\");
var j = resourcePath.lastIndexOf("/");
var p = i < 0 ? j : j < 0 ? i : i < j ? i : j;
if(idx >= 0) {
ext = resourcePath.substr(idx+1);
resourcePath = resourcePath.substr(0, idx);
}
if(p >= 0) {
basename = resourcePath.substr(p+1);
resourcePath = resourcePath.substr(0, p+1);
}
if (typeof context !== 'undefined') {
directory = path.relative(context, resourcePath + "_").replace(/\\/g, "/").replace(/\.\.(\/)?/g, "_$1");
directory = directory.substr(0, directory.length-1);
}
else {
directory = resourcePath.replace(/\\/g, "/").replace(/\.\.(\/)?/g, "_$1");
}
if(directory.length === 1) directory = "";
}
var url = filename;
if(content) {
// Match hash template
url = url.replace(/\[(?:(\w+):)?hash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, function() {
return exports.getHashDigest(content, arguments[1], arguments[2], parseInt(arguments[3], 10));
})
}
url = url.replace(/\[ext\]/ig, function() {
return ext;
}).replace(/\[name\]/ig, function() {
return basename;
}).replace(/\[path\]/ig, function() {
return directory;
});
if(regExp && loaderContext.resourcePath) {
var re = new RegExp(regExp);
var match = loaderContext.resourcePath.match(regExp);
if(match) {
for (var i = 1; i < match.length; i++) {
var re = new RegExp("\\[" + i + "\\]", "ig");
url = url.replace(re, match[i]);
}
}
}
return url;
};

5

package.json
{
"name": "loader-utils",
"version": "0.2.4",
"version": "0.2.5",
"author": "Tobias Koppers @sokra",
"description": "utils for webpack loaders",
"dependencies": {
"json5": "0.1.x"
"json5": "0.1.x",
"big.js": "~2.5.1"
},

@@ -9,0 +10,0 @@ "scripts": {

@@ -62,4 +62,69 @@ # loader-utils

### `interpolateName`
Interpolates a filename template using multiple placeholders and/or a regular expression.
The template and regular expresion are set as query params called `name` and `regExp` on the current loader's context.
```javascript
var interpolatedName = loaderUtils.interpolateName(loaderContext, name, options);
```
The following tokens are replaced in the `name` parameter:
* `[ext]` the extension of the resource
* `[name]` the basename of the resource
* `[path]` the path of the resource relative to the `context` query parameter or option.
* `[hash]` the hash of `options.content` (Buffer) (by default it's the hex digest of the md5 hash)
* `[<hashType>:hash:<digestType>:<length>]` optionally one can configure
* other `hashType`s, i. e. `sha1`, `md5`, `sha256`, `sha512`
* other `digestType`s, i. e. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`
* and `length` the length in chars
* `[N]` the N-th match obtained from matching the current file name against `options.regExp`
Examples
``` javascript
// loaderContext.resourcePath = "/app/js/javascript.js"
loaderUtils.interpolateName(loaderContext, "js/[hash].script.[ext]", { content: ... });
// => js/0dcbbaa701328a3c262cfd45869e351f.script.js
// loaderContext.resourcePath = "/app/page.html"
loaderUtils.interpolateName(loaderContext, "html-[hash:6].html", { content: ... });
// => html-109fa8.html
// loaderContext.resourcePath = "/app/flash.txt"
loaderUtils.interpolateName(loaderContext, "[hash]", { content: ... });
// => c31e9820c001c9c4a86bce33ce43b679
// loaderContext.resourcePath = "/app/img/image.png"
loaderUtils.interpolateName(loaderContext, "[sha512:hash:base64:7]", { content: ... });
// => gdyb21L.png
// use sha512 hash instead of md5 and with only 7 chars of base64
// loaderContext.resourcePath = "/app/img/myself.png"
// loaderContext.query.name =
loaderUtils.interpolateName(loaderContext, "picture.png");
// => picture.png
// loaderContext.resourcePath = "/app/dir/file.png"
loaderUtils.interpolateName(loaderContext, "[path][name].[ext]?[hash]", { content: ... });
// => dir/file.png?e43b20c069c4a01867c31e98cbce33c9
// loaderContext.resourcePath = "/app/js/page-home.js"
loaderUtils.interpolateName(loaderContext, "script-[1].[ext]", { regExp: "page-(.*)\\.js", content: ... });
// => script-home.js
```
### `getHashDigest`
``` javascript
var digestString = loaderUtils.getHashDigest(buffer, hashType, digestType, maxLength);
```
* `buffer` the content that should be hashed
* `hashType` one of `sha1`, `md5`, `sha256`, `sha512` or any other node.js supported hash type
* `digestType` one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`
* `maxLength` the maximum length in chars
## License
MIT (http://www.opensource.org/licenses/mit-license.php)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc