Skip to content

Commit

Permalink
chore(deps): update
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Oct 20, 2021
1 parent cc1e035 commit 47d0be7
Show file tree
Hide file tree
Showing 29 changed files with 1,779 additions and 2,841 deletions.
13 changes: 2 additions & 11 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"root": true,
"plugins": ["node", "prettier"],
"plugins": ["node"],
"extends": ["eslint:recommended", "plugin:node/recommended"],
"env": {
"node": true
Expand All @@ -18,15 +18,6 @@
"no-var": "error",
"prefer-const": "error",
"prefer-arrow-callback": "error",
"object-shorthand": "error",

"prettier/prettier": [
"error",
{
"singleQuote": true,
"trailingComma": "es5",
"arrowParens": "always"
}
]
"object-shorthand": "error"
}
}
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/coverage
/dist
/node_modules
/test/fixtures
CHANGELOG.md
5 changes: 0 additions & 5 deletions .prettierrc

This file was deleted.

10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ cache: yarn

matrix:
include:
- node_js: '12'
- node_js: "12"
script: yarn pretest
env: CI=pretest
- node_js: '8'
- node_js: "8"
script: yarn test:ci
env: CI=tests 8
- node_js: '10'
- node_js: "10"
script: yarn test:ci
env: CI=tests 10
- node_js: '12'
- node_js: "12"
script: yarn test:ci
env: CI=coverage 12
- node_js: '13'
- node_js: "13"
script: yarn test:ci
env: CI=coverage 13

Expand Down
91 changes: 50 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ const options = loaderUtils.getOptions(this);
```

1. If `this.query` is a string:
- Tries to parse the query string and returns a new object
- Throws if it's not a valid query string
- Tries to parse the query string and returns a new object
- Throws if it's not a valid query string
2. If `this.query` is object-like, it just returns `this.query`
3. In any other case, it just returns `null`

**Please note:** The returned `options` object is *read-only*. It may be re-used across multiple invocations.
If you pass it on to another library, make sure to make a *deep copy* of it:
**Please note:** The returned `options` object is _read-only_. It may be re-used across multiple invocations.
If you pass it on to another library, make sure to make a _deep copy_ of it:

```javascript
const options = Object.assign(
{},
defaultOptions,
loaderUtils.getOptions(this) // it is safe to pass null to Object.assign()
{},
defaultOptions,
loaderUtils.getOptions(this) // it is safe to pass null to Object.assign()
);
// don't forget nested objects or arrays
options.obj = Object.assign({}, options.obj);
options.obj = Object.assign({}, options.obj);
options.arr = options.arr.slice();
someLibrary(options);
```
Expand All @@ -42,16 +42,16 @@ If the loader options have been passed as loader query string (`loader?some&para

Parses a passed string (e.g. `loaderContext.resourceQuery`) as a query string, and returns an object.

