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

Unexpected token return with r.js optimize and underscore #17

Closed
wants to merge 1 commit into from

Conversation

idflood
Copy link

@idflood idflood commented Apr 30, 2012

Hi,

I'had issues with optimization of a relatively large project so I created a small setup replacating the whole thing. It helped fix an issue in r.js (still not commited) but then found a problem with the use! plugin.

Here is the r.js issue and change needed to make the optimization process not crash: requirejs/r.js#157 (comment)
If this one line change is done to r.js then there is an error thrown in chrome:

Uncaught SyntaxError: Unexpected token return

which comes from this line

define('use!Underscore', [],return typeof _ !== "undefined" ? _ : void 0);

Applying both the r.js "patch" and this commit makes the setup works in all situations. Does this proposed change looks right?

the test setup: https://github.com/idflood/next-boilerplate.js

@tbranyen
Copy link
Owner

This doesn't make sense, how are you getting that output when the function that renders looks like:

write([
  "define('", pluginName, "!", moduleName, "', ",
    "[", normalize.deps, "],",

    "function() {",
      normalize.attach,
    "}",

  ");\n"
].join(""));

You're missing the function wrapper somehow... is that bit missing from your source?

@idflood
Copy link
Author

idflood commented Apr 30, 2012

In fact I don't really understand why this happens. The file beeing optimized is https://github.com/idflood/next-boilerplate.js/blob/master/src/scripts/boot.js

So in both use! and cs! plugins are used in the application. The r.js optimization is run from there: https://github.com/idflood/next-boilerplate.js/blob/master/src/scripts/_build.coffee#L72

And here is the main require.js config file: https://github.com/idflood/next-boilerplate.js/blob/master/src/scripts/require-config.js

With the proposed change the previously buggy line become this:

define('use!Underscore', [],_);

Do you see any error on my part? How can I debug this further?

@idflood
Copy link
Author

idflood commented May 1, 2012

You are right, I tried to optimize the file again but without modifying the use.js plugin this time. I then manually added the missing function(){ ... } and it is working with this. It was missing for underscore and backbone in this example. To compare, here is the ouptut line with the pull request patch applied:

define('use!Underscore', [],_);

So is it a problem with the r.js function incorrectly stripping the function declaration?

@tbranyen
Copy link
Owner

tbranyen commented May 2, 2012

Try giving the latest build a shot and see if this is still an issue.

@tbranyen tbranyen closed this May 2, 2012
@idflood
Copy link
Author

idflood commented May 2, 2012

Oh thanks, it's working perfectly now : )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants