Require.js r.js: Compile directory to single file

244 views Asked by At

Is there any way to configure RequireJS to compile an entire directory to a single file? I don't mean the normal use case of the 'out' setting. I'll try to explain by example. If I have the following project structure:

- app
  - main.js
  - menu.js
- module
  - file-a.js
  - file-b.js

Then let's say I want to compile the 'app' directory to a single file. I don't care about it's dependencies - even if it requires 'module' or either of its files, they won't be included. Even if main.js doesn't require menu.js, it'll be included anyway. The resultant output file would define 'app/main' and 'app/menu' modules.

Likewise, if I wanted to compile the 'module' directory, the file would define 'module/file-a' and 'module/file-b' regardless of what other dependencies were defined.

I hope this is clear enough.

1

There are 1 answers

1
shriidhar On

You can use the dir parameter in build file of require instead of just name parameter.

You can read more about building whole directory on requirejs documentation - Optimize Whole Project

If you write build file something like app-build.js-

({
   appDir: ".",
   baseUrl: "app",
   dir: "../app-build",
})  

and if you run r.js -o app.build.js then it will create

  • app-build
    • main.js
    • menu.js

Here menu.js will not be include in main.js unless it is required somewhere in main.js source.