Skip to content

Commit 47bc1af

Browse files
committed
Basic Node API (styleguidist#183).
1 parent 9f1c8d5 commit 47bc1af

7 files changed

Lines changed: 127 additions & 2 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"url": "https://github.com/sapegin/react-styleguidist/issues"
1616
},
1717
"license": "MIT",
18-
"main": "scripts/build.js",
18+
"main": "scripts/index.js",
1919
"bin": {
2020
"styleguidist": "bin/styleguidist.js"
2121
},

scripts/build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const webpack = require('webpack');
44
const makeWebpackConfig = require('./make-webpack-config');
55

66
module.exports = function build(config, callback) {
7-
webpack(makeWebpackConfig(config, 'production'), (err, stats) => {
7+
return webpack(makeWebpackConfig(config, 'production'), (err, stats) => {
88
// require('fs').writeFileSync('stats.json', JSON.stringify(stats.toJson()));
99
callback(err, stats);
1010
});

scripts/index.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const build = require('./build');
4+
const server = require('./server');
5+
const makeWebpackConfig = require('./make-webpack-config');
6+
const getConfig = require('./config');
7+
8+
module.exports = {
9+
/**
10+
* Build style guide.
11+
*
12+
* @param {object} config Styleguidist config.
13+
* @param {Function} callback callback(err, config, stats).
14+
* @return {Compiler} Webpack Compiler instance.
15+
*/
16+
build(config, callback) {
17+
config = getConfig(config);
18+
return build(config, (err, stats) => callback(err, config, stats));
19+
},
20+
21+
/**
22+
* Start style guide dev server.
23+
*
24+
* @param {object} config Styleguidist config.
25+
* @param {Function} callback callback(err, config).
26+
* @return {Compiler} Webpack Compiler instance.
27+
*/
28+
server(config, callback) {
29+
config = getConfig(config);
30+
return server(config, err => callback(err, config));
31+
},
32+
33+
/**
34+
* Return Styleguidist Webpack config.
35+
*
36+
* @param {object} config Styleguidist config.
37+
* @param {string} env 'production' or 'development'.
38+
* @return {object}
39+
*/
40+
makeWebpackConfig(config, env) {
41+
config = getConfig(config);
42+
return makeWebpackConfig(config, env);
43+
},
44+
};

scripts/server.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ module.exports = function server(config, callback) {
2222
}
2323

2424
app.listen(config.serverPort, config.serverHost, callback);
25+
26+
return compiler;
2527
};

test/run.build.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const path = require('path');
2+
const styleguidist = require('../scripts');
3+
4+
/* eslint-disable no-console */
5+
6+
styleguidist.build({
7+
config: path.resolve(__dirname, '../examples/basic/styleguide.config.js'),
8+
}, (err, config) => {
9+
if (err) {
10+
console.log(err);
11+
}
12+
else {
13+
console.log('Style guide published to', config.styleguideDir);
14+
}
15+
});

test/run.server.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const path = require('path');
2+
const glob = require('glob');
3+
const styleguidist = require('../scripts');
4+
5+
/* eslint-disable no-console */
6+
7+
styleguidist.server({
8+
components() {
9+
return glob.sync(path.resolve(__dirname, '../examples/basic/lib/components/**/*.js'))
10+
.filter(module => /\/[A-Z]\w*\.js$/.test(module))
11+
;
12+
},
13+
updateWebpackConfig(webpackConfig) {
14+
const dir = path.resolve(__dirname, '../examples/basic/lib');
15+
webpackConfig.module.loaders.push(
16+
{
17+
test: /\.jsx?$/,
18+
include: dir,
19+
loader: 'babel',
20+
},
21+
{
22+
test: /\.css$/,
23+
include: dir,
24+
loader: 'style!css?modules&importLoaders=1',
25+
},
26+
{
27+
test: /\.json$/,
28+
include: path.dirname(require.resolve('dog-names/package.json')),
29+
loader: 'json',
30+
}
31+
);
32+
return webpackConfig;
33+
},
34+
}, (err, config) => {
35+
if (err) {
36+
console.log(err);
37+
}
38+
else {
39+
console.log('Listening at http://' + config.serverHost + ':' + config.serverPort);
40+
}
41+
});

test/scripts.index.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import test from 'ava';
2+
import styleguidist from '../scripts';
3+
4+
test('should have API methods', t => {
5+
t.truthy(styleguidist);
6+
t.is(typeof styleguidist.build, 'function');
7+
t.is(typeof styleguidist.server, 'function');
8+
t.is(typeof styleguidist.makeWebpackConfig, 'function');
9+
});
10+
11+
test('makeWebpackConfig should return development Webpack config', t => {
12+
const result = styleguidist.makeWebpackConfig({ components: '*.js' }, 'development');
13+
t.truthy(result);
14+
t.is(result.output.filename, 'build/bundle.js');
15+
t.true(result.cache);
16+
});
17+
18+
test('makeWebpackConfig should return production Webpack config', t => {
19+
const result = styleguidist.makeWebpackConfig({ components: '*.js' }, 'production');
20+
t.truthy(result);
21+
t.is(result.output.filename, 'build/bundle.js');
22+
t.false(result.cache);
23+
});

0 commit comments

Comments
 (0)