Jeff Storey
Jeff Storey

Reputation: 57162

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.

Upvotes: 8

Views: 1809

Answers (3)

Sheldorn
Sheldorn

Reputation: 1

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

Upvotes: -1

Marcin
Marcin

Reputation: 1

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

Upvotes: -1

Olical
Olical

Reputation: 41352

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.

Upvotes: 3

Related Questions