title: Working project structure that uses grunt.js to combine JavaScript files using RequireJS?
body: <p>I have some projects that use <a href="http://requirejs.org/">RequireJS</a> to load individual JavaScript modules in the browser, but I haven't optimized them yet. In both development and production, the app makes a separate request for each JavaScript file, and now I would like to fix that using <a href="http://gruntjs.com/">Grunt</a>. </p>
  
  <p>I have tried to put together a simple project structure to no avail, so I'm wondering if someone can provide a working example for me. My goals are the following:</p>
  
  <ol>
  <li>In development mode, everything works in the browser by issuing a separate request for each required module. <strong>No grunt tasks or concatenation are required in development mode.</strong></li>
  <li>When I'm ready, I can run a grunt task to optimize (combine) all of the JavaScript files using <a href="http://requirejs.org/docs/optimization.html">r.js</a> and test that out locally. Once I'm convinced the optimized application runs correctly, I can deploy it.</li>
  </ol>
  
  <p>Here's a sample structure for the sake of this conversation:</p>
  
  <pre><code>grunt-requirejs-example/
    grunt.js
    main.js (application entry point)
    index.html (references main.js)
    lib/ (stuff that main.js depends on)
      a.js
      b.js
      requirejs/
        require.js
        text.js
    build/ (optimized app goes here)
    node_modules/ (necessary grunt tasks live here)
  </code></pre>
  
  <p>Specifically, I'm looking for a <strong>working</strong> project structure that I can start from. My main questions are:</p>
  
  <ol>
  <li>If this project structure is flawed, what do you recommend?</li>
  <li>What exactly needs to be in my <code>grunt.js</code> file, especially to get the r.js optimizer working?</li>
  <li>If all of this isn't worth the work and there's a way to use the grunt <code>watch</code> task to automatically build everything in development mode every time I save a file, then I'm all ears. I want to avoid anything that slows down the loop from making a change to seeing it in the browser.</li>
  </ol>