Search code examples
javascriptrequirejsr.js

Requirejs module to use without require


I've already read this post:

Write Requirejs modules that work without requirejs

and also checked out almond. but I cannot get it to work.

My build file looks like this:

{
    "baseUrl" : "../",
    "name" : "build/almond",
    "include" : ["uploader"],
    "out" : "../uploader.min.js",
    "wrap" : {
        'startFile' : 'start.frag',
        'endFile' : 'end.frag'
    }
}

and my start.frag

(function (root, factory) {
  if (typeof define === 'function') {
    define(factory);
  } else if (typeof exports === 'object') {
    module.exports = factory();
  } else {
    root.Uploader = factory();
  }
}(this, function () {

and my end.frag

    return require('uploader');
}));

It works fine with requirejs.

require(['uploader'], function(Uploader) {
 var uploader = new Uploader();
});

but I want to be able to use it without requirejs so I can just use the script-tag

<script type="text/javascript" src="uploader.js"></script>
<script>
 var uploader = new Uploader();
</script>

how can I achieve this or do I have to rewrite my library?

I think jQuery does this already.

Any ideas?


Solution

  • Alright so the problem was I was trying to do new Uploader, when I had already loaded the require library.

    So I couldn't use both approaches.

    But it works fine when I don't have requirejs at all.

    I just wanted to let you know in case someone else has this problem in the future.