-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: webpack configs full configuration object
- Loading branch information
1 parent
f051248
commit 2504568
Showing
31 changed files
with
911 additions
and
461 deletions.
There are no files selected for viewing
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
# Table of contents | ||
|
||
- [Overview](#overview) | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [Basic usage](#basic-usage) | ||
- [Advanced usage](#advanced-usage) | ||
- [API](#api) | ||
- [merge](#merge) | ||
- [arrayMerge](#arraymerge) | ||
- [deepMerge](#deepmerge) | ||
- [deepMergeWithRules](#deepmergewithrules) | ||
- [getWebpackConfig](#getwebpackconfig) | ||
- [mergeWebpackConfig](#mergewebpackconfig) | ||
- [rulesFactory](#rulesfactory) | ||
- [RuleOptions](#ruleoptions) | ||
- [RuleType](#ruletype) | ||
- [RuleTypes](#ruletypes) | ||
|
||
# Overview | ||
|
||
Collection of standard webpack rules for [@component-controls/instrument](https://github.com/ccontrols/component-controls/tree/master/core/instrument). | ||
|
||
# Installation | ||
|
||
```bash | ||
$ npm install @component-controls/webpack-configs --save-dev | ||
``` | ||
|
||
# Usage | ||
|
||
in `.storybook/main.js` | ||
|
||
## Basic usage | ||
|
||
```js | ||
addons: [ | ||
... | ||
{ | ||
name: '@component-controls/storybook', | ||
options: { | ||
webpack: ['react-docgen-typescript'] | ||
} | ||
} | ||
], | ||
``` | ||
|
||
## Advanced usage | ||
|
||
addons: [ | ||
... | ||
{ | ||
name: '@component-controls/storybook', | ||
options: { | ||
webpack: [ | ||
'instrument', | ||
{ | ||
name: 'react-docgen-typescript', | ||
config: { | ||
module: { | ||
rules: [ | ||
{ | ||
loader: '@component-controls/loader/loader', | ||
options: { | ||
//instrumentation options | ||
prettier: { | ||
tabWidth: 4, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
], | ||
} | ||
}], | ||
|
||
# API | ||
|
||
<tsdoc-typescript files="@types/webpack/index.d.ts" entry="./src/index.ts,./src/types.ts"/> | ||
|
||
<!-- START-TSDOC-TYPESCRIPT --> | ||
|
||
## merge | ||
|
||
_defined in [@component-controls/webpack-configs/src/index.ts](https://github.com/ccontrols/component-controls/tree/master/core/webpack-configs/src/index.ts#L1)_ | ||
|
||
|
||
|
||
## arrayMerge | ||
|
||
_defined in [@component-controls/webpack-configs/src/index.ts](https://github.com/ccontrols/component-controls/tree/master/core/webpack-configs/src/index.ts#L17)_ | ||
|
||
**function** arrayMerge(`dest`\*: any\[], `src`\*: any\[]): any\[]; | ||
|
||
### parameters | ||
|
||
| Name | Type | Description | | ||
| --------- | ------ | ----------- | | ||
| `dest*` | any\[] | | | ||
| `src*` | any\[] | | | ||
| `returns` | any\[] | | | ||
|
||
## deepMerge | ||
|
||
_defined in [@component-controls/webpack-configs/src/index.ts](https://github.com/ccontrols/component-controls/tree/master/core/webpack-configs/src/index.ts#L44)_ | ||
|
||
**function** deepMerge(`dest`\*: any, `source`\*: any): any; | ||
|
||
### parameters | ||
|
||
| Name | Type | Description | | ||
| --------- | ---- | ----------- | | ||
| `dest*` | any | | | ||
| `source*` | any | | | ||
| `returns` | any | | | ||
|
||
## deepMergeWithRules | ||
|
||
_defined in [@component-controls/webpack-configs/src/index.ts](https://github.com/ccontrols/component-controls/tree/master/core/webpack-configs/src/index.ts#L36)_ | ||
|
||
**function** deepMergeWithRules(`dest`\*: any, `source`\*: any): any; | ||
|
||
### parameters | ||
|
||
| Name | Type | Description | | ||
| --------- | ---- | ----------- | | ||
| `dest*` | any | | | ||
| `source*` | any | | | ||
| `returns` | any | | | ||
|
||
## getWebpackConfig | ||
|
||
expands the rules into webpack rules | ||
|
||
_defined in [@component-controls/webpack-configs/src/index.ts](https://github.com/ccontrols/component-controls/tree/master/core/webpack-configs/src/index.ts#L55)_ | ||
|
||
**function** getWebpackConfig(`custom`\*: [RuleTypes](#ruletypes)): [Configuration](#configuration); | ||
|
||
### parameters | ||
|
||
| Name | Type | Description | | ||
| --------- | ------------------------------- | -------------- | | ||
| `custom*` | [RuleTypes](#ruletypes) | custom config | | ||
| `returns` | [Configuration](#configuration) | | | ||
|
||
## mergeWebpackConfig | ||
|
||
merge webpack config with custom set of rules | ||
|
||
_defined in [@component-controls/webpack-configs/src/index.ts](https://github.com/ccontrols/component-controls/tree/master/core/webpack-configs/src/index.ts#L84)_ | ||
|
||
**function** mergeWebpackConfig(`webPack`\*: [Configuration](#configuration), `custom`\*: [RuleTypes](#ruletypes)): [Configuration](#configuration); | ||
|
||
### parameters | ||
|
||
| Name | Type | Description | | ||
| ---------- | ------------------------------- | ---------------------------------- | | ||
| `webPack*` | [Configuration](#configuration) | passed configuration to merge with | | ||
| `custom*` | [RuleTypes](#ruletypes) | custom config | | ||
| `returns` | [Configuration](#configuration) | | | ||
|
||
## rulesFactory | ||
|
||
_defined in [@component-controls/webpack-configs/src/index.ts](https://github.com/ccontrols/component-controls/tree/master/core/webpack-configs/src/index.ts#L9)_ | ||
|
||
|
||
|
||
### properties | ||
|
||
| Name | Type | Description | | ||
| -------------------------- | ------------------------------- | ----------- | | ||
| `instrument*` | [Configuration](#configuration) | | | ||
| `react-docgen*` | [Configuration](#configuration) | | | ||
| `react-docgen-typescript*` | [Configuration](#configuration) | | | ||
|
||
## RuleOptions | ||
|
||
_defined in [@component-controls/webpack-configs/src/types.ts](https://github.com/ccontrols/component-controls/tree/master/core/webpack-configs/src/types.ts#L3)_ | ||
|
||
|
||
|
||
### properties | ||
|
||
| Name | Type | Description | | ||
| --------- | ------------------------------- | ----------- | | ||
| `config*` | [Configuration](#configuration) | | | ||
| `name*` | string | | | ||
|
||
## RuleType | ||
|
||
_defined in [@component-controls/webpack-configs/src/types.ts](https://github.com/ccontrols/component-controls/tree/master/core/webpack-configs/src/types.ts#L7)_ | ||
|
||
string | [RuleOptions](#ruleoptions) | ||
|
||
## RuleTypes | ||
|
||
_defined in [@component-controls/webpack-configs/src/types.ts](https://github.com/ccontrols/component-controls/tree/master/core/webpack-configs/src/types.ts#L9)_ | ||
|
||
[RuleType](#ruletype)\[] | ||
|
||
<!-- END-TSDOC-TYPESCRIPT --> |
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
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
const merge = require('deepmerge'); | ||
import { Configuration } from 'webpack'; | ||
import { instrument } from './instrument'; | ||
import { reactDocgen } from './react-docgen'; | ||
import { reactDocgenTypescript } from './react-docgen-typescript'; | ||
import { RuleOptions, RuleTypes, RuleType } from './types'; | ||
export * from './types'; | ||
|
||
export const rulesFactory: { | ||
[key: string]: Configuration; | ||
} = { | ||
'react-docgen-typescript': reactDocgenTypescript, | ||
'react-docgen': reactDocgen, | ||
instrument, | ||
}; | ||
|
||
const arrayMerge = (dest: any[], src: any[]) => { | ||
const srcCache = src ? [...src] : []; | ||
const destItems = dest | ||
? dest.reduce((acc, item) => { | ||
// merge rules by loader or test | ||
const srcItemIdx = srcCache.findIndex( | ||
d => d.test === item.test || d.loader === item.loader, | ||
); | ||
if (srcItemIdx >= 0) { | ||
const srcItem = srcCache[srcItemIdx]; | ||
srcCache.splice(srcItemIdx, 1); | ||
return [...acc, deepMerge(srcItem, item)]; | ||
} | ||
return [...acc, item]; | ||
}, []) | ||
: src || []; | ||
return [...destItems, ...srcCache]; | ||
}; | ||
|
||
const deepMergeWithRules = (dest: any, source: any) => { | ||
return dest && source | ||
? merge(dest, source, { | ||
arrayMerge: arrayMerge, | ||
}) | ||
: source || dest || {}; | ||
}; | ||
|
||
const deepMerge = (dest: any, source: any) => { | ||
return dest && source | ||
? merge(dest, source, { | ||
arrayMerge: (d: any[], s: any[]) => (d && s ? [...d, ...s] : d || s), | ||
}) | ||
: source || dest || {}; | ||
}; | ||
/** | ||
* expands the rules into webpack rules | ||
* @param custom custom config | ||
*/ | ||
export const getWebpackConfig = (custom: RuleTypes): Configuration => { | ||
const result: Configuration = custom.reduce( | ||
(acc: Configuration, config: RuleType) => { | ||
if (typeof config === 'string') { | ||
const f = rulesFactory[config]; | ||
return deepMergeWithRules(acc, f); | ||
} | ||
if (config && (config as RuleOptions).name) { | ||
const name = (config as RuleOptions).name; | ||
if (rulesFactory[name]) { | ||
const r: Configuration = rulesFactory[name]; | ||
const o: Configuration = (config as RuleOptions).config; | ||
const merged: Configuration[] = deepMergeWithRules(r, o); | ||
return deepMergeWithRules(acc, merged); | ||
} | ||
} | ||
return deepMergeWithRules(acc, config); | ||
}, | ||
{}, | ||
); | ||
return result; | ||
}; | ||
|
||
/** | ||
* merge webpack config with custom set of rules | ||
* @param webPack passed configuration to merge with | ||
* @param custom custom config | ||
*/ | ||
|
||
export const mergeWebpackConfig = ( | ||
webPack: Configuration, | ||
custom: RuleTypes, | ||
): Configuration => { | ||
const newConfig = getWebpackConfig(custom); | ||
return deepMerge(webPack, newConfig); | ||
}; |
Oops, something went wrong.