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.
({
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:
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.
/target
myself before running r.js?baseUrl=/overlay
is different from build.js residing inside /project
?dir
) live outside appDir
or baseUrl
?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
I would say
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With