Skip to content

Commit ead8127

Browse files
committed
update components and webpack config generator
1 parent 5488472 commit ead8127

File tree

5 files changed

+93
-74
lines changed

5 files changed

+93
-74
lines changed
Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { Component, PropTypes } from 'react';
22
import Editor from 'components/Editor';
33
import Preview from 'components/Preview';
4-
5-
import s from './Playground.css';
4+
import ReactDOM from 'react-dom';
65

76
export default class Playground extends Component {
87
static propTypes = {
98
highlightTheme: PropTypes.string.isRequired,
109
code: PropTypes.string.isRequired,
1110
evalInContext: PropTypes.func.isRequired,
11+
index: PropTypes.string.isRequired
1212
}
1313

1414
constructor(props) {
@@ -24,6 +24,14 @@ export default class Playground extends Component {
2424
});
2525
}
2626

27+
componentDidMount() {
28+
this.renderEditor(this.props);
29+
}
30+
31+
componentDidUpdate() {
32+
this.renderEditor(this.props);
33+
}
34+
2735
componentWillReceiveProps(nextProps) {
2836
let { code } = nextProps;
2937
if (code) {
@@ -33,19 +41,32 @@ export default class Playground extends Component {
3341
}
3442
}
3543

44+
getEditorContainer() {
45+
return document.querySelector('.source_styleguidist_code__' + this.props.index);
46+
}
47+
48+
getEditorElement() {
49+
let { highlightTheme } = this.props;
50+
let { code } = this.state;
51+
52+
return (
53+
<Editor code={code} highlightTheme={highlightTheme} onChange={this.handleChange}/>
54+
);
55+
}
56+
57+
renderEditor() {
58+
var container = this.getEditorContainer();
59+
60+
if (container) {
61+
ReactDOM.unstable_renderSubtreeIntoContainer(this, this.getEditorElement(), this.getEditorContainer());
62+
}
63+
}
64+
3665
render() {
3766
let { code } = this.state;
38-
let { highlightTheme } = this.props;
3967

4068
return (
41-
<div className={s.root}>
42-
<div className={s.preview}>
43-
<Preview code={code} evalInContext={this.props.evalInContext}/>
44-
</div>
45-
<div className={s.editor}>
46-
<Editor code={code} highlightTheme={highlightTheme} onChange={this.handleChange}/>
47-
</div>
48-
</div>
69+
<Preview code={code} evalInContext={this.props.evalInContext}/>
4970
);
5071
}
5172
}

