8
votes

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.

3
How about using a smaller AMD loader, like Almond? - github.com/jrburke/almondSimon Smith
@SimonSmith I'm aiming to remove AMD all together though from the compiled version. When compiled to a single file, why should I need to use AMD?Jeff Storey
I'm also curious about this, as I don't want AMD to be a necessary (just sufficient) - did you end up solving this?Aram Kocharyan
Right now I'm just manually ordering my dependencies and concatenating them (using the gradle plugin, but looking to switch to something like grunt or yeoman to get more sophisticated and integrate with require). It turns out that most of the dependency ordering doesn't matter in my case.Jeff Storey

3 Answers

3
votes

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.

-1
votes

https://github.com/kodmax/grunt-stonejs is the answer. Compiles requirejs based project to a JS Stone. Basicly zero overhead.

-1
votes

I had the same problem that you.

Finally I solved it, and I have created a basic structure of requireJS project and a Gruntfile that perform the automatization.

  1. Process LESS
  2. Optimize requireJS.
  3. Remove AMD
  4. Replace the scripts tags in the HTML from the require syntax to the script tag sintax.

I hope this helps you.

https://github.com/sheldorn/require-base-structure