cloudchen / grunt-template-jasmine-requirejs

RequireJS template for grunt-contrib-jasmine task
111 stars 96 forks source link

Question on how to use this #1

Closed svperfecta closed 11 years ago

svperfecta commented 11 years ago

Hi there - I'm not totally clear on how to use this. I have a (rather complicated) require.config defined in my project; it sets up various external libraries for i18n, set ups a paths object, a shims object, etc. In order to use this task it looks like I'd need to duplicate my config in into my gruntfile as well. Is this correct?

Thanks very much for putting this together!

jsoverson commented 11 years ago

For using grunt-contrib-jasmine and this template in an existing project you may need to duplicate a lot of requirejs configuration because of the many different ways people set up requirejs.

Others have had luck putting their configuration in a require-config.js script that is loaded prior to the include for require.js in order to reduce the need for duplication, e.g.

var require = {
  paths : {
    // paths config
  },
  // etc
}

Then in your app

<script src="require-config.js"></script>
<script src="require.js" data-main="your/main"></script>

and in the grunt setup, you could put require-config.js in the vendor config (not a good name for that, but in this template vendor is just scripts that load before require.js)

svperfecta commented 11 years ago

Hey @jsoverson Thanks for the help, got it! (Nice work by the way, I really like the template options)

netpoetica commented 11 years ago

Making a global require object is not my cup of tea. Looks hacky. Tell me there's a more clever solution? Doing this is putting you on your way to just breaking any dependencies out of the require framework when you can't quite find a place for them. Next thing you know your web app will be just as unstable and unreadable as it was before you had any AMD.

It would be interesting if there was a server-side config, like a node module

module.exports = {
  paths: {
    jQuery: '/js/libs/jquery',
    Underscore: '/js/libs/underscore',
    Backbone: '/js/libs/backbone',
    models: 'models',
    templates: '../templates',

    MyApp: '/js/MyApp'
  },

  shim: {
    'Backbone': ['Underscore', 'jQuery']
  }
}

and somehow one could serve this variable into the JS file as it gets parsed server-side.

EDIT: to be fair though, the RequireJS docs seem to think yours is an okay methodology: http://requirejs.org/docs/api.html#config

netpoetica commented 11 years ago

What would go under templateOptions: { requireConfig: '...' } if you had a global require object?

jsoverson commented 11 years ago

What would go under templateOptions: { requireConfig: '...' } if you had a global require object?

Whatever you wanted to manage via the gruntfile instead of the global, shared config

Making a global require object is not my cup of tea. Looks hacky. Tell me there's a more clever solution? Doing this is putting you on your way to just breaking any dependencies out of the require framework when you can't quite find a place for them. Next thing you know your web app will be just as unstable and unreadable as it was before you had any AMD.

The better solution is to not need much in your requirejs config. This doesn't strike me as being a bad thing, though. The requirejs configuration is not a dependency, it is configuration and having it separate to be used by multiple applications seems more maintainable than tying it directly to the startup of one application and finding a way for every other application to play nicely with that.