1
0
Fork 0

Combine script now handles relative includes in subdirectories

This is a bit of a kluge, specific to our scenerio, but it works.
protolib
Mike Gerwitz 2014-07-09 00:11:32 -04:00
parent ad5291eb8d
commit 913a497492
4 changed files with 21 additions and 8 deletions

View File

@ -47,6 +47,6 @@ exports.require = function( module )
*
* @return {udnefined}
*/
exports.testCase = require( __dirname + '/inc-testcase.js' );
exports.testCase = require( './inc-testcase.js' );

View File

@ -127,6 +127,7 @@ for module in $cat_modules; do
echo "( function( module, __dirname )"
echo "{"
echo " var exports = module.exports = {};"
echo " __cwd = '$( dirname "$module" )';"
# add the module, removing trailing commas
cat $filename | $RMTRAIL
@ -171,6 +172,7 @@ if [ "$INC_TEST" ]; then
echo "( function( module, __dirname )"
echo "{"
echo " var exports = module.exports = {};"
echo " __cwd = '.';"
# write out current test to make debugging easier in browsers with very
# little debugging support

View File

@ -26,7 +26,7 @@ module.common = module['test/common'] = { exports: {
testCase: function()
{
return require( 'test/inc-testcase' ).apply( this, arguments );
return require( '/test/inc-testcase' ).apply( this, arguments );
}
} };

View File

@ -15,7 +15,7 @@
*/
var easejs = {};
( function( ns_exports )
( function( ns_exports, __cwd )
{
/**
* CommonJS module exports
@ -42,15 +42,26 @@ var easejs = {};
*/
var require = function( module_id )
{
// remove the './' directory prefix (every module is currently included
// via a relative path), stupidly remove ../'s and remove .js extensions
var id_clean = module_id.replace( /^\.?\/|[^/]*?\/\.\.\/|\.js$/, '' );
// anything that is not an absolute require path will be prefixed
// with __cwd, which is set by the combined module; this allows
// including relative paths (but note that this also means that
// modules that perform ad-hoc conditional requires after another
// module has been processed may not work properly; we don't do
// this, though)
var id_norm = ( module_id.substr( 0, 1 ) === '/' )
? module_id
: __cwd + '/' + module_id;
// strip `../`, poorly strip `./` (for example, it would also strip
// `foo./`, but we know that this won't ever be the case with our
// files), and strip leading `/`
var id_clean = id_norm.replace( /([^\/]+\/\.\.\/|\.\/|^\/)/g, '' );
// attempt to retrieve the module
var mod = module[ id_clean ];
if ( mod === undefined )
{
throw "[ease.js] Undefined module: " + module_id;
throw "[ease.js] Undefined module: " + id_clean;
}
return mod.exports;
@ -65,5 +76,5 @@ var easejs = {};
ns_exports.Interface = module['interface'].exports;
ns_exports.Trait = module['Trait'].exports;
ns_exports.version = module['version'].exports;
} )( easejs );
} )( easejs, '.' );