Created
March 29, 2012 20:12
-
-
Save jeffbski/2243210 to your computer and use it in GitHub Desktop.
RequireJS r.js optimizer suggestion/feature request - resolve relative package dependency names to match define module names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given package structure | |
- package.json | |
- src | |
- dist.build.js | |
- scripts | |
- store | |
- main.js | |
- util.js | |
*/ | |
// package.json | |
{ | |
"name": "myapp", | |
"main": "scripts/store/main" | |
} | |
// store/main.js | |
if (typeof define === 'undefined') { | |
var define = require('amdefine'); | |
} | |
define(['./util'], function (util) { | |
// use util | |
var store = { | |
// store properties/methods | |
}; | |
return store; | |
}); | |
// store/util.js | |
if (typeof define === 'undefined') { | |
var define = require('amdefine'); | |
} | |
define([], function () { | |
var util = { | |
// util properties/methods | |
}; | |
return util; | |
}); | |
// src/dist.build.js | |
({ | |
baseUrl: "../scripts", | |
optimize: "none", | |
packages: ["store"], | |
name: "myapp", | |
out: "dist/myapp.js" | |
}) | |
// running r.js optimizer produces dist/myapp.js | |
define('store/util', [], function () { | |
var util = { | |
// util properties/methods | |
}; | |
return util; | |
}); | |
define('store', ['./util'], function (util) { | |
// use util | |
var store = { | |
// store properties/methods | |
}; | |
return store; | |
}); | |
/* | |
So using amdefine, the above works fine without requirejs in node. | |
However the optimized combined code above will not run in almond | |
or even requirejs without informing requirejs that 'store' is a package. | |
So you have to not only deliver the dist file, but also a bunch of | |
setup for requirejs config so it will be able to find 'store/util' | |
and any other packages. ie. This won't run in almond at all, and needs | |
lots of setup to be able to use requirejs. | |
If instead, if r.js optimizer were to convert './util' to | |
'store/util' like below, then everything would work in almond | |
and in requirejs without needing any special config. | |
Is there any reason r.js shouldn't do this by default? | |
If so, could it be a configuration option to have it work this way? | |
I wanted to check before I went through the trouble of trying to | |
modify it, to see if I am missing something. If this change | |
were made or was configurable, then we could generate optimized | |
single files which contain everything that would resolve | |
in requirejs without all the extra config package setup, and | |
would also work out of the box with almond. | |
*/ | |
// desired r.js optimizer output, resolving relative paths to | |
// the right module name. Note that the './util' dependency | |
// was changed to 'store/util' to match the generated | |
// module name. | |
define('store/util', [], function () { | |
var util = { | |
// util properties/methods | |
}; | |
return util; | |
}); | |
define('store', ['store/util'], function (util) { | |
// use util | |
var store = { | |
// store properties/methods | |
}; | |
return store; | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment