Skip to content

Commit 0f19ace

Browse files
authored
Merge pull request #13 from rollup-umd/dev
feat(options): adding options.js for hooking options
2 parents 5683c22 + 30ce774 commit 0f19ace

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

docs/layout-package/layout-package-create.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Creating a layout package is easy
77
```bash
88
lib
99
├── styleguide.config.js
10+
├── options.js
1011
├── favicons
1112
│   └── index.js
1213
├── Layout
@@ -22,6 +23,24 @@ lib
2223

2324
> If you don't respect this file structure, you can use [`createConfig`](#create-config) options to set the new file structure.
2425
26+
**`styleguide.config.js`**
27+
28+
It is react-styleguidist configuration that will be applied in ours.
29+
30+
It must return an `Object`.
31+
32+
**`options.js`**
33+
34+
It is $PACKAGE_NAME options that will be applied by ours after autoconfiguration, which means path for autoconfiguration cannot be reconfigured through this.
35+
36+
It must return an `Object`.
37+
38+
```js static
39+
export default {
40+
themeColor: 'rgb(46, 166, 152)',
41+
}
42+
```
43+
2544
**`favicons/index.js`**
2645

2746
Your file must look like this:

src/createConfig.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const defaultOptions = {
2121
styleGuideDirPath: 'styleguide',
2222
extensionFile: 'styleguide.ext.json',
2323
setupFile: 'setup.js',
24+
optionsPath: 'lib/options.js',
2425
licensePath: 'LICENSE.md',
2526
locale: 'en',
2627
loader: Object.keys(defaultLoaders)[0],
@@ -45,19 +46,20 @@ export const webpackMerge = merge;
4546
* @param thisPkg
4647
* @param base
4748
* @param opts
48-
* @returns {{finalStyleGuidePath: {}, finalWrapperPath: {}, finalConfigExtension: {}, finalFaviconExtension: {}}}
49+
* @returns {{finalStyleGuidePath: {}, finalWrapperPath: {}, finalConfigExtension: {}, finalFaviconExtension: {}, finalOptionsExtension: {}}}
4950
*/
5051
function retrieveComponentsApiPath(defaultStyleGuidePath, defaultWrapperPath, pkg, thisPkg, base, opts) {
5152
let finalStyleGuidePath = defaultStyleGuidePath;
5253
let finalWrapperPath = defaultWrapperPath;
5354
let finalConfigExtension = {};
5455
let finalLoadersExtension = {};
5556
let finalFaviconExtension = {};
57+
let finalOptionsExtension = {};
5658

5759
// get them dynamically, it must include documentation in the name, and be in devDependencies or dependencies
60+
let found = false;
5861
if (!opts.disableAutoConf) {
5962
[pkg.dependencies, pkg.devDependencies].forEach((o) => {
60-
let found = false;
6163
Object.keys(o).forEach((dep) => {
6264
if (dep.includes('documentation') && !dep.includes(thisPkg.name)) {
6365
const { keywords } = require(join(base, 'node_modules', dep, 'package.json')); // eslint-disable-line
@@ -82,6 +84,10 @@ function retrieveComponentsApiPath(defaultStyleGuidePath, defaultWrapperPath, pk
8284
finalFaviconExtension = require(join(base, 'node_modules', dep, opts.faviconConfigPath)); // eslint-disable-line global-require
8385
found = dep;
8486
}
87+
if (existsSync(join(base, 'node_modules', dep, opts.optionsPath))) {
88+
finalOptionsExtension = require(join(base, 'node_modules', dep, opts.optionsPath)); // eslint-disable-line global-require
89+
found = dep;
90+
}
8591
}
8692
}
8793
});
@@ -92,8 +98,7 @@ function retrieveComponentsApiPath(defaultStyleGuidePath, defaultWrapperPath, pk
9298
}
9399

94100
// get them from on options
95-
if (opts.layout && existsSync(join(base, 'node_modules', opts.layout))) {
96-
let found = false;
101+
if (!found && opts.layout && existsSync(join(base, 'node_modules', opts.layout))) {
97102
if (existsSync(join(base, 'node_modules', opts.layout, opts.layoutPath))) {
98103
finalStyleGuidePath = join(base, 'node_modules', opts.layout, opts.layoutPath); // eslint-disable-line global-require
99104
found = true;
@@ -114,6 +119,10 @@ function retrieveComponentsApiPath(defaultStyleGuidePath, defaultWrapperPath, pk
114119
finalFaviconExtension = require(join(base, 'node_modules', opts.layout, opts.faviconConfigPath)); // eslint-disable-line global-require
115120
found = true;
116121
}
122+
if (existsSync(join(base, 'node_modules', opts.layout, opts.optionsPath))) {
123+
finalOptionsExtension = require(join(base, 'node_modules', opts.layout, opts.optionsPath)); // eslint-disable-line global-require
124+
found = true;
125+
}
117126
if (!found) {
118127
console.log(`We cannot find the layout for package ${opts.layout}, it must be installed.`); // eslint-disable-line no-console
119128
}
@@ -135,6 +144,7 @@ function retrieveComponentsApiPath(defaultStyleGuidePath, defaultWrapperPath, pk
135144
finalConfigExtension,
136145
finalLoadersExtension,
137146
finalFaviconExtension,
147+
finalOptionsExtension,
138148
};
139149
}
140150

@@ -155,6 +165,7 @@ function retrieveComponentsApiPath(defaultStyleGuidePath, defaultWrapperPath, pk
155165
* @param {string} [options.licensePath=LICENSE.md] options.licensePath - Location of the license within your project
156166
* @param {string} [options.locale=en] options.locale - Locale used for the documentation
157167
* @param {string} [options.loader=wave] options.loader - Loader to be used for the documentation
168+
* @param {string} [options.optionsPath=lib/options.js] options.optionsPath - Object of options that will be hooked and used for generating the configuration. This options **can't** contains options related to path and autoconfigrauton, it can **only** exploit features that happen after.
158169
* @param {Object} [options.loaders={ wave: '<!-- content of wave loader >' }] options.loaders - object available for use (if layout package is installed, they will be automatically added during autoconfiguration)
159170
* @param {boolean} [options.loaderInnerApp=true] options.loaderInnerApp - If set to false, the loader will be injected in the main html outside of the react application context
160171
* @param {string} [options.favicon=null] options.favicon - favicon name
@@ -179,7 +190,7 @@ export function createConfig(config = {}, options = {}) {
179190
styleguideComponents: userStyleguideComponents,
180191
...userConfig
181192
} = config;
182-
const opts = { ...defaultOptions, ...options };
193+
let opts = { ...defaultOptions, ...options };
183194
const cwd = process.cwd();
184195

185196
const base = existsSync(join(cwd, 'package.json')) ? join(cwd) : join(__dirname, '..');
@@ -211,6 +222,7 @@ export function createConfig(config = {}, options = {}) {
211222
finalConfigExtension,
212223
finalLoadersExtension,
213224
finalFaviconExtension,
225+
finalOptionsExtension,
214226
} = retrieveComponentsApiPath(defaultStyleGuidePath, defaultWrapperPath, pkg, thisPkg, base, opts);
215227

216228
// Prepare to apply custom conf
@@ -221,6 +233,12 @@ export function createConfig(config = {}, options = {}) {
221233
...finalConfig
222234
} = finalConfigExtension;
223235

236+
// merge options
237+
opts = {
238+
...opts,
239+
...finalOptionsExtension,
240+
};
241+
224242
// Prepare loaders
225243
const loaders = {
226244
...opts.loaders,

0 commit comments

Comments
 (0)