Skip to content

Javascript ES6 preprocessor with Babel #940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ var AngularFullstackGenerator = yeoman.generators.Base.extend({

return filterMap[val];
}
}, {
type: "confirm",
name: "babel",
message: "Would you like to use Javascript ES6 in your client by preprocessing it with Babel?",
when: function (answers) {
return answers.script === 'js';
}
}, {
type: "list",
name: "markup",
Expand Down Expand Up @@ -110,6 +117,9 @@ var AngularFullstackGenerator = yeoman.generators.Base.extend({
return answers.bootstrap;
}
}], function (answers) {

this.filters.babel = !!answers.babel;
if(this.filters.babel){ this.filters.js = true; }
this.filters[answers.script] = true;
this.filters[answers.markup] = true;
this.filters[answers.stylesheet] = true;
Expand Down Expand Up @@ -211,6 +221,7 @@ var AngularFullstackGenerator = yeoman.generators.Base.extend({

if(this.filters.ngroute) filters.push('ngroute');
if(this.filters.uirouter) filters.push('uirouter');
if(this.filters.babel) extensions.push('babel');
if(this.filters.coffee) extensions.push('coffee');
if(this.filters.js) extensions.push('js');
if(this.filters.html) extensions.push('html');
Expand Down
52 changes: 45 additions & 7 deletions app/templates/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ module.exports = function (grunt) {
'<%%= yeoman.client %>/{app,components}/**/*.spec.{coffee,litcoffee,coffee.md}'
],
tasks: ['karma']
},<% } %><% if(filters.babel) { %>
babel: {
files: [
'<%%= yeoman.client %>/{app,components}/**/*.js',
'!<%%= yeoman.client %>/{app,components}/**/*.spec.js'
],
tasks: ['babel']
},<% } %>
gruntfile: {
files: ['Gruntfile.js']
Expand All @@ -135,7 +142,11 @@ module.exports = function (grunt) {
files: [
'{.tmp,<%%= yeoman.client %>}/{app,components}/**/*.css',
'{.tmp,<%%= yeoman.client %>}/{app,components}/**/*.html',
<% if(filters.babel) { %>
'.tmp/{app,components}/**/*.js',
<% } else { %>
'{.tmp,<%%= yeoman.client %>}/{app,components}/**/*.js',
<% } %>
'!{.tmp,<%%= yeoman.client %>}{app,components}/**/*.spec.js',
'!{.tmp,<%%= yeoman.client %>}/{app,components}/**/*.mock.js',
'<%%= yeoman.client %>/assets/images/{,*//*}*.{png,jpg,jpeg,gif,webp,svg}'
Expand Down Expand Up @@ -442,14 +453,16 @@ module.exports = function (grunt) {
// Run some tasks in parallel to speed up the build process
concurrent: {
server: [<% if(filters.coffee) { %>
'coffee',<% } %><% if(filters.jade) { %>
'coffee',<% } %><% if(filters.babel) { %>
'babel',<% } %><% if(filters.jade) { %>
'jade',<% } %><% if(filters.stylus) { %>
'stylus',<% } %><% if(filters.sass) { %>
'sass',<% } %><% if(filters.less) { %>
'less',<% } %>
],
test: [<% if(filters.coffee) { %>
'coffee',<% } %><% if(filters.jade) { %>
'coffee',<% } %><% if(filters.babel) { %>
'babel',<% } %><% if(filters.jade) { %>
'jade',<% } %><% if(filters.stylus) { %>
'stylus',<% } %><% if(filters.sass) { %>
'sass',<% } %><% if(filters.less) { %>
Expand All @@ -465,7 +478,8 @@ module.exports = function (grunt) {
}
},
dist: [<% if(filters.coffee) { %>
'coffee',<% } %><% if(filters.jade) { %>
'coffee',<% } %><% if(filters.babel) { %>
'babel',<% } %><% if(filters.jade) { %>
'jade',<% } %><% if(filters.stylus) { %>
'stylus',<% } %><% if(filters.sass) { %>
'sass',<% } %><% if(filters.less) { %>
Expand Down Expand Up @@ -551,6 +565,24 @@ module.exports = function (grunt) {
ext: '.js'
}]
}
},<% } %><% if(filters.babel) { %>

// Compiles ES6 to JavaScript using Babel
babel: {
options: {
sourceMap: true
},
server: {
files: [{
expand: true,
cwd: 'client',
src: [
'{app,components}/**/*.js',
'!{app,components}/**/*.spec.js'
],
dest: '.tmp'
}]
}
},<% } %><% if(filters.stylus) { %>

// Compiles Stylus to CSS
Expand Down Expand Up @@ -620,10 +652,16 @@ module.exports = function (grunt) {
},
files: {
'<%%= yeoman.client %>/index.html': [
['{.tmp,<%%= yeoman.client %>}/{app,components}/**/*.js',
'!{.tmp,<%%= yeoman.client %>}/app/app.js',
'!{.tmp,<%%= yeoman.client %>}/{app,components}/**/*.spec.js',
'!{.tmp,<%%= yeoman.client %>}/{app,components}/**/*.mock.js']
[
<% if(filters.babel) { %>
'.tmp/{app,components}/**/*.js',
<% } else { %>
'{.tmp,<%%= yeoman.client %>}/{app,components}/**/*.js',
<% } %>
'!{.tmp,<%%= yeoman.client %>}/app/app.js',
'!{.tmp,<%%= yeoman.client %>}/{app,components}/**/*.spec.js',
'!{.tmp,<%%= yeoman.client %>}/{app,components}/**/*.mock.js'
]
]
}
},<% if(filters.stylus) { %>
Expand Down
3 changes: 2 additions & 1 deletion app/templates/_.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public
.idea
client/bower_components
dist
/server/config/local.env.js
/server/config/local.env.js
npm-debug.log
3 changes: 2 additions & 1 deletion app/templates/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"grunt-contrib-watch": "~0.6.1",<% if(filters.coffee) { %>
"grunt-contrib-coffee": "^0.10.1",<% } %><% if(filters.jade) { %>
"grunt-contrib-jade": "^0.11.0",<% } %><% if(filters.less) { %>
"grunt-contrib-less": "^0.11.0",<% } %>
"grunt-contrib-less": "^0.11.0",<% } %><% if(filters.babel) { %>
"grunt-babel": "~5.0.0",<% } %>
"grunt-google-cdn": "~0.4.0",
"grunt-newer": "~0.7.0",
"grunt-ng-annotate": "^0.2.3",
Expand Down
4 changes: 4 additions & 0 deletions app/templates/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@
<script src="socket.io-client/socket.io.js"></script><% } %>
<!-- endbuild -->

<% if(filters.babel) { %>
<!-- build:js(.tmp) app/app.js -->

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do exactly? Why is it different depending on wether babel is used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heya - bit of a slow reply! Babel is precompiling all the /client js resources to /.tmp.

If we inject all the js resources from /.tmp and /client with the non-babel index.html annotation .... ..... then we get a double up of all ES5 and ES6 items causing angular to die with multiple definitions of modules or something along those lines.

<% } else { %>
<!-- build:js({.tmp,client}) app/app.js -->
<% } %>
<script src="app/app.js"></script>
<!-- injector:js -->
<!-- endinjector -->
Expand Down
16 changes: 8 additions & 8 deletions app/templates/server/api/thing/thing.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ exports.index = function(req, res) {<% if (!filters.mongoose) { %>
]);<% } %><% if (filters.mongoose) { %>
Thing.find(function (err, things) {
if(err) { return handleError(res, err); }
return res.json(200, things);
return res.status(200).json(things);
});<% } %>
};<% if (filters.mongoose) { %>

// Get a single thing
exports.show = function(req, res) {
Thing.findById(req.params.id, function (err, thing) {
if(err) { return handleError(res, err); }
if(!thing) { return res.send(404); }
if(!thing) { return res.status(404).send('Not Found'); }
return res.json(thing);
});
};
Expand All @@ -54,7 +54,7 @@ exports.show = function(req, res) {
exports.create = function(req, res) {
Thing.create(req.body, function(err, thing) {
if(err) { return handleError(res, err); }
return res.json(201, thing);
return res.status(201).json(thing);
});
};

Expand All @@ -63,11 +63,11 @@ exports.update = function(req, res) {
if(req.body._id) { delete req.body._id; }
Thing.findById(req.params.id, function (err, thing) {
if (err) { return handleError(res, err); }
if(!thing) { return res.send(404); }
if(!thing) { return res.status(404).send('Not Found'); }
var updated = _.merge(thing, req.body);
updated.save(function (err) {
if (err) { return handleError(res, err); }
return res.json(200, thing);
return res.status(200).json(thing);
});
});
};
Expand All @@ -76,14 +76,14 @@ exports.update = function(req, res) {
exports.destroy = function(req, res) {
Thing.findById(req.params.id, function (err, thing) {
if(err) { return handleError(res, err); }
if(!thing) { return res.send(404); }
if(!thing) { return res.status(404).send('Not Found'); }
thing.remove(function(err) {
if(err) { return handleError(res, err); }
return res.send(204);
return res.status(204).send('No Content');
});
});
};

function handleError(res, err) {
return res.send(500, err);
return res.status(500).send(err);
}<% } %>
18 changes: 9 additions & 9 deletions app/templates/server/api/user(auth)/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var config = require('../../config/environment');
var jwt = require('jsonwebtoken');

var validationError = function(res, err) {
return res.json(422, err);
return res.status(422).json(err);
};

/**
Expand All @@ -15,8 +15,8 @@ var validationError = function(res, err) {
*/
exports.index = function(req, res) {
User.find({}, '-salt -hashedPassword', function (err, users) {
if(err) return res.send(500, err);
res.json(200, users);
if(err) return res.status(500).send(err);
res.status(200).json(users);
});
};

Expand All @@ -42,7 +42,7 @@ exports.show = function (req, res, next) {

User.findById(userId, function (err, user) {
if (err) return next(err);
if (!user) return res.send(401);
if (!user) return res.status(401).send('Unauthorized');
res.json(user.profile);
});
};
Expand All @@ -53,8 +53,8 @@ exports.show = function (req, res, next) {
*/
exports.destroy = function(req, res) {
User.findByIdAndRemove(req.params.id, function(err, user) {
if(err) return res.send(500, err);
return res.send(204);
if(err) return res.status(500).send(err);
return res.status(204).send('No Content');
});
};

Expand All @@ -71,10 +71,10 @@ exports.changePassword = function(req, res, next) {
user.password = newPass;
user.save(function(err) {
if (err) return validationError(res, err);
res.send(200);
res.status(200).send('OK');
});
} else {
res.send(403);
res.status(403).send('Forbidden');
}
});
};
Expand All @@ -88,7 +88,7 @@ exports.me = function(req, res, next) {
_id: userId
}, '-salt -hashedPassword', function(err, user) { // don't ever give out the password or salt
if (err) return next(err);
if (!user) return res.json(401);
if (!user) return res.status(401).send('Unauthorized');
res.json(user);
});
};
Expand Down
10 changes: 7 additions & 3 deletions app/templates/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ var config = require('./config/environment');
<% if (filters.mongoose) { %>
// Connect to database
mongoose.connect(config.mongo.uri, config.mongo.options);

mongoose.connection.on('error', function(err) {
console.error('MongoDB connection error: ' + err);
process.exit(-1);
}
);
// Populate DB with sample data
if(config.seedDB) { require('./config/seed'); }

<% } %>// Setup server
var app = express();
var server = require('http').createServer(app);<% if (filters.socketio) { %>
var socketio = require('socket.io')(server, {
serveClient: (config.env === 'production') ? false : true,
serveClient: config.env !== 'production',
path: '/socket.io-client'
});
require('./config/socketio')(socketio);<% } %>
Expand All @@ -34,4 +38,4 @@ server.listen(config.port, config.ip, function () {
});

