Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.2.0 modules #71

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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