``` javascript
```javascript
const params = loaderUtils.parseQuery(this.resourceQuery); // resource: `file?param1=foo`
if (params.param1 === "foo") {
// do something
// do something
}
```

The string is parsed like this:

``` text
```text
-> Error
? -> {}
?flag -> { flag: true }
Expand Down Expand Up @@ -168,35 +168,39 @@ Interpolates a filename template using multiple placeholders and/or a regular ex
The template and regular expression are set as query params called `name` and `regExp` on the current loader's context.

```javascript
const interpolatedName = loaderUtils.interpolateName(loaderContext, name, options);
const 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.
* `[folder]` the folder the resource is in
* `[query]` the queryof the resource, i.e. `?foo=bar`
* `[emoji]` a random emoji representation of `options.content`
* `[emoji:<length>]` same as above, but with a customizable number of emojis
* `[contenthash]` the hash of `options.content` (Buffer) (by default it's the hex digest of the md4 hash)
* `[<hashType>:contenthash:<digestType>:<length>]` optionally one can configure
* other `hashType`s, i. e. `sha1`, `md4`, `md5`, `sha256`, `sha512`
* other `digestType`s, i. e. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`
* and `length` the length in chars
* `[hash]` the hash of `options.content` (Buffer) (by default it's the hex digest of the md4 hash)
* `[<hashType>:hash:<digestType>:<length>]` optionally one can configure
* other `hashType`s, i. e. `sha1`, `md4`, `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`
- `[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.
- `[folder]` the folder the resource is in
- `[query]` the queryof the resource, i.e. `?foo=bar`
- `[emoji]` a random emoji representation of `options.content`
- `[emoji:<length>]` same as above, but with a customizable number of emojis
- `[contenthash]` the hash of `options.content` (Buffer) (by default it's the hex digest of the md4 hash)
- `[<hashType>:contenthash:<digestType>:<length>]` optionally one can configure
- other `hashType`s, i. e. `sha1`, `md4`, `md5`, `sha256`, `sha512`
- other `digestType`s, i. e. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`
- and `length` the length in chars
- `[hash]` the hash of `options.content` (Buffer) (by default it's the hex digest of the md4 hash)
- `[<hashType>:hash:<digestType>:<length>]` optionally one can configure
- other `hashType`s, i. e. `sha1`, `md4`, `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`

In loader context `[hash]` and `[contenthash]` are the same, but we recommend using `[contenthash]` for avoid misleading.

Examples

``` javascript
```javascript
// loaderContext.resourcePath = "/absolute/path/to/app/js/javascript.js"
loaderUtils.interpolateName(loaderContext, "js/[hash].script.[ext]", { content: ... });
// => js/9473fdd0d880a43c21b7778d34872157.script.js
Expand Down Expand Up @@ -247,28 +251,33 @@ loaderUtils.interpolateName(loaderContext, "script-[1].[ext]", { regExp: "page-(
// loaderContext.resourcePath = "/absolute/path/to/app/js/javascript.js"
// loaderContext.resourceQuery = "?foo=bar"
loaderUtils.interpolateName(
loaderContext,
(resourcePath, resourceQuery) => {
loaderContext,
(resourcePath, resourceQuery) => {
// resourcePath - `/app/js/javascript.js`
// resourceQuery - `?foo=bar`

return "js/[hash].script.[ext]";
},
return "js/[hash].script.[ext]";
},
{ content: ... }
);
// => js/9473fdd0d880a43c21b7778d34872157.script.js
```

### `getHashDigest`

``` javascript
const digestString = loaderUtils.getHashDigest(buffer, hashType, digestType, maxLength);
```javascript
const digestString = loaderUtils.getHashDigest(
buffer,
hashType,
digestType,
maxLength
);
```

* `buffer` the content that should be hashed
* `hashType` one of `sha1`, `md4`, `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
- `buffer` the content that should be hashed
- `hashType` one of `sha1`, `md4`, `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

Expand Down
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:

clone_depth: 10

version: '{build}'
version: "{build}"
build: off
deploy: off

Expand All @@ -28,4 +28,4 @@ cache:
test_script:
- node --version
- npm --version
- cmd: 'yarn test:ci'
- cmd: "yarn test:ci"
4 changes: 2 additions & 2 deletions lib/getCurrentRequest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
"use strict";

function getCurrentRequest(loaderContext) {
if (loaderContext.currentRequest) {
Expand All @@ -10,7 +10,7 @@ function getCurrentRequest(loaderContext) {
.map((obj) => obj.request)
.concat([loaderContext.resource]);

return request.join('!');
return request.join("!");
}

module.exports = getCurrentRequest;
46 changes: 23 additions & 23 deletions lib/getHashDigest.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
'use strict';
"use strict";

const baseEncodeTables = {
26: 'abcdefghijklmnopqrstuvwxyz',
32: '123456789abcdefghjkmnpqrstuvwxyz', // no 0lio
36: '0123456789abcdefghijklmnopqrstuvwxyz',
49: 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ', // no lIO
52: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
58: '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ', // no 0lIO
62: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
64: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_',
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) {
const encodeTable = baseEncodeTables[base];
if (!encodeTable) {
throw new Error('Unknown encoding base' + base);
throw new Error("Unknown encoding base" + base);
}

const readLength = buffer.length;
const Big = require('big.js');
const Big = require("big.js");

Big.RM = Big.DP = 0;
let b = new Big(0);
Expand All @@ -27,7 +27,7 @@ function encodeBufferToBase(buffer, base) {
b = b.times(256).plus(buffer[i]);
}

let output = '';
let output = "";
while (b.gt(0)) {
output = encodeTable[b.mod(base)] + output;
b = b.div(base);
Expand All @@ -40,29 +40,29 @@ function encodeBufferToBase(buffer, base) {
}

function getHashDigest(buffer, hashType, digestType, maxLength) {
hashType = hashType || 'md4';
hashType = hashType || "md4";
maxLength = maxLength || 9999;

const hash = require('crypto').createHash(hashType);
const hash = require("crypto").createHash(hashType);

hash.update(buffer);

if (
digestType === 'base26' ||
digestType === 'base32' ||
digestType === 'base36' ||
digestType === 'base49' ||
digestType === 'base52' ||
digestType === 'base58' ||
digestType === 'base62' ||
digestType === 'base64'
digestType === "base26" ||
digestType === "base32" ||
digestType === "base36" ||
digestType === "base49" ||
digestType === "base52" ||
digestType === "base58" ||
digestType === "base62" ||
digestType === "base64"
) {
return encodeBufferToBase(hash.digest(), digestType.substr(4)).substr(
0,
maxLength
);
} else {
return hash.digest(digestType || 'hex').substr(0, maxLength);
return hash.digest(digestType || "hex").substr(0, maxLength);
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/getOptions.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';
"use strict";

const parseQuery = require('./parseQuery');
const parseQuery = require("./parseQuery");

function getOptions(loaderContext) {
const query = loaderContext.query;

if (typeof query === 'string' && query !== '') {
if (typeof query === "string" && query !== "") {
return parseQuery(loaderContext.query);
}

if (!query || typeof query !== 'object') {
if (!query || typeof query !== "object") {
// Not object-like queries are not supported.
return {};
}
Expand Down
4 changes: 2 additions & 2 deletions lib/getRemainingRequest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
"use strict";

function getRemainingRequest(loaderContext) {
if (loaderContext.remainingRequest) {
Expand All @@ -10,7 +10,7 @@ function getRemainingRequest(loaderContext) {
.map((obj) => obj.request)
.concat([loaderContext.resource]);

return request.join('!');
return request.join("!");
}

module.exports = getRemainingRequest;
22 changes: 11 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';
"use strict";

const getOptions = require('./getOptions');
const parseQuery = require('./parseQuery');
const stringifyRequest = require('./stringifyRequest');
const getRemainingRequest = require('./getRemainingRequest');
const getCurrentRequest = require('./getCurrentRequest');
const isUrlRequest = require('./isUrlRequest');
const urlToRequest = require('./urlToRequest');
const parseString = require('./parseString');
const getHashDigest = require('./getHashDigest');
const interpolateName = require('./interpolateName');
const getOptions = require("./getOptions");
const parseQuery = require("./parseQuery");
const stringifyRequest = require("./stringifyRequest");
const getRemainingRequest = require("./getRemainingRequest");
const getCurrentRequest = require("./getCurrentRequest");
const isUrlRequest = require("./isUrlRequest");
const urlToRequest = require("./urlToRequest");
const parseString = require("./parseString");
const getHashDigest = require("./getHashDigest");
const interpolateName = require("./interpolateName");

exports.getOptions = getOptions;
exports.parseQuery = parseQuery;
Expand Down
Loading

0 comments on commit 47d0be7

Please sign in to comment.