Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed Feb 18, 2017
1 parent a78426b commit ff86dd6
Showing 1 changed file with 26 additions and 37 deletions.
63 changes: 26 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,46 @@

## Methods

### `getLoaderConfig`
### getOptions

Recommended way to retrieve the loader config:
Recommended way to retrieve the options of a loader invocation:

```javascript
// inside your loader
config = loaderUtils.getLoaderConfig(this, "myLoader");
const options = loaderUtils.getOptions(this);
```

Tries to read the loader config from the `webpack.config.js` under the given property name (`"myLoader"` in this case) and merges the result with the loader query. For example, if your `webpack.config.js` had this property...
**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
cheesecakeLoader: {
type: "delicious",
slices: 4
}
const options = Object.assign({}, loaderUtils.getOptions(this));
// don't forget nested objects or arrays
options.obj = Object.assign({}, options.obj);
options.arr = options.arr.slice();
someLibrary(options);
```

...and your loader was called with `?slices=8`, `getLoaderConfig(this, "cheesecakeLoader")` would return
[assign-deep](https://www.npmjs.com/package/assign-deep) is a good library to make a deep copy of the options.

```javascript
{
type: "delicious",
slices: 8
}
```

It is recommended that you use the camelCased loader name as your default config property name.
#### Options as query strings

### `parseQuery`

``` javascript
var query = loaderUtils.parseQuery(this.query);
assert(typeof query == "object");
if(query.flag)
// ...
```
If the loader options have been passed as loader query string (`loader?some&params`), the string is parsed like this:

``` text
null -> {}
? -> {}
?flag -> { flag: true }
?+flag -> { flag: true }
?-flag -> { flag: false }
?xyz=test -> { xyz: "test" }
?xyz[]=a -> { xyz: ["a"] }
?flag1&flag2 -> { flag1: true, flag2: true }
?+flag1,-flag2 -> { flag1: true, flag2: false }
?xyz[]=a,xyz[]=b -> { xyz: ["a", "b"] }
?a%2C%26b=c%2C%26d -> { "a,&b": "c,&d" }
?{json:5,data:{a:1}} -> { json: 5, data: { a: 1 } }
null -> {}
? -> {}
?flag -> { flag: true }
?+flag -> { flag: true }
?-flag -> { flag: false }
?xyz=test -> { xyz: "test" }
?xyz=1 -> { xyz: "1" }
?xyz[]=a -> { xyz: ["a"] }
?flag1&flag2 -> { flag1: true, flag2: true }
?+flag1,-flag2 -> { flag1: true, flag2: false }
?xyz[]=a,xyz[]=b -> { xyz: ["a", "b"] }
?a%2C%26b=c%2C%26d -> { "a,&b": "c,&d" }
?{data:{a:1},isJSON5:true} -> { data: { a: 1 }, isJSON5: true }
```

### `stringifyRequest`
Expand Down

0 comments on commit ff86dd6

Please sign in to comment.