How to optimize a subset of JavaScript files created as RequireJS modules <p>I have single-page web application that uses RequireJS to organize and structure JavaScript code. <strong>Inside the app, I have some JS files that I want to optimize using r.js because they belong to an API that will be consumed by my customers. So, I just want to optimize those files and keep the rest of the code as it is now.</strong></p> <p>This is my application structure:</p> <ul> <li>app <ul> <li>main.js</li> <li>many other files and folders</li> </ul></li> <li>scripts <ul> <li>myAPI <ul> <li>app.js </li> <li>many other files and folders</li> </ul></li> <li>require.js </li> <li>jquery.js</li> <li>build.js</li> </ul></li> <li>index.htm</li> </ul> <p>All the JavaScript code is located under the <em>app</em> and <em>scripts</em> folders. As I mentioned before, all those files are defined as RequireJS modules. This is how the main.js looks now:</p> <pre><code>require.config({ baseUrl: 'scripts', paths: { base: 'myAPI/base', proxy: 'myAPI/proxyObject', localization: 'myAPI/localization', app: '../app', } }); require(['myAPI/app'], function (app) { //app.init.... } ); </code></pre> <p>As you can see, in the <em>paths</em> configuration I'm defining some aliases that point to <em>myAPI</em> (the folder I want to optimize).</p> <p>This is how I reference RequireJS from the index.htm file:</p> <pre><code>&lt;script data-main="app/main" src="scripts/require.js"&gt;&lt;/script&gt; </code></pre> <p>This is the build file I created for r.js (scripts/build.js):</p> <pre><code>({ baseUrl: '.', out: 'build/myAPI.js', name: 'myAPI/app', include: ['some modules'], excludeShallow: ['some modules defined in the app folder'], mainConfigFile: '../app/main.js', optimize: 'uglify2', optimizeCss: 'none' }) </code></pre> <p><strong>The optimized file is generated, but I have some challenges trying to use it</strong>:</p> <ol> <li>How do I reference that file from the app?</li> <li>The dependencies to <em>myAPI</em> modules are broken now. RequireJS doesn't find the modules defined in <em>myAPI</em>.</li> <li>What can I do to keep the aliases defined in require.config.paths working?</li> </ol> <p>Could you please help me with some suggestions or feedback for this situation?</p> <p>Thanks!!</p>