Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/template-jasmine-requirejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,33 @@ exports.process = function(grunt, task, context) {
var baseUrl = getBaseUrl();

/**
* Retrieves the module URL for a require call relative to the specified Base URL.
* Retrieves the module URL for a require call relative to the specified Base URL/module.
*/
function getRelativeModuleUrl(src) {
return path.relative(baseUrl, src).replace(/\.js$/, '');
function resolvePackageRoot(baseUrl, pkg) {
var root = baseUrl + path.sep;
if (pkg.location) root += pkg.location + path.sep;
root += pkg.main ? pkg.main : 'main';
return path.normalize(root);
}

// Remove baseUrl and .js from src files
context.scripts.src = grunt.util._.map(context.scripts.src, getRelativeModuleUrl);
// Find the root module, if any, in packages
var module = false;
var packages = context.options.requireConfig.packages || [];
packages.forEach(function (pkg) {
var root = resolvePackageRoot(baseUrl, pkg);
var rootPath = path.dirname(root);
if (rootPath === baseUrl) module = pkg;
});

var prefix = module ? (module.name + path.sep) : '';
var main = module ? resolvePackageRoot(baseUrl, module) : false;
context.scripts.src = grunt.util._.map(context.scripts.src, function(script) {
script = path.normalize(script);
if (script === main) script = module.name;
else script = script.replace(new RegExp('^' + baseUrl + path.sep + "?"), prefix);
if (script === prefix + 'main') script = module.name;
return script.replace(/\.js$/, '');
});

// Prepend loaderPlugins to the appropriate files
if (context.options.loaderPlugin) {
Expand Down