From c61f5d116a52650ac574cd5ba7a275f72d7e0d44 Mon Sep 17 00:00:00 2001 From: army8735 Date: Wed, 25 Jun 2014 13:11:10 +0800 Subject: [PATCH] config comboSuffix & fix file with timestamp #14 --- dist/seajs-combo-debug.js | 11 ++++++++++- dist/seajs-combo.js | 2 +- src/seajs-combo.js | 11 ++++++++++- tests/spec/combo-map/main.js | 2 +- tests/spec/test.html | 6 +++--- tests/spec/timestamp/a.js,b.js,c.js | 2 ++ tests/spec/timestamp/main.js | 25 +++++++++++++++++++++++++ 7 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 tests/spec/timestamp/a.js,b.js,c.js create mode 100644 tests/spec/timestamp/main.js diff --git a/dist/seajs-combo-debug.js b/dist/seajs-combo-debug.js index 4cc8f9c..ab706b5 100644 --- a/dist/seajs-combo-debug.js +++ b/dist/seajs-combo-debug.js @@ -12,6 +12,7 @@ var comboHash = data.comboHash = {} var comboSyntax = ["??", ","] var comboMaxLength = 2000 var comboExcludes +var comboSuffix seajs.on("load", setComboHash) @@ -25,6 +26,7 @@ function setComboHash(uris) { data.comboSyntax && (comboSyntax = data.comboSyntax) data.comboMaxLength && (comboMaxLength = data.comboMaxLength) + data.comboSuffix && (comboSuffix = data.comboSuffix) comboExcludes = data.comboExcludes var needComboUris = [] @@ -226,7 +228,14 @@ function paths2hash(paths) { } function setHash(root, files) { - var comboPath = root + comboSyntax[0] + files.join(comboSyntax[1]) + var copy = [] + for (var i = 0, len = files.length; i < len; i++) { + copy[i] = files[i].replace(/\?.*$/, '') + } + var comboPath = root + comboSyntax[0] + copy.join(comboSyntax[1]) + if(comboSuffix) { + comboPath += comboSuffix + } var exceedMax = comboPath.length > comboMaxLength // http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url diff --git a/dist/seajs-combo.js b/dist/seajs-combo.js index 92111d3..b216d15 100644 --- a/dist/seajs-combo.js +++ b/dist/seajs-combo.js @@ -1 +1 @@ -!function(){function a(a){var b=a.length;if(!(2>b)){q.comboSyntax&&(s=q.comboSyntax),q.comboMaxLength&&(t=q.comboMaxLength),n=q.comboExcludes;for(var d=[],e=0;b>e;e++){var f=a[e];if(!r[f]){var h=o.get(f);h.status1&&g(c(d))}}function b(a){a.requestUri=r[a.uri]||a.uri}function c(a){return e(d(a))}function d(a){for(var b={__KEYS:[]},c=0,d=a.length;d>c;c++)for(var e=a[c].replace("://","__").split("/"),f=b,g=0,h=e.length;h>g;g++){var i=e[g];f[i]||(f[i]={__KEYS:[]},f.__KEYS.push(i)),f=f[i]}return b}function e(a){for(var b=[],c=a.__KEYS,d=0,e=c.length;e>d;d++){for(var g=c[d],h=g,i=a[g],j=i.__KEYS;1===j.length;)h+="/"+j[0],i=i[j[0]],j=i.__KEYS;j.length&&b.push([h.replace("__","://"),f(i)])}return b}function f(a){for(var b=[],c=a.__KEYS,d=0,e=c.length;e>d;d++){var g=c[d],h=f(a[g]),i=h.length;if(i)for(var j=0;i>j;j++)b.push(g+"/"+h[j]);else b.push(g)}return b}function g(a){for(var b=0,c=a.length;c>b;b++)for(var d=a[b],e=d[0]+"/",f=j(d[1]),g=0,i=f.length;i>g;g++)h(e,f[g]);return r}function h(a,b){var c=a+s[0]+b.join(s[1]),d=c.length>t;if(b.length>1&&d){var e=i(b,t-(a+s[0]).length);h(a,e[0]),h(a,e[1])}else{if(d)throw new Error("The combo url is too long: "+c);for(var f=0,g=b.length;g>f;f++)r[a+b[f]]=c}}function i(a,b){for(var c=s[1],d=a[0],e=1,f=a.length;f>e;e++)if(d+=c+a[e],d.length>b)return[a.splice(0,e),a]}function j(a){for(var b=[],c={},d=0,e=a.length;e>d;d++){var f=a[d],g=k(f);g&&(c[g]||(c[g]=[])).push(f)}for(var h in c)c.hasOwnProperty(h)&&b.push(c[h]);return b}function k(a){var b=a.lastIndexOf(".");return b>=0?a.substring(b):""}function l(a){return n?n.test?n.test(a):n(a):void 0}function m(a){var b=q.comboSyntax||["??",","],c=b[0],d=b[1];return c&&a.indexOf(c)>0||d&&a.indexOf(d)>0}var n,o=seajs.Module,p=o.STATUS.FETCHING,q=seajs.data,r=q.comboHash={},s=["??",","],t=2e3;if(seajs.on("load",a),seajs.on("fetch",b),q.test){var u=seajs.test||(seajs.test={});u.uris2paths=c,u.paths2hash=g}define("seajs/seajs-combo/1.0.1/seajs-combo",[],{})}(); \ No newline at end of file +!function(){function a(a){var b=a.length;if(!(2>b)){r.comboSyntax&&(t=r.comboSyntax),r.comboMaxLength&&(u=r.comboMaxLength),r.comboSuffix&&(o=r.comboSuffix),n=r.comboExcludes;for(var d=[],e=0;b>e;e++){var f=a[e];if(!s[f]){var h=p.get(f);h.status1&&g(c(d))}}function b(a){a.requestUri=s[a.uri]||a.uri}function c(a){return e(d(a))}function d(a){for(var b={__KEYS:[]},c=0,d=a.length;d>c;c++)for(var e=a[c].replace("://","__").split("/"),f=b,g=0,h=e.length;h>g;g++){var i=e[g];f[i]||(f[i]={__KEYS:[]},f.__KEYS.push(i)),f=f[i]}return b}function e(a){for(var b=[],c=a.__KEYS,d=0,e=c.length;e>d;d++){for(var g=c[d],h=g,i=a[g],j=i.__KEYS;1===j.length;)h+="/"+j[0],i=i[j[0]],j=i.__KEYS;j.length&&b.push([h.replace("__","://"),f(i)])}return b}function f(a){for(var b=[],c=a.__KEYS,d=0,e=c.length;e>d;d++){var g=c[d],h=f(a[g]),i=h.length;if(i)for(var j=0;i>j;j++)b.push(g+"/"+h[j]);else b.push(g)}return b}function g(a){for(var b=0,c=a.length;c>b;b++)for(var d=a[b],e=d[0]+"/",f=j(d[1]),g=0,i=f.length;i>g;g++)h(e,f[g]);return s}function h(a,b){for(var c=[],d=0,e=b.length;e>d;d++)c[d]=b[d].replace(/\?.*$/,"");var f=a+t[0]+c.join(t[1]);o&&(f+=o);var g=f.length>u;if(b.length>1&&g){var j=i(b,u-(a+t[0]).length);h(a,j[0]),h(a,j[1])}else{if(g)throw new Error("The combo url is too long: "+f);for(var d=0,e=b.length;e>d;d++)s[a+b[d]]=f}}function i(a,b){for(var c=t[1],d=a[0],e=1,f=a.length;f>e;e++)if(d+=c+a[e],d.length>b)return[a.splice(0,e),a]}function j(a){for(var b=[],c={},d=0,e=a.length;e>d;d++){var f=a[d],g=k(f);g&&(c[g]||(c[g]=[])).push(f)}for(var h in c)c.hasOwnProperty(h)&&b.push(c[h]);return b}function k(a){var b=a.lastIndexOf(".");return b>=0?a.substring(b):""}function l(a){return n?n.test?n.test(a):n(a):void 0}function m(a){var b=r.comboSyntax||["??",","],c=b[0],d=b[1];return c&&a.indexOf(c)>0||d&&a.indexOf(d)>0}var n,o,p=seajs.Module,q=p.STATUS.FETCHING,r=seajs.data,s=r.comboHash={},t=["??",","],u=2e3;if(seajs.on("load",a),seajs.on("fetch",b),r.test){var v=seajs.test||(seajs.test={});v.uris2paths=c,v.paths2hash=g}define("seajs/seajs-combo/1.0.1/seajs-combo",[],{})}(); \ No newline at end of file diff --git a/src/seajs-combo.js b/src/seajs-combo.js index 270ec35..275e315 100644 --- a/src/seajs-combo.js +++ b/src/seajs-combo.js @@ -11,6 +11,7 @@ var comboHash = data.comboHash = {} var comboSyntax = ["??", ","] var comboMaxLength = 2000 var comboExcludes +var comboSuffix seajs.on("load", setComboHash) @@ -24,6 +25,7 @@ function setComboHash(uris) { data.comboSyntax && (comboSyntax = data.comboSyntax) data.comboMaxLength && (comboMaxLength = data.comboMaxLength) + data.comboSuffix && (comboSuffix = data.comboSuffix) comboExcludes = data.comboExcludes var needComboUris = [] @@ -225,7 +227,14 @@ function paths2hash(paths) { } function setHash(root, files) { - var comboPath = root + comboSyntax[0] + files.join(comboSyntax[1]) + var copy = [] + for (var i = 0, len = files.length; i < len; i++) { + copy[i] = files[i].replace(/\?.*$/, '') + } + var comboPath = root + comboSyntax[0] + copy.join(comboSyntax[1]) + if(comboSuffix) { + comboPath += comboSuffix + } var exceedMax = comboPath.length > comboMaxLength // http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url diff --git a/tests/spec/combo-map/main.js b/tests/spec/combo-map/main.js index edc0def..cfd1dd1 100644 --- a/tests/spec/combo-map/main.js +++ b/tests/spec/combo-map/main.js @@ -17,7 +17,7 @@ define(function(require) { test.assert(a.name == 'a', a.name) test.assert(b.name == 'b', b.name) - test.assert(c === null, 'return null when c is not found') + test.assert(c === void 0, 'return undefined when c is not found' + c) test.next() diff --git a/tests/spec/test.html b/tests/spec/test.html index 22897cb..12234c6 100644 --- a/tests/spec/test.html +++ b/tests/spec/test.html @@ -4,8 +4,8 @@ test - - + +
@@ -14,7 +14,7 @@ // seajs.data.preload.length = 0 seajs.use(['../test'], function(test) { - test.run(['combo', 'combo-map']) + test.run(['combo', 'combo-map', 'timestamp']) }) diff --git a/tests/spec/timestamp/a.js,b.js,c.js b/tests/spec/timestamp/a.js,b.js,c.js new file mode 100644 index 0000000..637b8f8 --- /dev/null +++ b/tests/spec/timestamp/a.js,b.js,c.js @@ -0,0 +1,2 @@ +define('a', [], { name: 'a' }) +define('b', [], { name: 'b' }) diff --git a/tests/spec/timestamp/main.js b/tests/spec/timestamp/main.js new file mode 100644 index 0000000..5e11ee1 --- /dev/null +++ b/tests/spec/timestamp/main.js @@ -0,0 +1,25 @@ +seajs.config({ + base: './timestamp/' +}) + +define(function(require) { + + var test = require('../../test') + + seajs.use('../../dist/seajs-combo', function() { + seajs.config({ + comboSyntax: ['', ','], + comboSuffix: '?1', + map: [ + [/^(.*)$/, '$1?1'] + ] + }) + seajs.use(['a', 'b', 'c'], function(a, b, c) { + test.assert(a.name == 'a', a.name) + test.assert(b.name == 'b', b.name) + test.assert(c === void 0, 'return undefined when c is not found' + c) + + test.next() + }) + }); +}); \ No newline at end of file