Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compile RequireJS to remove AMD dependency

I'm using RequireJS to manage my dependencies in development, but at production I would like to remove all dependencies on an AMD loader. It looks like the RequireJS optimizer creates a file that still uses an AMD load at runtime - I'm just looking to have a static (non-AMD dependent, but still AMD compatible) file, such as what jquery produces (from looking at jquery source, it appears they manually order their dependencies in their grunt file). Is this possible?

I'm open to using other libraries other than RequireJS as well.

Note: This is similar to my other question Javascript requirejs in development but compiled in production, but in this case I want to remove AMD all together.

like image 504
Jeff Storey Avatar asked Mar 27 '13 13:03

Jeff Storey


1 Answers

If you want to have your script loadable via a <script> tag or AMD then you might want to use something based on how my EventEmitter class exposes its self.

// Expose the class either via AMD, CommonJS or the global object
if (typeof define === 'function' && define.amd) {
    define(function () {
        return EventEmitter;
    });
}
else if (typeof module !== 'undefined' && module.exports){
    module.exports = EventEmitter;
}
else {
    this.EventEmitter = EventEmitter;
}

This exposes the object via AMD, CommonJS (node) and the global object (i.e. window). This has one main caveat, it is intended for single file scripts that just want to expose their class via AMD.

If you have a lot of modules then you may want to first compile the script with r.js, shim AMD with Almond and then use something like the above to expose it in multiple ways above.

like image 84
Olical Avatar answered Sep 30 '22 18:09

Olical