less.js icon indicating copy to clipboard operation
less.js copied to clipboard

Cannot read property 'contents' of undefined

Open krmarien opened this issue 12 years ago • 5 comments

We just updated to the latest version of less and with every file we try to compile we get the error:

TypeError: Cannot read property 'contents' of undefined
    at Object.Parser.parse (/usr/local/lib/node_modules/less/lib/less/parser/parser.js:108:20)

Is this a bug in less or in the asset manager we are using (kriswallsmith/assetic)?

Thank you

krmarien avatar Nov 09 '14 15:11 krmarien

You upgraded to v2? This has breaking changes in it. I expect whstever is using less is doing so using the old method. There is an upgrade guide on our website.. please come back if you think this is an issue and not just lack of support for v2.

lukeapage avatar Nov 09 '14 16:11 lukeapage

Our asset manager was nog compatible with v2, thanks

krmarien avatar Nov 09 '14 20:11 krmarien

Ran into this problem with a symfony project using assetic. Ended up downgrading the package to less 1.7.5 to resolve the issue.

jakraska avatar Nov 24 '14 18:11 jakraska

Here is the pull request kriswallsmith/assetic#673.

jm42 avatar Nov 25 '14 17:11 jm42

If you guys want example on how to update to v2, we have just updated Web Essentials's less service with v2's API.

New version vs Old version, here is the diff:

(you can ignore the post-css stuff: autoprefixer and rtlscss)

diff --git a/EditorExtensions/Resources/server/services/srv-less.js b/EditorExtensions/Resources/server/services/srv-less.js
index 8a1a269..a12bb11 100644
--- a/EditorExtensions/Resources/server/services/srv-less.js
+++ b/EditorExtensions/Resources/server/services/srv-less.js
@@ -5,8 +5,8 @@ var less = require("less"),
 //#endregion

 //#region Handler
-var handleLess = function (writer, params) {
-    fs.readFile(params.sourceFileName, 'utf8', function (err, data) {
+var handleLess = function(writer, params) {
+    fs.readFile(params.sourceFileName, 'utf8', function(err, data) {
         if (err) {
             writer.write(JSON.stringify({
                 Success: false,
@@ -24,69 +24,61 @@ var handleLess = function (writer, params) {
             return;
         }

-        try {
-            new (less.Parser)({ filename: params.sourceFileName, relativeUrls: true }).parse(data, function (parseErrors, tree) {
-                if (parseErrors) {
+        var css, map;
+        var mapFileName = params.targetFileName + ".map";
+        var sourceDir = path.dirname(params.sourceFileName);
+        var options = {
+            filename: params.sourceFileName,
+            relativeUrls: true,
+            paths: [sourceDir],
+            sourceMap: {
+                sourceMapFullFilename: mapFileName,
+                sourceMapURL: params.sourceMapURL !== undefined ? path.basename(mapFileName) : null,
+                sourceMapBasepath: sourceDir,
+                sourceMapOutputFilename: path.basename(params.targetFileName),
+                sourceMapRootpath: path.relative(path.dirname(params.targetFileName), sourceDir)
+            },
+            strictMath: params.strictMath !== undefined,
+        };
+
+        less.render(data, options, function(error, output) {
+            if (error) {
+                writer.write(JSON.stringify({
+                    Success: false,
+                    SourceFileName: params.sourceFileName,
+                    TargetFileName: params.targetFileName,
+                    MapFileName: params.mapFileName,
+                    Remarks: "LESS: Error parsing input file.",
+                    Details: error.message,
+                    Errors: [{
+                        Line: error.line,
+                        Column: error.column,
+                        Message: "LESS: " + error.message,
+                        FileName: error.filename
+                    }]
+                }));
+                writer.end();
+                return;
+            }
+
+            css = output.css;
+            map = JSON.parse(output.map);
+
+            if (params.autoprefixer !== undefined) {
+                var autoprefixedOutput = require("./srv-autoprefixer")
+                                        .processAutoprefixer(css, map, params.autoprefixerBrowsers,
+                                                             params.targetFileName, params.targetFileName);
+
+                if (!autoprefixedOutput.Success) {
                     writer.write(JSON.stringify({
                         Success: false,
                         SourceFileName: params.sourceFileName,
                         TargetFileName: params.targetFileName,
                         MapFileName: params.mapFileName,
-                        Remarks: "LESS: Error parsing input file.",
-                        Details: parseErrors.message,
+                        Remarks: "LESS: " + autoprefixedOutput.Remarks,
+                        Details: autoprefixedOutput.Remarks,
                         Errors: [{
-                            Line: parseErrors.line,
-                            Column: parseErrors.column,
-                            Message: "LESS: " + parseErrors.message,
-                            FileName: parseErrors.filename
-                        }]
-                    }));
-                    writer.end();
-                    return;
-                }
-
-                var css, map;
-                var mapFileName = params.targetFileName + ".map";
-                var mapDir = path.dirname(mapFileName);
-
-                try {
-                    css = tree.toCSS({
-                        paths: [path.dirname(params.sourceFileName)],
-                        sourceMap: mapFileName,
-                        sourceMapURL: params.sourceMapURL !== undefined ? path.basename(mapFileName) : null,
-                        sourceMapBasepath: mapDir,
-                        sourceMapOutputFilename: mapFileName,
-                        strictMath: params.strictMath !== undefined,
-                        writeSourceMap: function (output) {
-                            output = JSON.parse(output);
-                            output.file = path.basename(params.targetFileName);
-                            // There might be a configuration in toCSS which let us remove
-                            // the following fix to save a millisecond or such per compile.
-                            output.sources = output.sources.map(function (source) {
-                                var sourceDir = path.dirname(source);
-
-                                if (sourceDir !== '.' && mapDir !== sourceDir)
-                                    return path.relative(mapDir, source).replace(/\\/g, '/');
-
-                                return source;
-                            });
-
-                            map = output;
-                        }
-                    });
-
-                } catch (e) {
-                    writer.write(JSON.stringify({
-                        Success: false,
-                        SourceFileName: params.sourceFileName,
-                        TargetFileName: params.targetFileName,
-                        MapFileName: params.mapFileName,
-                        Remarks: "LESS: " + e.message,
-                        Details: e.message,
-                        Errors: [{
-                            Line: e.line,
-                            Column: e.column,
-                            Message: "LESS: " + e.message,
+                            Message: "LESS: " + autoprefixedOutput.Remarks,
                             FileName: params.sourceFileName
                         }]
                     }));
@@ -94,91 +86,51 @@ var handleLess = function (writer, params) {
                     return;
                 }

-                if (params.autoprefixer !== undefined) {
-                    var autoprefixedOutput = require("./srv-autoprefixer").processAutoprefixer(css, map, params.autoprefixerBrowsers, params.targetFileName, params.targetFileName);
+                css = autoprefixedOutput.css;
+                map = autoprefixedOutput.map;
+            }

-                    if (!autoprefixedOutput.Success) {
-                        writer.write(JSON.stringify({
-                            Success: false,
-                            SourceFileName: params.sourceFileName,
-                            TargetFileName: params.targetFileName,
-                            MapFileName: params.mapFileName,
-                            Remarks: "LESS: " + autoprefixedOutput.Remarks,
-                            Details: autoprefixedOutput.Remarks,
-                            Errors: [{
-                                Message: "LESS: " + autoprefixedOutput.Remarks,
-                                FileName: params.sourceFileName
-                            }]
-                        }));
-                        writer.end();
-                        return;
-                    }
+            if (params.rtlcss !== undefined) {
+                var rtlTargetWithoutExtension = params.targetFileName.substr(0, params.targetFileName.lastIndexOf("."));
+                var rtlTargetFileName = rtlTargetWithoutExtension + ".rtl.css";
+                var rtlMapFileName = rtlTargetFileName + ".map";
+                var rtlResult = require("./srv-rtlcss")
+                               .processRtlCSS(css, map, params.targetFileName, rtlTargetFileName);

-                    css = autoprefixedOutput.css;
-                    map = autoprefixedOutput.map;
-                }
-
-                if (params.rtlcss !== undefined) {
-                    var rtlTargetWithoutExtension = params.targetFileName.substr(0, params.targetFileName.lastIndexOf("."));
-                    var rtlTargetFileName = rtlTargetWithoutExtension + ".rtl.css";
-                    var rtlMapFileName = rtlTargetFileName + ".map";
-                    var rtlResult = require("./srv-rtlcss").processRtlCSS(css,
-                                                                          map,
-                                                                          params.targetFileName,
-                                                                          rtlTargetFileName);
-
-
-                    if (rtlResult.Success) {
-                        writer.write(JSON.stringify({
-                            Success: true,
-                            SourceFileName: params.sourceFileName,
-                            TargetFileName: params.targetFileName,
-                            MapFileName: params.mapFileName,
-                            RtlSourceFileName: params.targetFileName,
-                            RtlTargetFileName: rtlTargetFileName,
-                            RtlMapFileName: rtlMapFileName,
-                            Remarks: "Successful!",
-                            Content: css,
-                            Map: JSON.stringify(map),
-                            RtlContent: rtlResult.css,
-                            RtlMap: JSON.stringify(rtlResult.map)
-                        }));
-
-                        writer.end();
-                    } else {
-                        throw new Error("Error while processing RTLCSS");
-                    }
-                } else {
+                if (rtlResult.Success) {
                     writer.write(JSON.stringify({
                         Success: true,
                         SourceFileName: params.sourceFileName,
                         TargetFileName: params.targetFileName,
                         MapFileName: params.mapFileName,
+                        RtlSourceFileName: params.targetFileName,
+                        RtlTargetFileName: rtlTargetFileName,
+                        RtlMapFileName: rtlMapFileName,
                         Remarks: "Successful!",
                         Content: css,
-                        Map: JSON.stringify(map)
+                        Map: JSON.stringify(map),
+                        RtlContent: rtlResult.css,
+                        RtlMap: JSON.stringify(rtlResult.map)
                     }));
-                }

-                writer.end();
-            });
-        } catch (e) {
-            writer.write(JSON.stringify({
-                Success: false,
-                SourceFileName: params.sourceFileName,
-                TargetFileName: params.targetFileName,
-                MapFileName: params.mapFileName,
-                Remarks: "LESS: " + e.message,
-                Details: e.message,
-                Errors: [{
-                    Line: e.line,
-                    Column: e.column,
-                    Message: "LESS: " + e.message,
-                    FileName: params.sourceFileName
-                }]
-            }));
+                    writer.end();
+                } else {
+                    throw new Error("Error while processing RTLCSS");
+                }
+            } else {
+                writer.write(JSON.stringify({
+                    Success: true,
+                    SourceFileName: params.sourceFileName,
+                    TargetFileName: params.targetFileName,
+                    MapFileName: params.mapFileName,
+                    Remarks: "Successful!",
+                    Content: css,
+                    Map: JSON.stringify(map)
+                }));
+            }
+
             writer.end();
-        }
+        });
     });
 };
 //#endregion

am11 avatar Nov 26 '14 03:11 am11