Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Require.js optimizer supposed to copy all files over into the output directory?

Tags:

requirejs

r.js

I am trying to integrate the r.js optimizer on the server side (Apache Sling) and face one problem: when resolving modules it always looks them up under the output directory (dir), not from within the source directory (baseUrl or appDir), doesn't find them and thus fails.

/project/build.js

({
    name: "modules/main",
    dir: "/target",
    baseUrl: "/sources"
})

If you wonder, the root path / is inside the server's JCR repository, not a file system. Also I simplified the example a bit (hopefully without concealing the issue).

It will resolve and read the main file properly:

/sources/modules/main.js

require(["modules/foo"]);

However, when it now tries to resolve modules/foo, it tries to read it from /target/modules/foo.js instead of /sources/modules/foo.js as I would expect, which does not exist and the whole r.js execution fails and stops.

I tried using appDir and all kinds of combinations, but the issue is always the same. I am fairly sure it is not related to my integration code... AFAIU from documentation and googling around, it should either copy them to the target before building the optimized file or simply pick them up from the source directory automatically.

  • Am I supposed to copy all the raw source files to /target myself before running r.js?
  • Maybe the problem is that baseUrl=/overlay is different from build.js residing inside /project?
  • Maybe r.js also looks at the current working directory of the r.js process (which is so far undefined in my case)?
  • Can the output directory (dir) live outside appDir or baseUrl?
like image 801
Alexander Klimetschek Avatar asked Aug 08 '13 19:08

Alexander Klimetschek


2 Answers

My require.js configuration looks like so:

({
  appDir: "../app",
  baseUrl: "js/lib", // means the base URL is ../app/js/lib
  dir: "../app-built", //target

  // offtopic, but a very handy option
  mainConfigFile: "../app/config.js",

  // I'm not 100% sure if it's equivalent to your version
  // where you're not using "modules" and just "name"
  modules: [{
    name: "../some/main" // this is ../app/js/some/main.js
  }]
})

Reading through https://github.com/jrburke/r.js/blob/master/build/example.build.js#L15 - it seems you do want an appDir specified if you want the files to be copied to the target dir before optimization.

To answer your other questions

  • you don't need to manually copy files over
  • baseUrl should point to the same place as baseUrl used in your app's config - however you have to adjust it depending on what appDir you choose to use (e.g. appDir="../app" and baseUrl="js/lib", or appDir="../app/js" then baseUrl="lib", etc.)
  • appDir and dir should be relative to the build config file - I don't know what happens when you use absolute paths
  • yes - output dir does (has to?) live outside appDir. BaseURL is within the appDir/dir (all these names are really confusing..)

I would say

  1. use the "appDir" setting
  2. try using "modules" like I did instead of just "name"
  3. make "appDir" and "dir" relative paths to the build file if you can - these absolute paths might be what's breaking? because other than that the config looks very similar to the one I use

I know there's a different way of configuring it where your output is 1 file, which case the files are read from the source dir - but I haven't used that much myself.

Hope this helps.

like image 148
Karolis Avatar answered Sep 21 '22 13:09

Karolis


Answering myself: I got it to work with the single output file approach using out instead of appDir or dir:

({
    name: "modules/main",
    baseUrl: "/sources"
    out: "/target/out.js",
})

In this case it reads all the modules from the sources and creates a /target/out-temp.js which it then moves to /target/out.js when done.

This seems to suit my needs so far.

like image 37
Alexander Klimetschek Avatar answered Sep 19 '22 13:09

Alexander Klimetschek