Skip to content

Instantly share code, notes, and snippets.

@mkuklis
Created February 1, 2013 03:43
Show Gist options
  • Save mkuklis/4689023 to your computer and use it in GitHub Desktop.
Save mkuklis/4689023 to your computer and use it in GitHub Desktop.
requirejs + cordova
require.config({
dir: "../../release/js/",
optimize: "uglify",
paths: {
jquery: 'vendor/jquery/jquery-1.9.0',
underscore: 'vendor/underscore',
backbone: 'vendor/backbone/backbone',
text: 'vendor/require/plugins/text',
cordova: 'vendor/android/cordova-2.3.0'
},
shim: {
underscore: {
exports: '_'
},
backbone: {
deps: ["underscore", "jquery"],
exports: "Backbone"
},
cordova: {
exports: 'cordova'
},
app: {
deps: ['jquery', 'underscore', 'backbone', 'cordova']
}
}
});
require(['app'], function (App) {
document.addEventListener('deviceready', function () {
App.initialize();
}, false);
});
@scttnlsn
Copy link

scttnlsn commented Feb 1, 2013

My setup is very similar. I'm getting this when I try to build with Grunt/r.js:

Tracing dependencies for: main
Error: ENOENT, no such file or directory './app/vendor/cordova/channel.js'
In module tree:
    main
      cordova

    at Object.fs.openSync (fs.js:338:18)

Which I believe is happening because cordova.js has an internal module system that's calling require('cordova/channel'). Not really sure what to do except load cordova.js separately (outside of RequireJS).

@mkuklis
Copy link
Author

mkuklis commented Feb 1, 2013

Oh I see that's pretty sad :(. It's crazy that they define their own define/require. Their code is pretty messy too...

I just ran this:

:%s/define(/cordova_define(/g
:%s/require/cordova_require/g

in vim to replace the names. that seems to make r.js happy but not sure if I like it...
I think other than that you are right the only option is to load it outside of require :(

@scttnlsn
Copy link

scttnlsn commented Feb 1, 2013

Gah! That's no fun. This might be a little better: requirejs/r.js#170 (comment)
Thanks again for taking time to help with this.

@mkuklis
Copy link
Author

mkuklis commented Feb 1, 2013

oh didn't realize these hooks exist. This doesn't look too bad. Does it work for you?

@scttnlsn
Copy link

scttnlsn commented Feb 1, 2013

Yeah, me either but they're working well. Seems like a decent compromise.

On a side note...really wish there were notifications for Gists.

@mkuklis
Copy link
Author

mkuklis commented Feb 2, 2013

Ha I was just thinking the same (regarding gists :)). I will give it a try with the hooks as well. I'm glad you found them.

@ufologist
Copy link

Tracing dependencies for: main
Error: ENOENT, no such file or directory './app/vendor/cordova/channel.js'
In module tree:
main
cordova

the above problem solution

require.config({
    shim: {
        'cordova-android': {
            exports: 'cordova'
        }
    },
    paths: {
        // don't map cordova.js with path 'cordova',
        // because it conflicts with internal module named 'cordova' in cordova.js.
        // if you map path 'cordova' with cordova-2.6.js, when it exec require('cordova/channel'),
        // will load script from cordova-2.6/channel.js,
        // because 'cordova' = 'cordova-2.6' with the path setting.
        // http://stackoverflow.com/questions/15440529/cordova-2-4-0-or-2-5-0-or-2-6-0-and-requirejs
        'cordova-android': '../lib/cordova-2.6.0'
    }
});

@zmoxga
Copy link

zmoxga commented Jun 20, 2014

do you know how to do with phonegap 3.5(not cordova 3.5 ) and rquireJs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment