Beispiel #1
0
var parseConfig = Q.async(function*() {
	var args = process.argv.slice(3);
	var opts = parseOpts(_opts, args, 0);

	var rjsconfig = {};
	var dotAmdConfigFile = findup('.amdconfig');

	if (!opts.config && !opts['base-url'] && !opts['entry-point'] && !dotAmdConfigFile) {
		log.verbose.writeln('No --config, --base-url, or --entry-point given and no .amdconfig file found.');
		log.verbose.writeln('Attempting to guess this repository\'s RequireJS config...');

		rjsconfig = yield _guessConfig();
	}
	else {
		var configs = [];

		if (dotAmdConfigFile) {
			opts['entry-point'] = path.dirname(dotAmdConfigFile);
			var dotAmdConfig = getConfigRecursive(dotAmdConfigFile);
			configs.push(dotAmdConfig);
		}

		if (opts.config) {
			if (!fs.existsSync(opts.config)) {
				throw new Error('--config file "' + path.relative(process.cwd(), opts.config) + '" was not found!');
			}
			var config = getConfigRecursive({
				mainConfigFile: opts.config
			});
			configs.push(config);
		}

		if (opts['base-url']) {
			configs.push({
				baseUrl: opts['base-url']
			});
		}

		rjsconfig = mixin.apply(this, configs);

		opts['entry-point'] = opts['entry-point'] || process.cwd();
		log.verbose.writeln('Entry point: ' + opts['entry-point']);

		rjsconfig.baseUrl = path.resolve(opts['entry-point'], rjsconfig.baseUrl.replace(/^\//, ''));

		if (!fs.existsSync(rjsconfig.baseUrl)) {
			log.error('RequireJS baseUrl "' + rjsconfig.baseUrl + '" does not resolve to a real path! This is required.');
		}
	}

	log.verbose.writeln('RequireJS configuration:');
	log.verbose.write(JSON.stringify(rjsconfig, false, 4) + '\n\n');

	return rjsconfig;
});
Beispiel #2
0
var augmentRequireWithAmd = function(rjsconfig) {
	rjsconfig = getConfigRecursive(rjsconfig);
	rjsconfig.baseUrl = rjsconfig.baseUrl || process.cwd();
	rjsconfig.paths = rjsconfig.paths || {};

	_paths = Object.keys(rjsconfig.paths).map(function(key) {
		return path.resolve(rjsconfig.baseUrl, rjsconfig.paths[key]);
	});

	var _resolveFilename = Module._resolveFilename;
	Module._resolveFilename = function(request, parent) {
		if (request === 'node-amd-require-amdefine') {
			// use our copy of amdefine
			return require.resolve('amdefine');
		}

		var dirname = path.dirname(parent.filename);
		var filename = resolve(rjsconfig, dirname, request);

		if (!filename) {
			filename = _resolveFilename.apply(this, arguments);
		}

		return filename;
	};

	var _extensionsJs = require.extensions['.js'];
	require.extensions['.js'] = function(localModule, filename) {
		if (!_shouldTransform(filename)) {
			return _extensionsJs.apply(this, arguments);
		}

		var contents = fs.readFileSync(filename, 'utf8');
		contents = 'if (typeof define !== "function") { var define = require("node-amd-require-amdefine")(module); require.nodeRequire = require; } ' + contents;
		localModule._compile(contents, filename);
	};

};
Beispiel #3
0
var _guessConfig = Q.async(function*() {
	var root = _projectRoot();
	var files = '';

	// find a file that looks like a RequireJS config file
	var isGit = findup('.gitignore');
	if (isGit) {
		// we can use .gitignore rules to exclude tmp and dependency directories
		files = yield exec('/bin/bash -c \'comm -23 <(grep -Rl "require.config(" . | sort) <(grep -Rl "require.config(" . | git check-ignore --stdin | sort)\'', {
			cwd: root,
			maxBuffer: 1000000
		});
	}
	else {
		files = yield exec('grep -Rl "^require.config(" . | grep -v "node_modules" | grep -v "bower_components"', {
			cwd: root,
			maxBuffer: 1000000
		});
	}

	if (!files.length) {
		return {};
	}

	// shortest file path is probably most likely to be the user's (rather than
	// something in node_modules or bower_components)
	files = files.trim().split('\n').sort(function(a, b) {
		return a.length - b.length;
	});

	var f = files[0];

	if (!f) {
		return {};
	}

	log.verbose.writeln('Guessed --config: ' + f);

	if (files.length > 1) {
		log.verbose.writeln('\tOther candidates were:');
		files.forEach(function(name) {
			log.verbose.writeln('\t' + name);
		});
	}

	var config = getConfigRecursive({
		mainConfigFile: f
	});

	// get baseUrl from the config, then determine --entry-point by finding a
	// directory somewhere in the absolute path to the config file that matches
	// baseUrl
	var baseUrlMatcher = new RegExp('\\/' + escapeRegExp(config.baseUrl) + '\\/.*', 'g');
	var abspath = path.resolve(root, f) + '/';
	var entryPoint = abspath.replace(baseUrlMatcher, '');

	log.verbose.writeln('Guessed --entry-point: ' + entryPoint);

	// make baseUrl absolute
	config.baseUrl = path.resolve(entryPoint, config.baseUrl.replace(/^\//, ''));
	return config;
});