Skip to content
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

Adding Snockets compilation of js, cs and ts files #375

Closed
wants to merge 1 commit into from
Closed
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
63 changes: 27 additions & 36 deletions lib/assets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,38 @@ var JavascriptAsset = rack.Asset.extend({
self.paths = search(sails.config.assets.sequence, /(\.js|\.coffee|\.ts)$/);
self.assets = [];
async.forEachSeries(self.paths, function(path, next) {
var assetContent = fs.readFileSync(path, 'utf8');
var finalContent = '';
var assetUrl;
if (path.indexOf('.coffee') !== -1) {
assetUrl = '/' + pathutil.relative(sails.config.appPath, path).replace('.coffee', '.js').replace(/\\/g, '/');
var coffee = require('coffee-script');
try {
finalContent = coffee.compile(assetContent);
} catch (e) {
sails.log.error('CoffeeScript compilation failed:');
// Log CoffeeScript compilation error
sails.log.error(e);
// Include filepath from project root
sails.log.error('In file: ' + path.replace(sails.config.appPath, ''));
// Include line number of error
sails.log.error('On line: ' + (e.location.first_line - 1));
// Include problematic line and indicate offending character
sails.log.error( _.str.lines(assetContent)[e.location.first_line]);
sails.log.error( '\x1B[31m' + _.str.pad('^', e.location.first_column + 1) + '\x1B[39m');
}
} else if (path.indexOf('.ts') !== -1) {
assetUrl = '/' + pathutil.relative(sails.config.appPath, path).replace('.ts', '.js').replace(/\\/g, '/');
var ext = pathutil.extname(path);
var extensionHandlers = [];

var assetUrl = '/' + pathutil.relative(sails.config.appPath, path)
.replace(ext, '.js')
.replace(/\\/g, '/');

if (path.indexOf('.ts') !== -1) {
var tsc = require('node-typescript');
try {
finalContent = tsc.compile(path, assetContent);
} catch (e) {
sails.log.error('TypeScript compilation failed:');
sails.log.error(e);
}
} else {
assetUrl = '/' + pathutil.relative(sails.config.appPath, path).replace(/\\/g, '/');
finalContent = assetContent;
extensionHandlers.push [{ ext: 'ts', handler: tsc.compile }]
}
var asset = new rack.Asset({
mimetype: 'text/javascript',
url: assetUrl,
contents: finalContent
});

var asset = new rack.SnocketsAsset({
mimetype: 'text/javascript',
filename: path,
url: assetUrl,
compress: false
});

asset.isDev = true;
self.assets.push(asset);
asset.on('complete', next);
asset.on('error', function(e) {
if(ext == ".coffee") {
sails.log.error('CoffeeScript compilation failed:');
}
else if(ext == ".ts") {
sails.log.error('TypeScript compilation failed:');
}
sails.log.error(e);
});

}, function(error) {
if (error) self.emit('error', error);
self.contents = '';
Expand Down