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

Getting undefined when creating a global function variable in a file loaded via require.js #69

Closed
thbaja opened this issue May 16, 2013 · 7 comments
Labels

Comments

@thbaja
Copy link

thbaja commented May 16, 2013

I'm using the sockjs-client module in a backbone+require.js setup.
(Sockjs-client on GH: https://github.com/sockjs/sockjs-client)

Source file: http://cdn.sockjs.org/sockjs-0.3.4.js

This part on line 30 seemed to be the problem:

SockJS = (function(){
              var _document = document;
              var _window = window;
              var utils = {};

And avoiding being global by adding a 'var' in front helped:

var SockJS = (function(){
@papandreou
Copy link
Member

Hmm, I'm not sure I fully understand the setup. Are you loading http://cdn.sockjs.org/sockjs-0.3.4.js directly from the CDN in a require statement?

And what goes wrong? Could you quote the relevant output of buildProduction and describe the problem with it?

@thbaja
Copy link
Author

thbaja commented May 20, 2013

I'm loading the sockjs module from a static file via reuirejs paths.
The link to the cdn, was just to let you see the source.

When building, I get no errors, but when running the code in a browser I get:
Uncaught ReferenceError: SockJS is not defined

It all works running in the browser before using the builder.

@papandreou
Copy link
Member

I couldn't reproduce the issue. Please provide more details:

  1. Which version of assetgraph-builder are you using? A lot of require.js-related changes landed in the latest 1.8.0 betas.
  2. What does your directory structure (including file names) and require.js config look like? If you didn't name the file containing SocketJS exactly socket.js and put it in the root, you might be running into the bug described here: Remove explicit AMD name for greater portability (issue #53). sockjs/sockjs-client#107

@thbaja
Copy link
Author

thbaja commented May 21, 2013

  1. I'm using 1.8.0beta12
  2. Selection of the directory structure:

app/main.js
assets/
vendor/
index.html

This is in the main.js file, that is loaded via data-main when loading requirejs (version 2.1.6):

require.config({
    baseUrl: 'app',
    enforceDefine: false,

    paths: {
        jquery: '../vendor/jquery/jquery-1.8.3.min',
        underscore: '../vendor/lodash/lodash.custom.min',
        backbone: '../vendor/backbone/backbone-min',
        sockjs: '../vendor/sockjs/sockjs-0.3.4',
        text: '../vendor/text/text'
    },
    shim: {
        backbone: {
            // Dependencies to be loaded before backbone.js
            deps: ['underscore', 'jquery'],
            // Once loaded, use the global 'Backbone' as the module value
            exports: 'Backbone'
        }
    }
});

@papandreou
Copy link
Member

Unfortunately I still cannot get it to fail in a reduced version where only SockJS is loaded: https://github.com/One-com/assetgraph-builder/tree/master/test/buildProduction/issue69

Perhaps it only happens when backbone and the other vendor scripts are added to the mix. Could you share a little more of your code? Maybe the main require statement is sufficient.

@Munter
Copy link
Member

Munter commented Jan 22, 2014

This issue is still relevant. I see error messages that are similar in some of the TodoMVC apps.

I have rebased the issue69 branch onto master and will see if I can figure out the root cause.

Since I see the same issues in non-AMD projects I suspect the minification process to be the culprit

@papandreou
Copy link
Member

Hopefully this will be fixed with 5.0.0 now that we delegate to r.js for bundling.

Please reopen if that's not the case.

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

No branches or pull requests

3 participants