gruntjs / grunt-contrib-requirejs

Optimize RequireJS projects using r.js.
http://gruntjs.com/
MIT License
504 stars 105 forks source link

Cannot call method 'substring' of undefined in r.js #81

Closed kingnight closed 10 years ago

kingnight commented 10 years ago

{ [Error: TypeError: Cannot call method 'substring' of undefined at modLine (/Users/aisino/Documents/yjyImsCenter/www/node_modules/grunt-contrib-requirejs/node_modules/requirejs/bin/r.js:23950:48) ] originalError: [TypeError: Cannot call method 'substring' of undefined] }

node version 0.10.29 install by grunt 0.4.4

jamesplease commented 10 years ago

@kingnight can you give more information? Have you been able to isolate the problem at all?

kingnight commented 10 years ago

1) I find if at the beginning of the javascript ,add some comments, after r.js optimizer , the result js file will partly not auto id in define

2) https://groups.google.com/forum/#!topic/requirejs/klf1OMS3zA8

Actually the problem is in r.js optimizer. In my case, one of the files was saved using old Mac OS line endings (\r). And other files were saved using Windows line endings (\r\n).

r.js doesn't detect correct line endings for such case, and following line simply doesn't split the file by lines: contentLines = contents.split('\n');

So I've changed this line in the file r.js to following: contentLines = contents.split(/(\r\n|\r|\n)/g);

And after this everything worked fine for me. Or you can simply check and fix all your .js files for correct line endings - \n or \r\n

jamesplease commented 10 years ago

Cool @kingnight. Glad you figured it out! I'm going to close this for now. You might consider raising an issue over on r.js about this if you think the behavior of the optimizer could be improved.