Skip to content

Commit 197c993

Browse files
committed
move production build to separate util
1 parent 5c360cc commit 197c993

File tree

7 files changed

+59
-29
lines changed

7 files changed

+59
-29
lines changed

contributing.md

-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ function foo(bar, fum) {
6060
- Don’t commit generated files: compiled from Stylus CSS, minified JavaScript, etc.
6161
- Don’t change version number and changelog.
6262
- Install [EditorConfig](http://editorconfig.org/) plugin for your code editor.
63-
- Feel free to [ask me](http://sapegin.me) anything you need.
64-
6563

6664
## Building and running tests
6765

core/build.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var path = require('path');
2+
var express = require('express');
3+
var webpack = require('webpack');
4+
5+
global.userPath = global.userPath || process.cwd();
6+
global.pathToApp = global.pathToApp || path.join(process.cwd(), 'node_modules/sourcejs');
7+
8+
var makeWebpackConfig = require('../src/make-webpack-config.js');
9+
var config = require('../src/utils/config');
10+
var env = process.env.NODE_ENV || 'development';
11+
12+
console.log('ReactStyleguidist: webpack building '+ env +' bundle...');
13+
14+
webpack(makeWebpackConfig(process.env.NODE_ENV), function (err, stats) {
15+
if (err) {
16+
console.error('ReactStyleguidist: error building webpack bundle', err, stats);
17+
}
18+
19+
console.log('ReactStyleguidist: webpack '+ env +' build done');
20+
});

core/index.js

+3-11
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,12 @@ var config = require('../src/utils/config');
77
if (config.enabled) {
88
var compiler = webpack(makeWebpackConfig(env));
99

10-
if (env === 'development') {
10+
if (env !== 'production') {
1111
global.app.use(require('webpack-dev-middleware')(compiler, {
1212
noInfo: true
1313
}));
1414
global.app.use(require('webpack-hot-middleware')(compiler));
15-
} else {
16-
console.log('webpack building production styleguidist bundle...');
17-
18-
webpack(makeWebpackConfig('production'), function(err, stats) {
19-
if (err) {
20-
console.log(err, stats);
21-
}
22-
23-
console.log('webpack production styleguidist build done');
24-
});
15+
} else if (config.preBuild) {
16+
require('./build.js');
2517
}
2618
}

core/middleware/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ var processRequest = function (req, res, next) {
1919
var pathToStyleguide = config._styleguideDir.replace(/^\./, '');
2020
var pathToBundle = config.bundlePath.replace(/^\./, '');
2121

22-
if (global.MODE === 'development') {
23-
append = '<script src="/'+ config.bundlePath +'"></script>'
24-
} else {
22+
if (global.MODE === 'production') {
2523
append = '<link rel="stylesheet" href="' + pathToStyleguide + '/build/styles.css"><script src="' + pathToStyleguide + pathToBundle + '"></script>'
24+
} else {
25+
append = '<script src="'+ pathToBundle +'"></script>'
2626
}
2727

2828
req.specData.renderedHtml += append;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "SourceJS integration plugin for react-styleuguidist",
44
"version": "0.3.2",
55
"homepage": "https://github.com/sourcejs/sourcejs-react-styleguidist",
6-
"contributirs": [
6+
"contributors": [
77
{
88
"name": "Artem Sapegin",
99
"url": "http://sapegin.me/"

readme.md

+25-4
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ Examples are written in Markdown where any code blocks will be rendered as a rea
6969

7070
## How it works
7171

72-
SourceJS plugins are loaded together with main applications, adding additional initialization steps or changing rendering flow using middleware integration. With this plugin, in development mode, SourceJS in enhanced with webpack middleware, that builds all the React examples on demand and listens to file changes for hot-reloading.
72+
SourceJS plugins are loaded together with main application, adding additional initialization steps or changing rendering flow using middleware integration. With this plugin, in development mode, SourceJS in enhanced with webpack middleware, that builds all the React examples on demand and listens to file changes for hot-reloading.
7373

74-
Running app with `NODE_ENV=production`, webpack will be triggered only once to build a static, minified version, which is done also on app start.
74+
In production mode webpack is not triggered, expecting that bundle is already built (read configuration section for more).
7575

7676
Rendering flow with this plugins looks like this:
7777

@@ -86,6 +86,14 @@ Rendering flow with this plugins looks like this:
8686

8787
Use SourceJS `options.js` for deep plugin configuration.
8888

89+
* **`enabled`**
90+
Type: `Boolean`
91+
Set to `false`, if wan't to disable plugin load with SourceJS app.
92+
93+
* **`preBuild`**
94+
Type: `Boolean`
95+
Set to `true`, if you wan't to automatically build webpack bundle in production mode right after app starts.
96+
8997
* **`rootDir`**
9098
Type: `String`, required
9199
Your components sources root folder (eg. `./lib`). Should not point to a folder with the `node_modules` folder.
@@ -140,12 +148,25 @@ Use SourceJS `options.js` for deep plugin configuration.
140148
}
141149
```
142150

151+
### Environment settings
152+
153+
Running app with `NODE_ENV=production`, initial webpack build won't be triggered. To properly prepare production environment, first run react-styleguidist build command, and only after that run application:
154+
155+
```
156+
NODE_ENV=production node ./node_modules/sourcejs-react-styleguidist/core/build.js
157+
NODE_ENV=production npm start
158+
```
159+
160+
Note: this command should be ran from SourceJS root folder, where `node_modules` is placed.
161+
162+
Alternatively, you can set `preBuild` to true in plugin configuration, to build webpack bundle once app is ran in production mode. This will require less build steps, but may cause higher load in production environment container.
163+
143164
## Contributing
144165

145-
Everyone is welcome to contribute. Please take a moment to review the [contributing guidelines](Contributing.md).
166+
Everyone is welcome to contribute. Please take a moment to review the [contributing guidelines](contributing.md).
146167

147168
---
148169

149170
## License
150171

151-
The MIT License, see the included [License.md](License.md) file.
172+
The MIT License, see the included [license.md](license.md) file.

src/utils/config.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var pathToSourceJSUser = '';
1111

1212
if (global.pathToApp) {
1313
sourceJSUtils = require(path.join(global.pathToApp, 'core/lib/utils'));
14-
globalConfig = global.opts.plugins && global.opts.plugins.reactStyleguidist ? global.opts.plugins.reactStyleguidist : {};
14+
globalConfig = global.opts && global.opts.plugins && global.opts.plugins.reactStyleguidist ? global.opts.plugins.reactStyleguidist : {};
1515

1616
pathToSourceJSUser = global.userPath;
1717
}
@@ -57,7 +57,6 @@ function readConfig() {
5757
options = _.merge({}, options, customConfig);
5858
options.rootDir = path.resolve(path.dirname(configFilepath), options.rootDir);
5959
} else {
60-
options._rootDir = config.rootDir;
6160
options.rootDir = path.join(pathToSourceJSUser, options.rootDir);
6261

6362
options._styleguideDir = config.styleguideDir;
@@ -73,10 +72,10 @@ function readConfig() {
7372
}
7473

7574
if (options.rootDir === global.userPath) {
76-
throw Error('Styleguidist: "rootDir" should not point to folder with node_modules');
75+
throw Error('ReactStyleguidist: "rootDir" should not point to folder with node_modules');
7776
}
7877
if (!utils.isDirectoryExists(options.rootDir)) {
79-
throw Error('Styleguidist: "rootDir" directory not found: ' + options.rootDir);
78+
throw Error('ReactStyleguidist: "rootDir" directory not found: ' + options.rootDir);
8079
}
8180

8281
return options;
@@ -97,16 +96,16 @@ function findConfig(argv) {
9796

9897
function validateConfig(options) {
9998
if (!options.rootDir) {
100-
throw Error('Styleguidist: "rootDir" options is required.');
99+
throw Error('ReactStyleguidist: "rootDir" options is required.');
101100
}
102101
if (!options.components) {
103-
throw Error('Styleguidist: "components" options is required.');
102+
throw Error('ReactStyleguidist: "components" options is required.');
104103
}
105104
if (options.getExampleFilename && typeof options.getExampleFilename !== 'function') {
106-
throw Error('Styleguidist: "getExampleFilename" options must be a function.');
105+
throw Error('ReactStyleguidist: "getExampleFilename" options must be a function.');
107106
}
108107
if (options.updateWebpackConfig && typeof options.updateWebpackConfig !== 'function') {
109-
throw Error('Styleguidist: "updateWebpackConfig" options must be a function.');
108+
throw Error('ReactStyleguidist: "updateWebpackConfig" options must be a function.');
110109
}
111110
}
112111

0 commit comments

Comments
 (0)