I have a project with a structure that looks similar to this:
/path/to/project
/apps
/app1
/js
/modules
/bar.js
/etc..
/app1.js
/app1.build.js
/foo
/app2
/js
/modules
/cad.js
/etc..
/app2.js
/app2.build.js
/static
/js
/libraries
/jQuery
/etc...
/common-config.js
/common-build-config.js
common-config.js
defines a set of paths for loading libraries that is relative to the web-root of the project that both app1
and app2
can use. The goal of common-build-config.js
is to provide similar functionality for the build process. The problem I am encountering is that I cannot use fully-qualified paths in common-build-config.js
, as path/to/project
is dependent on a number of factors (environment, project branch, server, etc), and relative paths fail because the relative paths from app1
and app2
to common-build-config.js
are different. Module loading in app1
and app2
assumes that the base directory is the app directory, so I cannot change the baseUrl
parameter in the app[x].build.js
files without manually specifying paths for a ton of modules.
Is there a way I can specify baseUrl
in common-build-config.js
such that it will not affect the path loading in either of the main.build.js
files? Alternatively, is there a way to use variables when defining paths in the build config (like getting the value of Node's __dirName
variable)? Or is there another approach that can end-route this problem?
Example files:
[/path/to/project/static/common-config.js]
requirejs.config({
paths: {
jquery: '/static/js/libraries/jquery/jquery',
underscore: '/static/js/libraries/underscore/underscore',
backbone: '/static/js/libraries/backbone/backbone'
}
});
[/path/to/project/apps/app1/js/app1.js]
require(['/static/js/common-config.js'], function () {
require([
// Load common modules
'jquery',
'underscore',
'backbone',
// Load local modules
'bar'
], function ($, _, backbone, bar) {
// do stuff
});
});
[/path/to/project/apps/app1/js/app1.build.js]
({
name: 'app1',
out: './app1.min.js',
optimize: 'uglify',
mainConfigFile: '../../../../static/js/common-build-config.js',
findNestedDependencies: true
})
[/path/to/project/static/common-build-config.js]
It would be nice if I could do something like this without affecting the paths in app1.build.js:
requirejs.config({
baseUrl: './',
paths: {
jquery: 'libraries/jquery/jquery',
underscore: 'libraries/underscore/underscore',
backbone: 'libraries/backbone/backbone'
}
});
or something like this without getting an error like __dirName is not defined
:
requirejs.config({
paths: {
jquery: __dirName + 'libraries/jquery/jquery',
underscore: __dirName + 'libraries/underscore/underscore',
backbone: __dirName + 'libraries/backbone/backbone'
}
});
In case it's relevant, I build the min files from the project root with the commands:
node "static/js/libraries/require/r.js" -o "apps/app1/js/app1.build.js"
node "static/js/libraries/require/r.js" -o "apps/foo/app2/js/app2.build.js"
I did not find a solution to this in require.js, but I wrote a nAnt script that does a string replace on the config script before running the build, which end-routes the problem:
<target name="build.require.common">
<!-- copy the common build config file to a temp file -->
<copy file="static/js/common-build-config-template.js" tofile="static/js/common-build-config.js" overwrite="true">
<filterchain>
<!-- replace {root} with the project root directory (defined elsewhere) -->
<replacestring from="{root}" to="${projectRoot}" />
</filterchain>
</copy>
<!-- run node "static/js/r.js" -o "apps/app1/js/app1.build.js" -->
<exec program="${nodeExe}">
<arg line=""static/js/r.js" -o "apps/app1/js/app1.build.js""/>
</exec>
<!-- delete the temp config file -->
<delete file="static/js/common-build-config.js" />
</target>
with a common-build-config-template.js
file that looks like this:
requirejs.config({
paths: {
jquery: '{root}/libraries/jquery/jquery',
underscore: '{root}/libraries/underscore/underscore',
backbone: '{root}/libraries/backbone/backbone'
}
});