razzle-config-utils
Version:
Collection of useful utilities for razzle.config.js
204 lines (124 loc) • 6.91 kB
Markdown
# razzle-config-utils
razzle-config-utils is a set of utilities for razzle.config.js
If there's a snippet of code that you keep reusing in your own razzle config, consider sending a pull request!
## API
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
### Plugins
#### getPluginIndex
Retrieves plugin's index in webpack configuration or returns null
**Parameters**
- `config` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** webpack configuration
- `name` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Plugin name
Returns **([number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) | null)**
#### isPluginLoaded
Checks if plugin is loaded
**Parameters**
- `config` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** webpack configuration
- `name` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Plugin name
Returns **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
#### modifyPlugin
Modifies webpack plugin in place.
**Parameters**
- `config` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** webpack configuration
- `name` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Plugin name
- `fn` **[modifyPluginFn](#modifypluginfn)**
**Examples**
```javascript
modifyPlugin(appConfig, 'StartServerPlugin', (plugin) => {
plugin.options.nodeArgs = ['--inspect']
})
```
- Throws **any** an exception if it cannot find requested plugin
Returns **any** result of the callback
#### replacePlugin
Replaces webpack plugin. If the plugin cannot be found, it's ignored.
**Parameters**
- `config` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** webpack configuration
- `name` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Plugin name
- `plugin`
- `fn` **[modifyPluginFn](#modifypluginfn)**
**Examples**
```javascript
replacePlugin(appConfig, 'UglifyJsPlugin', new MinifyPlugin())
```
Returns **any** plugin
### Rules
#### getRuleIndexByTest
Retrieves rule's index in webpack configuration by test property
**Parameters**
- `config` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** webpack configuration
- `test` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** exact value of test property
Returns **([number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) | null)**
#### getRuleIndexByLoader
Retrieves rule's index in webpack configuration by loader.
Returns first found result.
**Parameters**
- `config` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** webpack configuration
- `loaderName`
- `loader` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** loader name
Returns **([number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) | null)**
#### modifyRule
Modifies webpack rule in place. You can pass either test property
(ex. `{ test: /\.css$/ })` or loader (ex. `{ loader: 'css-loader' }`)\`
to the second argument.
**Parameters**
- `config` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** webpack configuration
- `rule` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** webpack rule
- `rule.test` **([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp))** rule's test property
- `rule.loader` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** rule's loader
- `fn` **[modifyRuleFn](#modifyrulefn)**
**Examples**
```javascript
modifyRule(appConfig, { test: /\.css$/ }, (rule) => {
rule.test = /\.s?css/
rule.use.push({ loader: 'scss-loader' })
})
```
- Throws **any** an exception if it cannot find requested rule
Returns **any** result of the callback
#### replaceRule
Replaces webpack rule. You can pass either test property
(ex. `{ test: /\.css/ })` or loader (ex. `{ loader: 'css-loader' }`)\`
to the second argument. If the rule cannot be found, it's ignored.
**Parameters**
- `config` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** webpack configuration
- `$1` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `$1.test`
- `$1.loader`
- `rule`
- `fn` **[modifyRuleFn](#modifyrulefn)**
**Examples**
```javascript
replaceRule(appConfig, { test: /\.css$/ }, {
test: /\.s?css/,
use: [
// ... etc
]
})
```
Returns **any** rule
#### ignoreFileExtension
Adds file extension to the list of extensions ignored by file-loader
This is useful if you're adding support for something that Razzle does
not support yet (SASS/Less, Handlebars, you name it)
**Parameters**
- `config` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** webpack configuration
- `extension` **[RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp)** file extension
- `fn` **[modifyRuleFn](#modifyrulefn)**
**Examples**
```javascript
ignoreFileExtension(appConfig, /\.hbs$/)
```
Returns **any** rule
### modifyPluginFn
Function that will be invoked with found plugin
Type: [Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)
**Parameters**
- `plugin` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `plugin` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** 's index
### modifyRuleFn
Function that will be invoked with found rule.
Type: [Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)
**Parameters**
- `rule` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `rule` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** 's index