src/make-webpack-config.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
var fs = require('fs');
22
var path = require('path');
33
var webpack = require('webpack');
4-
var HtmlWebpackPlugin = require('html-webpack-plugin');
54
var ExtractTextPlugin = require('extract-text-webpack-plugin');
65
var AdvancedVariables = require('postcss-advanced-variables');
76
var merge = require('webpack-merge');
@@ -30,7 +29,8 @@ module.exports = function(env) {
3029
var webpackConfig = {
3130
output: {
3231
path: config.styleguideDir,
33-
filename: 'build/bundle.js'
32+
publicPath: '/',
33+
filename: config.bundlePath
3434
},
3535
resolve: {
3636
root: path.join(__dirname),
@@ -51,11 +51,6 @@ module.exports = function(env) {
5151
]
5252
},
5353
plugins: [
54-
new HtmlWebpackPlugin({
55-
title: config.title,
56-
template: config.template,
57-
inject: true
58-
}),
5954
new webpack.DefinePlugin({
6055
'process.env': {
6156
NODE_ENV: JSON.stringify(env)

src/utils/config.js

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,82 @@
1-
var fs = require('fs');
21
var path = require('path');
3-
var findup = require('findup');
2+
var fs = require('fs');
43
var minimist = require('minimist');
54
var prettyjson = require('prettyjson');
6-
var _ = require('lodash');
75
var utils = require('./server');
6+
var _ = require('lodash');
7+
8+
var sourceJSUtils;
9+
var globalConfig;
10+
var pathToSourceJSUser = '';
11+
12+
if (global.pathToApp) {
13+
sourceJSUtils = require(path.join(global.pathToApp, 'core/lib/utils'));
14+
globalConfig = global.opts.plugins && global.opts.plugins.reactStyleguidist ? global.opts.plugins.reactStyleguidist : {};
15+
16+
pathToSourceJSUser = global.userPath;
17+
}
18+
19+
var config = {
20+
enabled: true,
21+
bundlePath: 'build/bundle.js',
22+
23+
// Public object is exposed to Front-end via options API.
24+
public: {},
25+
26+
// Original styleguidist options
27+
rootDir: path.join(pathToSourceJSUser, 'specs'),
28+
components: './**/*.js',
29+
styleguideDir: path.join(pathToSourceJSUser, 'build/styleguide'),
830

9-
var CONFIG_FILENAME = 'styleguide.config.js';
10-
var DEFAULT_CONFIG = {
11-
rootDir: null,
12-
components: null,
13-
title: 'Style guide',
14-
styleguideDir: 'styleguide',
15-
template: path.join(__dirname, '../templates/index.html'),
16-
serverHost: 'localhost',
17-
serverPort: 3000,
1831
highlightTheme: 'base16-light',
1932
verbose: false,
2033
getExampleFilename: function(componentpath) {
21-
return path.join(path.dirname(componentpath), 'Readme.md');
34+
return path.join(path.dirname(componentpath), 'readme.md');
2235
},
2336
updateWebpackConfig: null
37+
38+
// Not used in SourceJS integration
39+
// title: 'Style guide',
40+
// template: path.join(__dirname, '../templates/index.html'),
41+
// serverHost: 'localhost',
42+
// serverPort: 3000,
2443
};
2544

45+
if (sourceJSUtils) {
46+
sourceJSUtils.extendOptions(config, globalConfig);
47+
}
48+
2649
function readConfig() {
2750
var argv = minimist(process.argv.slice(2));
2851
var configFilepath = findConfig(argv);
52+
var customConfig = {};
53+
var options = config;
2954

30-
var options = require(configFilepath);
31-
32-
validateConfig(options);
55+
if (configFilepath) {
56+
customConfig = require(configFilepath);
57+
}
3358

34-
var configDir = path.dirname(configFilepath);
35-
var rootDir = path.resolve(configDir, options.rootDir);
59+
options = _.merge({}, options, customConfig);
3660

37-
if (rootDir === configDir) {
38-
throw Error('Styleguidist: "rootDir" should not point to a folder with the Styleguidist config and node_modules folder');
39-
}
40-
if (!utils.isDirectoryExists(rootDir)) {
41-
throw Error('Styleguidist: "rootDir" directory not found: ' + rootDir);
61+
if (customConfig) {
62+
options.rootDir = path.resolve(path.dirname(configFilepath), options.rootDir);
4263
}
4364

44-
options = _.merge({}, DEFAULT_CONFIG, options);
45-
options = _.merge({}, options, {
46-
verbose: !!argv.verbose,
47-
rootDir: rootDir,
48-
styleguideDir: path.resolve(configDir, options.styleguideDir)
49-
});
65+
validateConfig(options);
5066

5167
if (options.verbose) {
5268
console.log();
53-
console.log('Using config file:', configFilepath);
5469
console.log(prettyjson.render(options));
5570
console.log();
5671
}
5772

73+
if (options.rootDir === global.userPath) {
74+
throw Error('Styleguidist: "rootDir" should not point to folder with node_modules');
75+
}
76+
if (!utils.isDirectoryExists(options.rootDir)) {
77+
throw Error('Styleguidist: "rootDir" directory not found: ' + options.rootDir);
78+
}
79+
5880
return options;
5981
}
6082

@@ -69,19 +91,6 @@ function findConfig(argv) {
6991

7092
return configFilepath;
7193
}
72-
else {
73-
// Search config file in all parent directories
74-
75-
var configDir;
76-
try {
77-
configDir = findup.sync(__dirname, CONFIG_FILENAME);
78-
}
79-
catch (e) {
80-
throw Error('Styleguidist config not found: ' + CONFIG_FILENAME + '.');
81-
}
82-
83-
return path.join(configDir, CONFIG_FILENAME);
84-
}
8594
}
8695

8796
function validateConfig(options) {

src/utils/utils.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
export function setComponentsNames(components) {
2-
components.map((component) => {
2+
Object.keys(components).forEach(key => {
3+
var component = components[key];
4+
35
let module = component.module;
46
component.name = module.displayName || module.name;
57
if (!component.name) {
68
throw Error(`Cannot detect component name for ${component.filepath}`);
79
}
810
});
11+
912
return components;
1013
}
1114

1215
export function globalizeComponents(components) {
13-
components.map((component) => {
16+
Object.keys(components).forEach(key => {
17+
var component = components[key];
18+
1419
global[component.name] = component.module;
1520
});
1621
}

test/examples.loader.spec.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,6 @@ describe('examples loader', () => {
7070
});
7171
});
7272

73-
describe('readExamples', () => {
74-
it('should separate code and html chunks', () => {
75-
let examplesMarkdown = '# header\n\n <div />\n\ntext';
76-
let examples = examplesLoader.readExamples(examplesMarkdown);
77-
expect(examples).to.have.length(3);
78-
expect(examples[0].type).to.equal('html');
79-
expect(examples[1].type).to.equal('code');
80-
expect(examples[2].type).to.equal('html');
81-
});
82-
});
83-
8473
describe('loader', () => {
8574
it('should return valid, parsable js', () => {
8675
let exampleMarkdown = '# header\n\n <div />\n\ntext';

0 commit comments

Comments
 (0)