Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

less assets now deligate to `Rack.LessAsset`

  • Loading branch information...
commit 08b603695447b0f0c28eb744666066eb9402e2d1 1 parent edf29c0
@vicapow authored
Showing with 33 additions and 67 deletions.
  1. +33 −67 lib/assets.js
View
100 lib/assets.js
@@ -34,25 +34,23 @@ var JavascriptAsset = rack.Asset.extend({
});
} else if (path.indexOf('.ts') != -1) {
var tsc = require('node-typescript');
-
- var jscode = null;
- try {
- jscode = tsc.compile(path, fs.readFileSync(path, 'utf8'));
- } catch(e) {
- jscode = '';
- }
-
+ var jscode = null;
+ try {
+ jscode = tsc.compile(path, fs.readFileSync(path, 'utf8'));
+ } catch(e) {
+ jscode = '';
+ }
asset = new rack.Asset({
mimetype: 'text/javascript',
url: '/' + pathutil.relative(sails.config.appPath, path).replace('.ts', '.js').replace(/\\/g, '/'),
contents: jscode
});
} else {
- asset = new rack.Asset({
- mimetype: 'text/javascript',
- url: '/' + pathutil.relative(sails.config.appPath, path).replace(/\\/g, '/'),
- contents: fs.readFileSync(path, 'utf8')
- });
+ asset = new rack.Asset({
+ mimetype: 'text/javascript',
+ url: '/' + pathutil.relative(sails.config.appPath, path).replace(/\\/g, '/'),
+ contents: fs.readFileSync(path, 'utf8')
+ });
}
asset.isDev = true;
self.assets.push(asset);
@@ -80,63 +78,26 @@ var CssAsset = rack.Asset.extend({
self.regex = /(\.css|\.less)$/;
self.paths = search(sails.config.assets.sequence, self.regex);
self.assets = [];
- self.lessContents = '';
async.forEachSeries(self.paths, function(path, next) {
- var asset;
- if(pathutil.extname(path) == '.less') {
- self.lessContents += fs.readFileSync(path);
- return next();
- } else {
- asset = new rack.Asset({
- url: '/' + pathutil.relative(sails.config.appPath, path).replace(/\\/g, '/'),
- contents: fs.readFileSync(path, 'utf8')
- });
- }
+ var asset,
+ url = '/' + pathutil
+ .relative(sails.config.appPath, path).replace(/\\/g, '/');
+ if(pathutil.extname(path) === '.less')
+ asset = new rack.LessAsset({
+ url : url,
+ filename : path
+ })
+ else asset = new rack.Asset({
+ url: url,
+ contents: fs.readFileSync(path, 'utf8')
+ });
asset.isDev = true;
self.assets.push(asset);
asset.on('complete', next);
}, function(error) {
if(error) self.emit('error', error);
- var parser = new less.Parser({
- paths : _.map(sails.config.assets.sequence, function(asset_path){
- return pathutil.join(sails.config.appPath, asset_path)
- })
- });
- try{
- parser.parse(self.lessContents, function(error, tree) {
- if (error) {
- if(!(error instanceof Error)){
- less.writeError(error)
- error = new Error("Less asset compilation error");
- }
- return self.emit('error', error);
- }
- var lessAsset = new rack.Asset({
- url: '/assets/styles/style.less.css',
- mimetype: 'text/css',
- contents: tree.toCSS()
- });
- lessAsset.isDev = true;
- self.assets.push(lessAsset);
- lessAsset.on('complete', function() {
- self.contents = '';
- if (isProduction) {
- _.each(self.assets, function(asset) {
- self.contents += asset.contents += '\n';
- });
- self.contents = cleancss.process(self.contents);
- }
- self.isDev = false;
- self.emit('created');
- });
- });
- }catch(error){
- if(!(error instanceof Error)){
- less.writeError(error);
- error = new Error("Less asset compilation error");
- }
- return self.emit('error', error);
- }
+ self.isDev = false;
+ self.emit('created');
});
}
});
@@ -192,15 +153,20 @@ var Rack = rack.Rack.extend({
}
});
+/**
+ * given a list directories with relative paths to `sails.config.appPath`
+ * and a regex, return all the files in the directories that match
+ * the given regex
+ */
var search = function(dirnames, regex) {
paths = [];
_.each(dirnames, function(dirname) {
abspath = pathutil.join(sails.config.appPath, dirname);
filenames = wrench.readdirSyncRecursive(abspath);
_.each(filenames, function(filename) {
- path = pathutil.join(abspath, filename);
- if(!fs.statSync(path).isFile() || !regex.test(path)) return;
- paths.push(path);
+ var filepath = pathutil.join(abspath, filename);
+ if(!fs.statSync(filepath).isFile() || !regex.test(filepath)) return;
+ paths.push(filepath);
});
});
return paths;
Please sign in to comment.
Something went wrong with that request. Please try again.