From 4c9ecbdf57ff3370e5d11babc6d281b027d5f77d Mon Sep 17 00:00:00 2001 From: Justin Meyer Date: Sun, 29 Jun 2014 23:20:28 -0500 Subject: [PATCH] fixes #129 and makes it possible to list require as a dependency --- dist/system-production-csp.js | 51 ++++++++++++++----- dist/system-production-csp.min.js | 2 +- dist/system.js | 51 ++++++++++++++----- dist/system.min.js | 2 +- lib/extension-amd.js | 35 ++++++++++--- lib/extension-cjs.js | 16 ++++-- test/test.js | 9 ++++ .../amd-simplified-cjs-aliased-require1.js | 7 +++ .../amd-simplified-cjs-aliased-require2.js | 8 +++ 9 files changed, 143 insertions(+), 38 deletions(-) create mode 100644 test/tests/amd-simplified-cjs-aliased-require1.js create mode 100644 test/tests/amd-simplified-cjs-aliased-require2.js diff --git a/dist/system-production-csp.js b/dist/system-production-csp.js index c3b0779d..3044c004 100644 --- a/dist/system-production-csp.js +++ b/dist/system-production-csp.js @@ -1039,13 +1039,19 @@ function cjs(loader) { // CJS Module Format // require('...') || exports[''] = ... || exports.asd = ... || module.exports = ... var cjsExportsRegEx = /(?:^\s*|[}{\(\);,\n=:\?\&]\s*|module\.)(exports\s*\[\s*('[^']+'|"[^"]+")\s*\]|\exports\s*\.\s*[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*|exports\s*\=)/; - var cjsRequireRegEx = /(?:^\s*|[}{\(\);,\n=:\?\&]\s*)require\s*\(\s*("([^"]+)"|'([^']+)')\s*\)/g; + var cjsRequirePre = "(?:^\\s*|[}{\\(\\);,\\n=:\\?\\&]\\s*)"; + var cjsRequirePost = "\\s*\\(\\s*(\"([^\"]+)\"|'([^']+)')\\s*\\)"; + var cjsRequireRegEx = new RegExp(cjsRequirePre+"require"+cjsRequirePost,"g"); var commentRegEx = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg; - function getCJSDeps(source) { + function getCJSDeps(source, requireAlias) { cjsExportsRegEx.lastIndex = 0; - cjsRequireRegEx.lastIndex = 0; - + + // If a requireAlias is given, generate the regexp; otherwise, use the cached version. + var requireRegEx = requireAlias ? + new RegExp(cjsRequirePre+(requireAlias)+cjsRequirePost,"g") : + cjsRequireRegEx; + requireRegEx.lastIndex = 0; var deps = []; // remove comments from the source first @@ -1053,7 +1059,7 @@ function cjs(loader) { var match; - while (match = cjsRequireRegEx.exec(source)) + while (match = requireRegEx.exec(source)) deps.push(match[2] || match[3]); return deps; @@ -1150,11 +1156,26 @@ function amd(loader) { var isNode = typeof module != 'undefined' && module.exports; + // Matches parenthesis + var parensRegExp = /\(([^)]+)/; + var commentRegEx = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg; + var argRegEx = /[\w\d]+/g; + function getRequireAlias(source, index){ + var match = source.match(parensRegExp); + if(match){ + var args = []; + match[1].replace(commentRegEx,"").replace(argRegEx, function(arg){ + args.push(arg); + }); + return args[index||0]; + } + }; + + // AMD Module Format Detection RegEx // define([.., .., ..], ...) // define(varName); || define(function(require, exports) {}); || define({}) var amdRegEx = /(?:^\s*|[}{\(\);,\n\?\&]\s*)define\s*\(\s*("[^"]+"\s*,\s*|'[^']+'\s*,\s*)?\s*(\[(\s*("[^"]+"|'[^']+')\s*,)*(\s*("[^"]+"|'[^']+')\s*,?\s*)?\]|function\s*|{|[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*\))/; - /* AMD-compatible require To copy RequireJS, set window.require = window.requirejs = loader.require @@ -1229,10 +1250,7 @@ function amd(loader) { } if (!(deps instanceof Array)) { factory = deps; - // CommonJS AMD form - if (!loader._getCJSDeps) - throw "AMD extension needs CJS extension for AMD CJS support"; - deps = ['require', 'exports', 'module'].concat(loader._getCJSDeps(factory.toString())); + deps = ['require','exports','module'] } if (typeof factory != 'function') @@ -1242,8 +1260,17 @@ function amd(loader) { // remove system dependencies var requireIndex, exportsIndex, moduleIndex - if ((requireIndex = indexOf.call(deps, 'require')) != -1) - deps.splice(requireIndex, 1); + + if ((requireIndex = indexOf.call(deps, 'require')) != -1) { + + deps.splice(requireIndex, 1); + // CommonJS AMD form + if (!loader._getCJSDeps) + throw "AMD extension needs CJS extension for AMD CJS support"; + var factoryText = factory.toString(); + deps = deps.concat(loader._getCJSDeps(factoryText, getRequireAlias(factoryText, requireIndex))); + } + if ((exportsIndex = indexOf.call(deps, 'exports')) != -1) deps.splice(exportsIndex, 1); diff --git a/dist/system-production-csp.min.js b/dist/system-production-csp.min.js index 0360472c..2dede58a 100644 --- a/dist/system-production-csp.min.js +++ b/dist/system-production-csp.min.js @@ -5,4 +5,4 @@ * MIT License */ -!function(__$global){function __eval(__source,__global,__address,__sourceMap){try{__source=(__global!=__$global?"with(__global) { (function() { "+__source+" \n }).call(__global); }":__source)+"\n//# sourceURL="+__address+(__sourceMap?"\n//# sourceMappingURL="+__sourceMap:""),eval(__source)}catch(e){throw"SyntaxError"==e.name&&(e.message="Evaluating "+__address+"\n "+e.message),System.trace&&0==System.execute&&(e="Execution error for "+__address+": "+e.stack||e),e}}__$global.upgradeSystemLoader=function(){function parseURI(e){var t=String(e).replace(/^\s+|\s+$/g,"").match(/^([^:\/?#]+:)?(\/\/(?:[^:@]*(?::[^:@]*)?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/);return t?{href:t[0]||"",protocol:t[1]||"",authority:t[2]||"",host:t[3]||"",hostname:t[4]||"",port:t[5]||"",pathname:t[6]||"",search:t[7]||"",hash:t[8]||""}:null}function toAbsoluteURL(e,t){function a(e){var t=[];return e.replace(/^(\.\.?(\/|$))+/,"").replace(/\/(\.(\/|$))+/g,"/").replace(/\/\.\.$/,"/../").replace(/\/?[^\/]*/g,function(e){"/.."===e?t.pop():t.push(e)}),t.join("").replace(/^\//,"/"===e.charAt(0)?"/":"")}return t=parseURI(t||""),e=parseURI(e||""),t&&e?(t.protocol||e.protocol)+(t.protocol||t.authority?t.authority:e.authority)+a(t.protocol||t.authority||"/"===t.pathname.charAt(0)?t.pathname:t.pathname?(e.authority&&!e.pathname?"/":"")+e.pathname.slice(0,e.pathname.lastIndexOf("/")+1)+t.pathname:e.pathname)+(t.protocol||t.authority||t.pathname?t.search:t.search||e.search)+t.hash:null}function scriptLoader(e){"undefined"==typeof indexOf&&(indexOf=Array.prototype.indexOf);var t=document.getElementsByTagName("head")[0];e.onScriptLoad=function(){},e.fetch=function(a){return new Promise(function(r,n){function o(){l.readyState&&"loaded"!=l.readyState&&"complete"!=l.readyState||(s(),e.onScriptLoad(a),r(""))}function i(e){s(),n(e)}function s(){l.detachEvent?l.detachEvent("onreadystatechange",o):(l.removeEventListener("load",o,!1),l.removeEventListener("error",i,!1)),t.removeChild(l)}var l=document.createElement("script");l.async=!0,l.attachEvent&&l.attachEvent("onreadystatechange",o),l.addEventListener("load",o,!1),l.addEventListener("error",i,!1),l.src=a.address,t.appendChild(l)})},e.scriptLoader=!0}function meta(e){function t(e,t){var a=e.meta&&e.meta[t.name];if(a)for(var r in a)t.metadata[r]=t.metadata[r]||a[r]}var a=/^(\s*\/\*.*\*\/|\s*\/\/[^\n]*|\s*"[^"]+"\s*;?|\s*'[^']+'\s*;?)+/,r=/\/\*.*\*\/|\/\/[^\n]*|"[^"]+"\s*;?|'[^']+'\s*;?/g;e.meta={};var n=e.locate;e.locate=function(e){return t(this,e),n.call(this,e)};var o=e.translate;e.translate=function(e){var n=e.source.match(a);if(n)for(var i=n[0].match(r),s=0;s0&&1!=r.length)throw"Invalid System.register form for "+t+". Declare function must take one argument.";o={declarative:!0,deps:a,declare:r}}if(t)e.defined[t]||(e.defined[t]=o);else if(o.declarative){if(c)throw"Multiple anonymous System.register calls in the same module file.";c=o}}function n(e){if(!e.register){e.register=r,e.defined||(e.defined={});var t=e.onScriptLoad;e.onScriptLoad=function(e){t(e),c&&(e.metadata.entry=c),(c||m)&&(e.metadata.format=e.metadata.format||"register"),m&&(e.metadata.registered=!0)}}}function o(e,t,a){if(a[e.groupIndex]=a[e.groupIndex]||[],-1==indexOf.call(a[e.groupIndex],e)){a[e.groupIndex].push(e);for(var r=0;r=0;i--){for(var l=r[i],u=0;ua;a++)"/"===e[a]&&t++;return t}function r(e,t,a){return a+e.substr(t)}function n(e,n,o){var i,s,l,d,u=0,f=0;if(n)for(var c in o.map){var m=o.map[c];if("object"==typeof m&&t(n,c)&&(l=a(c),!(f>=l)))for(var p in m)t(e,p)&&(d=a(p),u>=d||(i=p,u=d,s=c,f=l))}if(i)return r(e,i.length,o.map[s][i]);for(var c in o.map){var m=o.map[c];if("string"==typeof m&&t(e,c)){var d=a(c);u>=d||(i=c,u=d)}}return i?r(e,i.length,o.map[i]):e}e.map=e.map||{};var o=e.normalize;e.normalize=function(e,t,a){var r=this;r.map||(r.map={});var i=!1;return"/"==e.substr(e.length-1,1)&&(i=!0,e+="#"),Promise.resolve(o.call(r,e,t,a)).then(function(e){if(e=n(e,t,r),i){var a=e.split("/");a.pop();var o=a.pop();a.push(o),a.push(o),e=a.join("/")}return e})}}function bundles(e){"undefined"==typeof indexOf&&(indexOf=Array.prototype.indexOf),e.bundles=e.bundles||{};var t=e.fetch;e.fetch=function(e){var a=this;if(a.trace)return t.call(this,e);a.bundles||(a.bundles={});for(var r in a.bundles)if(-1!=indexOf.call(a.bundles[r],e.name))return Promise.resolve(a.normalize(r)).then(function(e){return a.bundles[e]=a.bundles[e]||a.bundles[r],a.meta=a.meta||{},a.meta[e]=a.meta[e]||{},a.meta[e].bundle=!0,a.load(e)}).then(function(){return""});return t.call(this,e)}}function versions(e){"undefined"==typeof indexOf&&(indexOf=Array.prototype.indexOf);var t=/^(\d+)(?:\.(\d+)(?:\.(\d+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?)?)?$/,a=function(e,t){var a,r=e.split("."),n=t.split(".");r[2]&&-1!=(a=indexOf.call(r[2],"-"))&&r.splice(2,1,r[2].substr(0,a),r[2].substr(a+1)),n[2]&&-1!=(a=indexOf.call(n[2],"-"))&&n.splice(2,1,n[2].substr(0,a),n[2].substr(a+1));for(var o=0;oparseInt(n[o])?1:-1}return 0};e.versions=e.versions||{};var r=e.normalize;e.normalize=function(n,o,i){e.versions||(e.versions={});var s,l,d=this.versions;if(n.indexOf("@")>0){var u=n.lastIndexOf("@"),f=n.substr(u+1,n.length-u-1).split("/");s=f[0],l=f.length,n=n.substr(0,u)+n.substr(u+s.length+1,n.length-u-s.length-1)}return Promise.resolve(r.call(this,n,o,i)).then(function(e){var r,n,o,i,u=e.indexOf("@");if(s&&(-1==u||0==u)){var f=e.split("/");f[f.length-l]+="@"+s,e=f.join("/"),u=e.indexOf("@")}if(-1==u||0==u){for(var c in d)if(i=d[c],e.substr(0,c.length)==c&&(o=e.substr(c.length,1),!o||"/"==o))return c+"@"+("string"==typeof i?i:i[i.length-1])+e.substr(c.length);return e}r=e.substr(u+1).split("/")[0];var m,p=r.length;if("^"==r.substr(0,1)&&(r=r.substr(1),m=!0),n=r.match(t),!n)return e;m&&(n[2]||(m=!1),n[3]||(n[2]>0?n[3]="0":m=!1)),m&&(n[1]>0?(n[2]||(r=n[1]+".0.0"),n[3]||(r=n[1]+".0"),m=r,n=[n[1]]):n[2]>0?(m=r,n=[0,n[2]]):(m=!1,n=[0,0,n[3]]),r=n.join("."));var g=e.substr(0,u);if(i=d[g]||[],"string"==typeof i&&(i=[i]),!n[3]||m)for(var h=i.length-1;h>=0;h--){var v=i[h];if(v.substr(0,r.length)==r&&v.substr(r.length,1).match(/^[\.\-]?$/)&&(!m||m&&-1!=a(v,m)))return g+"@"+v+e.substr(g.length+p+1)}return-1==indexOf.call(i,r)&&(i.push(r),i.sort(a),e=g+"@"+r+e.substr(g.length+p+1),n[3]&&-1!=(u=indexOf.call(i,n[1]+"."+n[2]))&&i.splice(u,1),n[2]&&-1!=(u=indexOf.call(i,n[1]))&&i.splice(u,1),d[g]=1==i.length?i[0]:i),e})}}function depCache(e){e.depCache=e.depCache||{},loaderLocate=e.locate,e.locate=function(e){var t=this;t.depCache||(t.depCache={});var a=t.depCache[e.name];if(a)for(var r=0;rt;t++)if(this[t]===e)return t;return-1},originalSystem=__$global.System,System=__$global.System=new LoaderPolyfill(originalSystem);System.baseURL=originalSystem.baseURL,System.paths={"*":"*.js"},System.originalSystem=originalSystem,System.noConflict=function(){__$global.SystemJS=System,__$global.System=System.originalSystem},scriptLoader(System),meta(System),register(System),core(System),global(System),cjs(System),amd(System),map(System),bundles(System),versions(System),depCache(System),System.paths["@traceur"]||(System.paths["@traceur"]=__$curScript&&__$curScript.getAttribute("data-traceur-src")||(__$curScript&&__$curScript.src?__$curScript.src.substr(0,__$curScript.src.lastIndexOf("/")+1):System.baseURL+(System.baseURL.lastIndexOf("/")==System.baseURL.length-1?"":"/"))+"traceur.js")};var __$curScript;!function(e){if("undefined"!=typeof window){var t=document.getElementsByTagName("script");if(__$curScript=t[t.length-1],e.System&&e.LoaderPolyfill)e.upgradeSystemLoader();else{var a=__$curScript.src,r=a.substr(0,a.lastIndexOf("/")+1);document.write('')}}else{var n=require("es6-module-loader");e.System=n.System,e.Loader=n.Loader,e.upgradeSystemLoader(),module.exports=e.System}}(__$global)}("undefined"!=typeof window?window:global); \ No newline at end of file diff --git a/dist/system.js b/dist/system.js index b71cee5c..e12e837e 100644 --- a/dist/system.js +++ b/dist/system.js @@ -980,13 +980,19 @@ function cjs(loader) { // CJS Module Format // require('...') || exports[''] = ... || exports.asd = ... || module.exports = ... var cjsExportsRegEx = /(?:^\s*|[}{\(\);,\n=:\?\&]\s*|module\.)(exports\s*\[\s*('[^']+'|"[^"]+")\s*\]|\exports\s*\.\s*[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*|exports\s*\=)/; - var cjsRequireRegEx = /(?:^\s*|[}{\(\);,\n=:\?\&]\s*)require\s*\(\s*("([^"]+)"|'([^']+)')\s*\)/g; + var cjsRequirePre = "(?:^\\s*|[}{\\(\\);,\\n=:\\?\\&]\\s*)"; + var cjsRequirePost = "\\s*\\(\\s*(\"([^\"]+)\"|'([^']+)')\\s*\\)"; + var cjsRequireRegEx = new RegExp(cjsRequirePre+"require"+cjsRequirePost,"g"); var commentRegEx = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg; - function getCJSDeps(source) { + function getCJSDeps(source, requireAlias) { cjsExportsRegEx.lastIndex = 0; - cjsRequireRegEx.lastIndex = 0; - + + // If a requireAlias is given, generate the regexp; otherwise, use the cached version. + var requireRegEx = requireAlias ? + new RegExp(cjsRequirePre+(requireAlias)+cjsRequirePost,"g") : + cjsRequireRegEx; + requireRegEx.lastIndex = 0; var deps = []; // remove comments from the source first @@ -994,7 +1000,7 @@ function cjs(loader) { var match; - while (match = cjsRequireRegEx.exec(source)) + while (match = requireRegEx.exec(source)) deps.push(match[2] || match[3]); return deps; @@ -1091,11 +1097,26 @@ function amd(loader) { var isNode = typeof module != 'undefined' && module.exports; + // Matches parenthesis + var parensRegExp = /\(([^)]+)/; + var commentRegEx = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg; + var argRegEx = /[\w\d]+/g; + function getRequireAlias(source, index){ + var match = source.match(parensRegExp); + if(match){ + var args = []; + match[1].replace(commentRegEx,"").replace(argRegEx, function(arg){ + args.push(arg); + }); + return args[index||0]; + } + }; + + // AMD Module Format Detection RegEx // define([.., .., ..], ...) // define(varName); || define(function(require, exports) {}); || define({}) var amdRegEx = /(?:^\s*|[}{\(\);,\n\?\&]\s*)define\s*\(\s*("[^"]+"\s*,\s*|'[^']+'\s*,\s*)?\s*(\[(\s*("[^"]+"|'[^']+')\s*,)*(\s*("[^"]+"|'[^']+')\s*,?\s*)?\]|function\s*|{|[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*\))/; - /* AMD-compatible require To copy RequireJS, set window.require = window.requirejs = loader.require @@ -1170,10 +1191,7 @@ function amd(loader) { } if (!(deps instanceof Array)) { factory = deps; - // CommonJS AMD form - if (!loader._getCJSDeps) - throw "AMD extension needs CJS extension for AMD CJS support"; - deps = ['require', 'exports', 'module'].concat(loader._getCJSDeps(factory.toString())); + deps = ['require','exports','module'] } if (typeof factory != 'function') @@ -1183,8 +1201,17 @@ function amd(loader) { // remove system dependencies var requireIndex, exportsIndex, moduleIndex - if ((requireIndex = indexOf.call(deps, 'require')) != -1) - deps.splice(requireIndex, 1); + + if ((requireIndex = indexOf.call(deps, 'require')) != -1) { + + deps.splice(requireIndex, 1); + // CommonJS AMD form + if (!loader._getCJSDeps) + throw "AMD extension needs CJS extension for AMD CJS support"; + var factoryText = factory.toString(); + deps = deps.concat(loader._getCJSDeps(factoryText, getRequireAlias(factoryText, requireIndex))); + } + if ((exportsIndex = indexOf.call(deps, 'exports')) != -1) deps.splice(exportsIndex, 1); diff --git a/dist/system.min.js b/dist/system.min.js index 4c5f6f3d..479b8d0b 100644 --- a/dist/system.min.js +++ b/dist/system.min.js @@ -5,4 +5,4 @@ * MIT License */ -!function(__$global){function __eval(__source,__global,__address,__sourceMap){try{__source=(__global!=__$global?"with(__global) { (function() { "+__source+" \n }).call(__global); }":__source)+"\n//# sourceURL="+__address+(__sourceMap?"\n//# sourceMappingURL="+__sourceMap:""),eval(__source)}catch(e){throw"SyntaxError"==e.name&&(e.message="Evaluating "+__address+"\n "+e.message),System.trace&&0==System.execute&&(e="Execution error for "+__address+": "+e.stack||e),e}}__$global.upgradeSystemLoader=function(){function parseURI(e){var t=String(e).replace(/^\s+|\s+$/g,"").match(/^([^:\/?#]+:)?(\/\/(?:[^:@]*(?::[^:@]*)?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/);return t?{href:t[0]||"",protocol:t[1]||"",authority:t[2]||"",host:t[3]||"",hostname:t[4]||"",port:t[5]||"",pathname:t[6]||"",search:t[7]||"",hash:t[8]||""}:null}function toAbsoluteURL(e,t){function a(e){var t=[];return e.replace(/^(\.\.?(\/|$))+/,"").replace(/\/(\.(\/|$))+/g,"/").replace(/\/\.\.$/,"/../").replace(/\/?[^\/]*/g,function(e){"/.."===e?t.pop():t.push(e)}),t.join("").replace(/^\//,"/"===e.charAt(0)?"/":"")}return t=parseURI(t||""),e=parseURI(e||""),t&&e?(t.protocol||e.protocol)+(t.protocol||t.authority?t.authority:e.authority)+a(t.protocol||t.authority||"/"===t.pathname.charAt(0)?t.pathname:t.pathname?(e.authority&&!e.pathname?"/":"")+e.pathname.slice(0,e.pathname.lastIndexOf("/")+1)+t.pathname:e.pathname)+(t.protocol||t.authority||t.pathname?t.search:t.search||e.search)+t.hash:null}function meta(e){function t(e,t){var a=e.meta&&e.meta[t.name];if(a)for(var r in a)t.metadata[r]=t.metadata[r]||a[r]}var a=/^(\s*\/\*.*\*\/|\s*\/\/[^\n]*|\s*"[^"]+"\s*;?|\s*'[^']+'\s*;?)+/,r=/\/\*.*\*\/|\/\/[^\n]*|"[^"]+"\s*;?|'[^']+'\s*;?/g;e.meta={};var n=e.locate;e.locate=function(e){return t(this,e),n.call(this,e)};var o=e.translate;e.translate=function(e){var n=e.source.match(a);if(n)for(var l=n[0].match(r),i=0;i0&&1!=r.length)throw"Invalid System.register form for "+t+". Declare function must take one argument.";o={declarative:!0,deps:a,declare:r}}if(t)e.defined[t]||(e.defined[t]=o);else if(o.declarative){if(c)throw"Multiple anonymous System.register calls in the same module file.";c=o}}function n(e){if(!e.register){e.register=r,e.defined||(e.defined={});var t=e.onScriptLoad;e.onScriptLoad=function(e){t(e),c&&(e.metadata.entry=c),(c||m)&&(e.metadata.format=e.metadata.format||"register"),m&&(e.metadata.registered=!0)}}}function o(e,t,a){if(a[e.groupIndex]=a[e.groupIndex]||[],-1==indexOf.call(a[e.groupIndex],e)){a[e.groupIndex].push(e);for(var r=0;r=0;l--){for(var s=r[l],u=0;ua;a++)"/"===e[a]&&t++;return t}function r(e,t,a){return a+e.substr(t)}function n(e,n,o){var l,i,s,d,u=0,f=0;if(n)for(var c in o.map){var m=o.map[c];if("object"==typeof m&&t(n,c)&&(s=a(c),!(f>=s)))for(var p in m)t(e,p)&&(d=a(p),u>=d||(l=p,u=d,i=c,f=s))}if(l)return r(e,l.length,o.map[i][l]);for(var c in o.map){var m=o.map[c];if("string"==typeof m&&t(e,c)){var d=a(c);u>=d||(l=c,u=d)}}return l?r(e,l.length,o.map[l]):e}e.map=e.map||{};var o=e.normalize;e.normalize=function(e,t,a){var r=this;r.map||(r.map={});var l=!1;return"/"==e.substr(e.length-1,1)&&(l=!0,e+="#"),Promise.resolve(o.call(r,e,t,a)).then(function(e){if(e=n(e,t,r),l){var a=e.split("/");a.pop();var o=a.pop();a.push(o),a.push(o),e=a.join("/")}return e})}}function plugins(e){"undefined"==typeof indexOf&&(indexOf=Array.prototype.indexOf);var t=e.normalize;e.normalize=function(e,a,r){var n,o=this;return a&&-1!=(n=a.indexOf("!"))&&(a=a.substr(0,n)),Promise.resolve(t.call(o,e,a,r)).then(function(e){var t=e.lastIndexOf("!");if(-1!=t){var n=e.substr(0,t),l=e.substr(t+1)||n.substr(n.lastIndexOf(".")+1);return new Promise(function(e){e(o.normalize(l,a,r))}).then(function(e){return l=e,o.normalize(n,a,r)}).then(function(e){return e+"!"+l})}return e})};var a=e.locate;e.locate=function(e){var t=this,r=e.name,n=r.lastIndexOf("!");if(-1!=n){var o=r.substr(n+1);e.name=r.substr(0,n);var l=t.pluginLoader||t;return l["import"](o).then(function(){var a=l.get(o);return a=a["default"]||a,a.build===!1&&t.pluginLoader&&(e.metadata.build=!1),e.metadata.plugin=a,e.metadata.pluginName=o,e.metadata.pluginArgument=e.name,a.locate?a.locate.call(t,e):Promise.resolve(t.locate(e)).then(function(e){return e.substr(0,e.length-3)})})}return a.call(this,e)};var r=e.fetch;e.fetch=function(e){var t=this;return e.metadata.build===!1?"":e.metadata.plugin&&e.metadata.plugin.fetch&&!e.metadata.pluginFetchCalled?(e.metadata.pluginFetchCalled=!0,e.metadata.plugin.fetch.call(t,e,r)):r.call(t,e)};var n=e.translate;e.translate=function(e){var t=this;return e.metadata.plugin&&e.metadata.plugin.translate?Promise.resolve(e.metadata.plugin.translate.call(t,e)).then(function(a){return a?a:n.call(t,e)}):n.call(t,e)};var o=e.instantiate;e.instantiate=function(e){var t=this;return e.metadata.plugin&&e.metadata.plugin.instantiate?Promise.resolve(e.metadata.plugin.instantiate.call(t,e)).then(function(a){return e.metadata.format="defined",e.metadata.execute=function(){return a},o.call(t,e)}):e.metadata.plugin&&e.metadata.plugin.build===!1?(e.metadata.format="defined",e.metadata.deps.push(e.metadata.pluginName),e.metadata.execute=function(){return t.newModule({})},o.call(t,e)):o.call(t,e)}}function bundles(e){"undefined"==typeof indexOf&&(indexOf=Array.prototype.indexOf),e.bundles=e.bundles||{};var t=e.fetch;e.fetch=function(e){var a=this;if(a.trace)return t.call(this,e);a.bundles||(a.bundles={});for(var r in a.bundles)if(-1!=indexOf.call(a.bundles[r],e.name))return Promise.resolve(a.normalize(r)).then(function(e){return a.bundles[e]=a.bundles[e]||a.bundles[r],a.meta=a.meta||{},a.meta[e]=a.meta[e]||{},a.meta[e].bundle=!0,a.load(e)}).then(function(){return""});return t.call(this,e)}}function versions(e){"undefined"==typeof indexOf&&(indexOf=Array.prototype.indexOf);var t=/^(\d+)(?:\.(\d+)(?:\.(\d+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?)?)?$/,a=function(e,t){var a,r=e.split("."),n=t.split(".");r[2]&&-1!=(a=indexOf.call(r[2],"-"))&&r.splice(2,1,r[2].substr(0,a),r[2].substr(a+1)),n[2]&&-1!=(a=indexOf.call(n[2],"-"))&&n.splice(2,1,n[2].substr(0,a),n[2].substr(a+1));for(var o=0;oparseInt(n[o])?1:-1}return 0};e.versions=e.versions||{};var r=e.normalize;e.normalize=function(n,o,l){e.versions||(e.versions={});var i,s,d=this.versions;if(n.indexOf("@")>0){var u=n.lastIndexOf("@"),f=n.substr(u+1,n.length-u-1).split("/");i=f[0],s=f.length,n=n.substr(0,u)+n.substr(u+i.length+1,n.length-u-i.length-1)}return Promise.resolve(r.call(this,n,o,l)).then(function(e){var r,n,o,l,u=e.indexOf("@");if(i&&(-1==u||0==u)){var f=e.split("/");f[f.length-s]+="@"+i,e=f.join("/"),u=e.indexOf("@")}if(-1==u||0==u){for(var c in d)if(l=d[c],e.substr(0,c.length)==c&&(o=e.substr(c.length,1),!o||"/"==o))return c+"@"+("string"==typeof l?l:l[l.length-1])+e.substr(c.length);return e}r=e.substr(u+1).split("/")[0];var m,p=r.length;if("^"==r.substr(0,1)&&(r=r.substr(1),m=!0),n=r.match(t),!n)return e;m&&(n[2]||(m=!1),n[3]||(n[2]>0?n[3]="0":m=!1)),m&&(n[1]>0?(n[2]||(r=n[1]+".0.0"),n[3]||(r=n[1]+".0"),m=r,n=[n[1]]):n[2]>0?(m=r,n=[0,n[2]]):(m=!1,n=[0,0,n[3]]),r=n.join("."));var g=e.substr(0,u);if(l=d[g]||[],"string"==typeof l&&(l=[l]),!n[3]||m)for(var h=l.length-1;h>=0;h--){var v=l[h];if(v.substr(0,r.length)==r&&v.substr(r.length,1).match(/^[\.\-]?$/)&&(!m||m&&-1!=a(v,m)))return g+"@"+v+e.substr(g.length+p+1)}return-1==indexOf.call(l,r)&&(l.push(r),l.sort(a),e=g+"@"+r+e.substr(g.length+p+1),n[3]&&-1!=(u=indexOf.call(l,n[1]+"."+n[2]))&&l.splice(u,1),n[2]&&-1!=(u=indexOf.call(l,n[1]))&&l.splice(u,1),d[g]=1==l.length?l[0]:l),e})}}function depCache(e){e.depCache=e.depCache||{},loaderLocate=e.locate,e.locate=function(e){var t=this;t.depCache||(t.depCache={});var a=t.depCache[e.name];if(a)for(var r=0;rt;t++)if(this[t]===e)return t;return-1},originalSystem=__$global.System,System=__$global.System=new LoaderPolyfill(originalSystem);System.baseURL=originalSystem.baseURL,System.paths={"*":"*.js"},System.originalSystem=originalSystem,System.noConflict=function(){__$global.SystemJS=System,__$global.System=System.originalSystem},meta(System),register(System),core(System),global(System),cjs(System),amd(System),map(System),plugins(System),bundles(System),versions(System),depCache(System),System.paths["@traceur"]||(System.paths["@traceur"]=__$curScript&&__$curScript.getAttribute("data-traceur-src")||(__$curScript&&__$curScript.src?__$curScript.src.substr(0,__$curScript.src.lastIndexOf("/")+1):System.baseURL+(System.baseURL.lastIndexOf("/")==System.baseURL.length-1?"":"/"))+"traceur.js")};var __$curScript;!function(e){if("undefined"!=typeof window){var t=document.getElementsByTagName("script");if(__$curScript=t[t.length-1],e.System&&e.LoaderPolyfill)e.upgradeSystemLoader();else{var a=__$curScript.src,r=a.substr(0,a.lastIndexOf("/")+1);document.write('')}}else{var n=require("es6-module-loader");e.System=n.System,e.Loader=n.Loader,e.upgradeSystemLoader(),module.exports=e.System}}(__$global)}("undefined"!=typeof window?window:global); \ No newline at end of file diff --git a/lib/extension-amd.js b/lib/extension-amd.js index 8a730742..31aedd76 100644 --- a/lib/extension-amd.js +++ b/lib/extension-amd.js @@ -7,11 +7,26 @@ function amd(loader) { var isNode = typeof module != 'undefined' && module.exports; + // Matches parenthesis + var parensRegExp = /\(([^)]+)/; + var commentRegEx = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg; + var argRegEx = /[\w\d]+/g; + function getRequireAlias(source, index){ + var match = source.match(parensRegExp); + if(match){ + var args = []; + match[1].replace(commentRegEx,"").replace(argRegEx, function(arg){ + args.push(arg); + }); + return args[index||0]; + } + }; + + // AMD Module Format Detection RegEx // define([.., .., ..], ...) // define(varName); || define(function(require, exports) {}); || define({}) var amdRegEx = /(?:^\s*|[}{\(\);,\n\?\&]\s*)define\s*\(\s*("[^"]+"\s*,\s*|'[^']+'\s*,\s*)?\s*(\[(\s*("[^"]+"|'[^']+')\s*,)*(\s*("[^"]+"|'[^']+')\s*,?\s*)?\]|function\s*|{|[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*\))/; - /* AMD-compatible require To copy RequireJS, set window.require = window.requirejs = loader.require @@ -86,10 +101,7 @@ function amd(loader) { } if (!(deps instanceof Array)) { factory = deps; - // CommonJS AMD form - if (!loader._getCJSDeps) - throw "AMD extension needs CJS extension for AMD CJS support"; - deps = ['require', 'exports', 'module'].concat(loader._getCJSDeps(factory.toString())); + deps = ['require','exports','module'] } if (typeof factory != 'function') @@ -99,8 +111,17 @@ function amd(loader) { // remove system dependencies var requireIndex, exportsIndex, moduleIndex - if ((requireIndex = indexOf.call(deps, 'require')) != -1) - deps.splice(requireIndex, 1); + + if ((requireIndex = indexOf.call(deps, 'require')) != -1) { + + deps.splice(requireIndex, 1); + // CommonJS AMD form + if (!loader._getCJSDeps) + throw "AMD extension needs CJS extension for AMD CJS support"; + var factoryText = factory.toString(); + deps = deps.concat(loader._getCJSDeps(factoryText, getRequireAlias(factoryText, requireIndex))); + } + if ((exportsIndex = indexOf.call(deps, 'exports')) != -1) deps.splice(exportsIndex, 1); diff --git a/lib/extension-cjs.js b/lib/extension-cjs.js index 13a7cab9..4bd72572 100644 --- a/lib/extension-cjs.js +++ b/lib/extension-cjs.js @@ -6,13 +6,19 @@ function cjs(loader) { // CJS Module Format // require('...') || exports[''] = ... || exports.asd = ... || module.exports = ... var cjsExportsRegEx = /(?:^\s*|[}{\(\);,\n=:\?\&]\s*|module\.)(exports\s*\[\s*('[^']+'|"[^"]+")\s*\]|\exports\s*\.\s*[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*|exports\s*\=)/; - var cjsRequireRegEx = /(?:^\s*|[}{\(\);,\n=:\?\&]\s*)require\s*\(\s*("([^"]+)"|'([^']+)')\s*\)/g; + var cjsRequirePre = "(?:^\\s*|[}{\\(\\);,\\n=:\\?\\&]\\s*)"; + var cjsRequirePost = "\\s*\\(\\s*(\"([^\"]+)\"|'([^']+)')\\s*\\)"; + var cjsRequireRegEx = new RegExp(cjsRequirePre+"require"+cjsRequirePost,"g"); var commentRegEx = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg; - function getCJSDeps(source) { + function getCJSDeps(source, requireAlias) { cjsExportsRegEx.lastIndex = 0; - cjsRequireRegEx.lastIndex = 0; - + + // If a requireAlias is given, generate the regexp; otherwise, use the cached version. + var requireRegEx = requireAlias ? + new RegExp(cjsRequirePre+(requireAlias)+cjsRequirePost,"g") : + cjsRequireRegEx; + requireRegEx.lastIndex = 0; var deps = []; // remove comments from the source first @@ -20,7 +26,7 @@ function cjs(loader) { var match; - while (match = cjsRequireRegEx.exec(source)) + while (match = requireRegEx.exec(source)) deps.push(match[2] || match[3]); return deps; diff --git a/test/test.js b/test/test.js index b6f6f4bb..30b1ca93 100644 --- a/test/test.js +++ b/test/test.js @@ -497,3 +497,12 @@ asyncTest('AMD -> System.register circular -> ES6', function() { }, err); }); +asyncTest('AMD simplified CommonJS wrapping with an aliased require', function() { + System['import']('tests/amd-simplified-cjs-aliased-require1').then(function(m) { + ok(m.require2,"got dependency from aliased require"); + ok(m.require2.amdCJS,"got dependency from aliased require listed as a dependency"); + start(); + }, err); +}); + +console.log("here") diff --git a/test/tests/amd-simplified-cjs-aliased-require1.js b/test/tests/amd-simplified-cjs-aliased-require1.js new file mode 100644 index 00000000..f0deec08 --- /dev/null +++ b/test/tests/amd-simplified-cjs-aliased-require1.js @@ -0,0 +1,7 @@ +define(function(req){ + var r2 = req("./amd-simplified-cjs-aliased-require2"); + return { + name: "require1", + require2: r2 + }; +}); diff --git a/test/tests/amd-simplified-cjs-aliased-require2.js b/test/tests/amd-simplified-cjs-aliased-require2.js new file mode 100644 index 00000000..b1f3c161 --- /dev/null +++ b/test/tests/amd-simplified-cjs-aliased-require2.js @@ -0,0 +1,8 @@ +define(["./amd-dep","require"],function(amdDep, req){ + var amdCJS = req("./amd-cjs-module"); + return { + name: "require2", + amdCJS: amdCJS, + amdDep: amdDep + }; +});