-
-
Save leonid-bauxy/5e829251335f521c42547bff4d574064 to your computer and use it in GitHub Desktop.
./helpers/hoistLoaderIfOne.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import _ from 'lodash'; | |
/** | |
* @example | |
* | |
* Turns this: | |
* | |
* hoistLoaderIfOne({ | |
* test: /\.js$/, | |
* loaders: [ | |
* { | |
* loader: 'babel', | |
* } | |
* ] | |
* }) | |
* | |
* into this: | |
* { | |
* test: /\.js$/ | |
* loader: 'babel' | |
* } | |
* | |
* @param {(string|Object)[]|undefined} loaders | |
* @param {Object} topLoader | |
* @returns {Object} | |
*/ | |
export default function hoistLoaderIfOne({ loaders, ...topLoader }) { | |
if (loaders && loaders.length === 1) { | |
const firstLoader = loaders[0]; | |
if (_.isString(firstLoader)) { | |
return { | |
...topLoader, | |
loader: firstLoader, | |
}; | |
} | |
return { | |
...topLoader, | |
...firstLoader, | |
}; | |
} | |
return { | |
...topLoader, | |
loaders, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment