Skip to content

Commit 9806cd4

Browse files
committed
use cached version in prod mode, add ejs pre renderer loader
1 parent c92067b commit 9806cd4

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

core/loaders/preejs-loader.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
var path = require('path');
3+
var ejs = require(path.join(global.pathToApp, 'core/ejsWithHelpers.js'));
4+
5+
module.exports = function (source) {
6+
try {
7+
return ejs.render(source, {info: global.__currentSpecInfo__});
8+
} catch(e) {
9+
console.log('error prerendering webpack files', e);
10+
return source;
11+
}
12+
};

core/middleware/index.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,19 @@ var processRequest = function (req, res, next) {
3232
var outputPath = path.join(specPath, 'build/index.js');
3333
var insertReactTpl = fs.readFileSync(path.join(currentDir, '../templates/insert-react.ejs'), 'utf-8');
3434

35+
if (
36+
(process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'presentation') &&
37+
fs.existsSync(outputPath)
38+
) {
39+
req.specData.renderedHtml += '<script src="build/index.js"></script>';
40+
next();
41+
return;
42+
}
43+
3544
fs.outputFileSync(outputPath, ejs.render(insertReactTpl));
3645

37-
//if (false) {
46+
global.__currentSpecInfo__ = req.specData.info;
47+
3848
if (fs.existsSync(jsxFilePath)) {
3949
webpack({
4050
entry: outputPath,
@@ -45,14 +55,21 @@ var processRequest = function (req, res, next) {
4555
devtool: "#inline-source-map",
4656
module: {
4757
loaders: [
48-
{test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel-loader'}
58+
// TODO: load stripejs only for styleguide
59+
{test: /\.jsx?$/, loader: 'babel-loader!preejs'}
4960
]
5061
},
5162
resolveLoader: {
52-
root: path.join(currentDir, '../../node_modules')
63+
modulesDirectories: [
64+
path.join(currentDir, '../../node_modules'),
65+
path.join(currentDir, '../loaders'),
66+
'node_modules'
67+
]
5368
}
54-
}, function(err) {
69+
}, function(err, stats) {
5570
if (err) console.log('error', err);
71+
if (stats.compilation.errors) console.log(stats.compilation.errors.toString());
72+
if (stats.compilation.warnings) console.log(stats.compilation.warnings.toString());
5673

5774
next();
5875
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sourcejs-webpack",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "SourceJS webpack builder middleware.",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)