// Expose app
exports = module.exports = app;
exports = module.exports = app;
6 changes: 3 additions & 3 deletions app/templates/server/auth(auth)/auth.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function isAuthenticated() {
.use(function(req, res, next) {
User.findById(req.user._id, function (err, user) {
if (err) return next(err);
if (!user) return res.send(401);
if (!user) return res.status(401).send('Unauthorized');

req.user = user;
next();
Expand All @@ -48,7 +48,7 @@ function hasRole(roleRequired) {
next();
}
else {
res.send(403);
res.status(403).send('Forbidden');
}
});
}
Expand All @@ -64,7 +64,7 @@ function signToken(id) {
* Set token cookie directly for oAuth strategies
*/
function setTokenCookie(req, res) {
if (!req.user) return res.json(404, { message: 'Something went wrong, please try again.'});
if (!req.user) return res.status(404).json({ message: 'Something went wrong, please try again.'});
var token = signToken(req.user._id, req.user.role);
res.cookie('token', JSON.stringify(token));
res.redirect('/');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ exports.setup = function (User, config) {
facebook: profile._json
});
user.save(function(err) {
if (err) done(err);
return done(err, user);
if (err) return done(err);
done(err, user);
});
} else {
return done(err, user);
}
})
}
));
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ exports.setup = function (User, config) {
google: profile._json
});
user.save(function(err) {
if (err) done(err);
return done(err, user);
if (err) return done(err);
done(err, user);
});
} else {
return done(err, user);
Expand Down
4 changes: 2 additions & 2 deletions app/templates/server/auth(auth)/local/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ var router = express.Router();
router.post('/', function(req, res, next) {
passport.authenticate('local', function (err, user, info) {
var error = err || info;
if (error) return res.json(401, error);
if (!user) return res.json(404, {message: 'Something went wrong, please try again.'});
if (error) return res.status(401).json(error);
if (!user) return res.status(404).json({message: 'Something went wrong, please try again.'});

var token = auth.signToken(user._id, user.role);
res.json({token: token});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ exports.setup = function (User, config) {
});
user.save(function(err) {
if (err) return done(err);
return done(err, user);
done(err, user);
});
} else {
return done(err, user);
}
});
}
));
};
};
Loading