From 913a49749244e9974a3a63f5699a1e8bdb2a6cbe Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Wed, 9 Jul 2014 00:11:32 -0400 Subject: [PATCH] Combine script now handles relative includes in subdirectories This is a bit of a kluge, specific to our scenerio, but it works. --- test/common.js | 2 +- tools/combine | 2 ++ tools/combine-test.tpl | 2 +- tools/combine.tpl | 23 +++++++++++++++++------ 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/test/common.js b/test/common.js index 64119dd..845b73b 100644 --- a/test/common.js +++ b/test/common.js @@ -47,6 +47,6 @@ exports.require = function( module ) * * @return {udnefined} */ -exports.testCase = require( __dirname + '/inc-testcase.js' ); +exports.testCase = require( './inc-testcase.js' ); diff --git a/tools/combine b/tools/combine index c6aa7b2..378c393 100755 --- a/tools/combine +++ b/tools/combine @@ -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 diff --git a/tools/combine-test.tpl b/tools/combine-test.tpl index 574bb79..7f44107 100644 --- a/tools/combine-test.tpl +++ b/tools/combine-test.tpl @@ -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 ); } } }; diff --git a/tools/combine.tpl b/tools/combine.tpl index e97e7e4..16a83f2 100644 --- a/tools/combine.tpl +++ b/tools/combine.tpl @@ -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, '.' );