Skip to content

Instantly share code, notes, and snippets.

@is-already-taken
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save is-already-taken/11346778 to your computer and use it in GitHub Desktop.
Save is-already-taken/11346778 to your computer and use it in GitHub Desktop.
main.js with requirejs.config() and NodeJS module export using requirejs Stub and passing the config with assignment
--- r.js 2014-04-27 09:10:55.394219131 +0200
+++ r.js 2014-04-27 16:32:41.574726697 +0200
@@ -23071,9 +23072,16 @@
jsConfig = stringData.value;
foundRange = stringData.range;
return false;
+ } else if (arg && arg.type === "AssignmentExpression") {
+ stringData = parse.nodeToString(fileContents, arg.right);
+ jsConfig = stringData.value;
+ foundRange = stringData.range;
+ return false;
+
}
} else {
arg = parse.getRequireObjectLiteral(node);
+
if (arg) {
stringData = parse.nodeToString(fileContents, arg);
jsConfig = stringData.value;
@@ -23245,6 +23253,9 @@
// require/requirejs.config({}) call
callName = c.object.name + 'Config';
}
+ } else if (node && node.type === 'VariableDeclarator' &&
+ node.id && node.id.name === '_RequireJsConfig') {
+ callName = 'requirejsConfigVariable';
}
return callName;
if (typeof requirejs === "undefined") {
// Non-browser context: create stub of requirejs API to prevent runtime error
var requirejs = { IS_STUB: true };
requirejs.config = function(){ /* STUB*/ };
}
var _RequireJSConfig;
// Depdending on the context, call the real or stub's config() and
// assign config to local variable
requirejs.config(_RequireJSConfig = {
paths: {
// ...
},
shim: {
// ...
}
});
if (typeof exports !== "undefined") {
// NodeJS context: export assigned config
module.exports.requireJsConfig = _RequireJSConfig;
}
if (!requirejs.IS_STUB) {
// Browser context (not subbed): "require" is RequireJS related, not NodeJS
require(["jquery", "backbone", "router"], function($, Backbone, Router){
// Code here
});
}
@is-already-taken
Copy link
Author

Requires a patch to r.js making the findConfig() method invoked when using mainConfigFile aware of inline assignments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment