Skip to content

Commit

Permalink
use cached version in prod mode, add ejs pre renderer loader
Browse files Browse the repository at this point in the history
  • Loading branch information
robhrt7 committed Oct 6, 2015
1 parent c92067b commit 9806cd4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
12 changes: 12 additions & 0 deletions core/loaders/preejs-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

var path = require('path');
var ejs = require(path.join(global.pathToApp, 'core/ejsWithHelpers.js'));

module.exports = function (source) {
try {
return ejs.render(source, {info: global.__currentSpecInfo__});
} catch(e) {
console.log('error prerendering webpack files', e);
return source;
}
};
25 changes: 21 additions & 4 deletions core/middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,19 @@ var processRequest = function (req, res, next) {
var outputPath = path.join(specPath, 'build/index.js');
var insertReactTpl = fs.readFileSync(path.join(currentDir, '../templates/insert-react.ejs'), 'utf-8');

if (
(process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'presentation') &&
fs.existsSync(outputPath)
) {
req.specData.renderedHtml += '<script src="build/index.js"></script>';
next();
return;
}

fs.outputFileSync(outputPath, ejs.render(insertReactTpl));

//if (false) {
global.__currentSpecInfo__ = req.specData.info;

if (fs.existsSync(jsxFilePath)) {
webpack({
entry: outputPath,
Expand All @@ -45,14 +55,21 @@ var processRequest = function (req, res, next) {
devtool: "#inline-source-map",
module: {
loaders: [
{test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel-loader'}
// TODO: load stripejs only for styleguide
{test: /\.jsx?$/, loader: 'babel-loader!preejs'}
]
},
resolveLoader: {
root: path.join(currentDir, '../../node_modules')
modulesDirectories: [
path.join(currentDir, '../../node_modules'),
path.join(currentDir, '../loaders'),
'node_modules'
]
}
}, function(err) {
}, function(err, stats) {
if (err) console.log('error', err);
if (stats.compilation.errors) console.log(stats.compilation.errors.toString());
if (stats.compilation.warnings) console.log(stats.compilation.warnings.toString());

next();
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sourcejs-webpack",
"version": "0.1.0",
"version": "0.2.0",
"description": "SourceJS webpack builder middleware.",
"repository": {
"type": "git",
Expand Down

0 comments on commit 9806cd4

Please sign in to comment.