Skip to content

Commit

Permalink
fix: merge build and run time configs for overlapping props
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Jun 8, 2020
1 parent c5313ee commit 8db4a10
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 21 deletions.
4 changes: 2 additions & 2 deletions core/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from 'path';
import * as fs from 'fs';
import { sync as globSync } from 'glob';
import yargs from 'yargs';
import { Configuration } from '@component-controls/specification';
import { BuildConfiguration } from '@component-controls/specification';

export const buildConfigFileNames = [
'build.js',
Expand All @@ -14,7 +14,7 @@ export const buildConfigFileNames = [

export const optionsFileNames = ['runtime.js', 'options.js'];
export interface ConfigrationResult {
config: Configuration;
config: BuildConfiguration;
configPath: string;
optionsFilePath?: string;
}
Expand Down
5 changes: 5 additions & 0 deletions core/loader/src/replaceSource.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BuildConfiguration } from '@component-controls/specification';

export interface StoryPath {
absPath: string;
relPath: string;
Expand All @@ -6,13 +8,15 @@ export interface StoryPath {
export const replaceSource = (
stories: StoryPath[],
configFilePath: string | undefined,
config: BuildConfiguration | undefined,
hashKey: string,
) => {
const imports = `
const imports = {};
const configJSON = ${
configFilePath ? `require("${configFilePath}")` : 'undefined'
};
${stories
.map(story => `imports['${story.absPath}'] = require(${story.relPath});`)
.join('\n')}
Expand Down Expand Up @@ -73,6 +77,7 @@ ${stories
${imports}
${storeConst}
store.config = configJSON;
store.buildConfig = ${config ? JSON.stringify(config) : '{}'};
${loadStories}
${hmr}
${exports}
Expand Down
11 changes: 6 additions & 5 deletions core/loader/src/runtimeLoader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { deepMerge, defaultRunConfig } from '@component-controls/specification';
import {
deepMerge,
defaultBuildConfig,
} from '@component-controls/specification';

import { loadConfiguration, extractStories } from '@component-controls/config';
import { stringifyRequest } from 'loader-utils';
Expand All @@ -10,10 +13,7 @@ module.exports = function(content: string) {
const params = JSON.parse(this.query.slice(1));
//@ts-ignore
const config = loadConfiguration(this.rootContext, params.config);
store.config = deepMerge(
defaultRunConfig,
config?.optionsFilePath ? require(config?.optionsFilePath) : undefined,
);
store.buildConfig = deepMerge(defaultBuildConfig, config?.config);

const stories: StoryPath[] = (config ? extractStories(config) || [] : []).map(
fileName => ({
Expand All @@ -26,6 +26,7 @@ module.exports = function(content: string) {
content = replaceSource(
stories,
config?.optionsFilePath,
store.buildConfig,
`__COMPILATION_HASH__${params.compilationHash}`,
);
return content;
Expand Down
8 changes: 8 additions & 0 deletions core/loader/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
StoryComponents,
StoryPackages,
BuildConfiguration,
RunConfiguration,
StoriesDoc,
} from '@component-controls/specification';
Expand All @@ -10,6 +11,12 @@ export interface LoadingStore {
* global configuration from project config file
*/
config?: RunConfiguration;

/**
* global configuration from project config file
*/
buildConfig?: BuildConfiguration;

/**
* global store of packages
*/
Expand All @@ -33,6 +40,7 @@ class Store implements LoadingStore {
components: LoadingStore['components'] = {};
packages: LoadingStore['packages'] = {};
config: LoadingStore['config'] = {};
buildConfig: LoadingStore['buildConfig'] = {};
getDocs = () =>
this.stores
.filter(
Expand Down
37 changes: 25 additions & 12 deletions core/specification/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ export interface BuildConfiguration {
* example: "./stories/**/ /*.stories.(js|jsx|tsx|mdx)"
*/
stories?: string[];
/**
* base url path for API documentation pages. Default is "docs/"
*/
docsPath?: string;
/**
* base url path for blogs pages. Default is "blogs/"
*/
blogsPath?: string;
}

/**
Expand Down Expand Up @@ -61,30 +69,32 @@ export interface RunConfiguration {
* link to site image
*/
siteImage?: string;
/**
* base url path for API documentation pages. Default is "docs/"
*/
docsPath?: string;

/**
* Label for docs menu. Default is Docs
*/
docsLabel?: string;

/**
* base url path for blogs pages. Default is "blogs/"
*/
blogsPath?: string;

/**
* Label for blog menu. Default is Blog
*/
blogsLabel?: string;

/**
* story sorting function
*/
storySort?: (a: string, b: string) => number;

/**
* the following used in both run time and build time. Should be set in the build config and
* they will be carried to the run config
/**
* base url path for API documentation pages. Default is "docs/"
*/
docsPath?: string;
/**
* base url path for blogs pages. Default is "blogs/"
*/
blogsPath?: string;
}

export const defaultRunConfig: RunConfiguration = {
Expand All @@ -97,8 +107,11 @@ export const defaultRunConfig: RunConfiguration = {
'Component controls stories. Write your components documentation with MDX and JSX. Design, develop, test and review in a single site.',
siteLanguage: 'en',
author: '@component-controls',
docsPath: 'docs/',
docsLabel: 'Docs',
blogsPath: 'blogs/',
blogsLabel: 'Blog',
};

export const defaultBuildConfig: BuildConfiguration = {
docsPath: 'docs/',
blogsPath: 'blogs/',
};
3 changes: 2 additions & 1 deletion core/store/src/serialization/load-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const loadStoryStore = (
packages: loadedPackages,
components: loadedComponents,
config,
buildConfig,
} = newStore;

if (stores) {
Expand All @@ -35,7 +36,7 @@ export const loadStoryStore = (
stories: {},
components: {},
packages: {},
config: deepMerge(config, defaultRunConfig),
config: deepMerge(buildConfig, deepMerge(config, defaultRunConfig)),
};
stores.forEach(s => {
const storeDoc = s.doc;
Expand Down
1 change: 1 addition & 0 deletions examples/gatsby/.config/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ module.exports = {
'../src/stories/*.stories.(js|jsx|tsx|mdx)',
'../../../ui/components/src/**/*.stories.(js|jsx|tsx|mdx)',
],
docsPath: 'page/',
};
2 changes: 1 addition & 1 deletion integrations/gatsby-theme-stories/src/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports.createPages = async (
: await compile(config);
if (store) {
const docTemplate = require.resolve(`../src/templates/DocPage.tsx`);
const { docsPath = '', blogsPath = '' } = store.config || {};
const { docsPath = '', blogsPath = '' } = store.buildConfig || {};
const docs = store.getDocs();
docs.forEach(doc => {
createPage({
Expand Down

0 comments on commit 8db4a10

Please sign in to comment.