Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

show meaningful less errors #244

Merged
merged 9 commits into from

4 participants

@vicapow

show this...

error: Less compilation error
FileError: '../flatstrap/less/bootstrap.less' wasn't found.

1 @import "../flatstrap/less/bootstrap.less";
2 @import "../flatstrap/less/responsive.less";

instead of this...

verbose: Server stopped.

/example/node_modules/sails/node_modules/less/lib/less/parser.js:390
                        else                                  throw new(LessEr
                                                                    ^
[object Object]
@vicapow

still issues. closing for now.

@vicapow vicapow closed this
@mikermcneil
Owner

@vicapow excited to see your solution!

vicapow added some commits
@vicapow vicapow properly set the less path so less @import statements to files outsid…
…e of the

asset directory work properly. more properly log less error messages
8ff53c1
@vicapow vicapow Merge branch 'master' of github.com:vicapow/sails
Conflicts:
	lib/assets.js
0ce89b2
@vicapow vicapow reopened this
@vicapow

there we go! now it should at least report the error. still not able to get the line number info since all the less files in the assets/styles folder are clobbered into a single string that is passed to less.

this commit also fixes issues with using the @import statement in less to files outside of the assets folders

@mikermcneil
Owner

@vicapow Thanks!

I think the reason the files are concatenated is to allow mixins/variables to work-- has anyone seen a way to get original line numbers/files in this sort of a setup? @techpines any ideas?

@techpines

@vicapow I would love to be able to fix this upstream in asset-rack. Is this what's needed to make the less errors come out correctly?

if (error) {
   if(!(error instanceof Error)){
        less.writeError(error)
        error = new Error("Less asset compilation error");
    }
    return self.emit('error', error);
}

Technically, the way it is currently set up, I think it is outputting the error "[object Object]" just not in a nonusable form.

@vicapow

@techpines yeah, that would be a better solution. in addition, sails will need to use the LessAsset instead of compiling the less code directly. seems like this code might have been written before asset-rack had support for less.

@techpines

@vicapow I did the less one by hand to match how sails was doing the less stuff before. But I don't really like how it concatenates the less files first. I think it does this to handle dependencies, but less supports dependency resolution out of the box with import.

If you got rid of the concatenation step, that would fit more in line with the asset-rack Less asset. Then you wouldn't need the extra custom stuff, plus I think it's just cleaner.

@vicapow

i agree. ok. i'm working on a patch right now for asset-rack to produce better less errors then I'll swing back into sails and see if I can remove the concatenation step. sounds good?

@techpines

Yea, sounds awesome!

@vicapow

ok. so. less compilation is now delegated to asset-rack but this will have to wait until asset-track's version gets bumped and fixes a bug with mimetypes.

once that all happens, sails will get nice less error messages like the following:

Error: Less error: missing closing `}`
    filename: /sails-example/assets/styles/styles.less
    line 7 column 0
    ...
     7: }
    ...
@mikermcneil
Owner

This is awesome-- saw you just committed- should I wait to pull until Asset Rack is updated? I can change the package.json to point to the master branch of Asset Rack, but I'd feel more confident waiting until there was a stable patch to depend on

@techpines

@mikermcneil The asset-rack master branch isn't totally clean, don't point sails at it.

I'm going to push something stable to npm tonight, and then I'll come back here with the version number.

@mikermcneil
Owner

Cool, thanks. Bros4lyfe

@techpines

New version, enjoy!

asset-rack@2.1.4

@vicapow should update the merge request :)

@vicapow

should be good to go!

@mikermcneil mikermcneil merged commit 744eea5 into balderdashy:master
@mikermcneil
Owner

Thanks fellas!

@mikermcneil
Owner

Guys, this is breaking my existing projects using LESS. Seems like it breaks the asset loading order (/assets/mixins don't seem to be brought in before /assets/styles)

@mikermcneil
Owner

Or it's compiling them independently, so variables in mixins that are included in a previous folder in the asset sequence aren't being made available in the other files

@techpines

I think that's what's happening. It's a breaking change.

@mikermcneil
Owner

Hm- so how do we do mixins? We'll need some way to include lib/mixin LESS files before the rest to have the variables recognized.

@techpines

We could do one of two things:

You can require the mixins:

@import "../../mixins.less";

That is probably how the less folks would recommend doing dependencies.

Or LessAsset could be modified to take some extra parameter that it uses for mixins. Right now the Less Asset looks like this:

new LessAsset({
    url: '/cool-stuff.css'
    filename: './somefile.less'
});

Maybe we could add an option to prepend a less file like this:

new LessAsset({
    url: '/cool-stuff.css',
    filename: './somefile.less',
    prepend: '../mixins.less'
});
@mikermcneil
Owner

Tried out imports, and it worked for me-- sorry I'm just a LESS n00b, didn't know the best practice there. For anyone else who's curious: http://lesscss.org/#-importing

That said, eventually I think it would still be nice for our LESS modules to understand the variables from previously included LESS files

@rachaelshaw
Owner

Thought using imports would fix the problem, but now I'm just getting this:

( to see your app, visit: http://localhost:1337 )  

/Users/rachaelshaw/Desktop/kitten/sails-website/node_modules/sails/node_modules/asset-     
rack/node_modules/less/lib/less/parser.js:421
                    throw new(LessError)(e, env);
                          ^
[object Object]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 38 additions and 55 deletions.
  1. +37 −53 lib/assets.js
  2. +1 −2  package.json
View
90 lib/assets.js
@@ -14,7 +14,6 @@ var fs = require('fs');
var cleancss = require('clean-css');
var async = require('async');
var uglify = require('uglify-js');
-var less = require('less');
var isProduction = sails.config.environment === 'production';
var JavascriptAsset = rack.Asset.extend({
@@ -34,25 +33,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,45 +77,27 @@ 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, '/')
+ .replace('.less', '.css');
+ 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();
- parser.parse(self.lessContents, function(error, tree) {
- if (error) return self.emit('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');
- });
- });
+ self.isDev = false;
+ self.emit('created');
});
}
});
@@ -174,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 = [];
+ var paths = [];
_.each(dirnames, function(dirname) {
- abspath = pathutil.join(sails.config.appPath, dirname);
- filenames = wrench.readdirSyncRecursive(abspath);
+ var abspath = pathutil.join(sails.config.appPath, dirname);
+ var 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;
View
3  package.json
@@ -71,7 +71,6 @@
"connect": "1.8.3",
"async": "0.1.22",
"underscore": "1.4.2",
- "less": "1.3.0",
"optimist": "0.3.4",
"underscore.string": "2.3.0",
"glob": "3.1.14",
@@ -80,7 +79,7 @@
"i18n": "0.3.5",
"wrench": "1.4.4",
"uglify-js": "2.2.4",
- "asset-rack": "2.0.6",
+ "asset-rack": "2.1.4",
"node-uuid": "~1.4.0",
"parley": "0.0.2",
"include-all": "0.0.5",
Something went wrong with that request. Please try again.