Comparing version 0.2.0 to 0.2.1
185
lib/layer.js
@@ -10,3 +10,3 @@ var path = require('path'); | ||
if (!layer)return; | ||
if (!layer) return; | ||
@@ -17,3 +17,3 @@ // layer 在 pagelet 里面收集的 js 不能与原来的合并。 | ||
fork.isPagelet = true; | ||
subpagelets = []; | ||
@@ -23,5 +23,5 @@ origin = fork.addPagelet; | ||
// pagelet 中套 pagelet,在父 pagelet 渲染完后再开始子 pagelet. | ||
// 缓存起来。 | ||
fork.addPagelet = function() { | ||
fork.addPagelet = function () { | ||
subpagelets.push(arguments); | ||
@@ -31,8 +31,8 @@ }; | ||
// 等待父 pagelet 渲染完毕。 | ||
pagelet.once('after', function() { | ||
subpagelets.forEach(function(args) { | ||
pagelet.once('after', function () { | ||
subpagelets.forEach(function (args) { | ||
args[0].parentId = pagelet.id; | ||
origin.apply(fork, args); | ||
}); | ||
fork.addPagelet = origin; | ||
@@ -82,9 +82,9 @@ fork = locals = origin = subpagelets = pagelet = null; | ||
css: '<% if (this.css) { %>' + | ||
'<% this.css.forEach(function(uri) { %>' + | ||
'<link rel="stylesheet" href="<%= uri %>" />' + | ||
'<% }); %>' + | ||
'<% } %>'+ | ||
'<% this.css.forEach(function(uri) { %>' + | ||
'<link rel="stylesheet" href="<%= uri %>" />' + | ||
'<% }); %>' + | ||
'<% } %>' + | ||
'<% if (this.embedCss) { %>' + | ||
'<style type="text/css"><%= this.embedCss %></style>' + | ||
'<style type="text/css"><%= this.embedCss %></style>' + | ||
'<% } %>', | ||
@@ -94,17 +94,17 @@ | ||
js: '<% if (this.framework) { %>' + | ||
'<script type="text/javascript" src="<%= this.framework %>"></script>' + | ||
'<script type="text/javascript" src="<%= this.framework %>"></script>' + | ||
'<% } %>' + | ||
'<% if (this.sourceMap) { %>' + | ||
'<script type="text/javascript">require.resourceMap(<%= this.sourceMap %>);</script>' + | ||
'<script type="text/javascript">require.resourceMap(<%= this.sourceMap %>);</script>' + | ||
'<% } %>' + | ||
'<% if (this.js) { %>' + | ||
'<% this.js.forEach(function(uri) { %>' + | ||
'<script type="text/javascript" src="<%= uri %>"></script>' + | ||
'<% }); %>' + | ||
'<% this.js.forEach(function(uri) { %>' + | ||
'<script type="text/javascript" src="<%= uri %>"></script>' + | ||
'<% }); %>' + | ||
'<% } %>' + | ||
'<% if (this.embedJs) { %>' + | ||
'<script type="text/javascript"><%= this.embedJs %></script>' + | ||
'<script type="text/javascript"><%= this.embedJs %></script>' + | ||
'<% } %>' | ||
@@ -114,3 +114,3 @@ } | ||
var createHanlder = module.exports = function(res, options) { | ||
var createHanlder = module.exports = function (res, options) { | ||
@@ -146,7 +146,5 @@ options = _.mixin(_.mixin({}, defaultOptions), options); | ||
var addedAsync = { | ||
}; | ||
var addedAsync = {}; | ||
var addedSync = { | ||
}; | ||
var addedSync = {}; | ||
@@ -162,3 +160,3 @@ var jsList = []; | ||
var usedPkg = {}; | ||
var usedSync= {}; | ||
var usedSync = {}; | ||
@@ -180,3 +178,3 @@ /** | ||
var dep = fis.getInfo(depId, true); | ||
if (!dep || (dep.type !== 'js' && dep.type !== 'css')){ | ||
if (!dep || (dep.type !== 'js' && dep.type !== 'css')) { | ||
return; | ||
@@ -209,3 +207,3 @@ } | ||
var dep = fis.getInfo(depId, true); | ||
if (!dep || (dep.type !== 'js' && dep.type !== 'css')){ | ||
if (!dep || (dep.type !== 'js' && dep.type !== 'css')) { | ||
return; | ||
@@ -222,3 +220,3 @@ } | ||
var async = fis.getInfo(asyncId, true); | ||
if (!async || (async.type !== 'js' && async.type !== 'css')){ | ||
if (!async || (async.type !== 'js' && async.type !== 'css')) { | ||
return; | ||
@@ -235,5 +233,5 @@ } | ||
function load(id){ | ||
function load(id) { | ||
var file = fis.getInfo(id, true); | ||
if (file){ | ||
if (file) { | ||
changed = true; | ||
@@ -244,3 +242,3 @@ file.id = id; | ||
syncs = syncs.concat(depList); | ||
if (file.type === 'js' || file.type === 'css'){ | ||
if (file.type === 'js' || file.type === 'css') { | ||
syncs.push(file); | ||
@@ -253,4 +251,4 @@ } | ||
if (bigpipe && !hasEventLinstener(bigpipe, 'pagelet:render:before', | ||
beforePageletRender) ) { | ||
beforePageletRender)) { | ||
bigpipe | ||
@@ -266,3 +264,3 @@ .on('pagelet:render:before', beforePageletRender) | ||
BIGPIPE_HOOK: bigpipe ? '<!--FIS_BIGPIPE_HOOK-->' : '', | ||
/** | ||
@@ -276,3 +274,3 @@ * 添加内嵌 js | ||
getScripts: function() { | ||
getScripts: function () { | ||
return scripts; | ||
@@ -285,3 +283,3 @@ }, | ||
*/ | ||
addJs: function(url) { | ||
addJs: function (url) { | ||
changed = true; | ||
@@ -292,3 +290,4 @@ var info = fis && fis.getInfo(url, true); | ||
this.load(url); | ||
} else { | ||
} | ||
else { | ||
~jsList.indexOf(url) || jsList.push(url); | ||
@@ -302,3 +301,3 @@ } | ||
*/ | ||
getJs: function() { | ||
getJs: function () { | ||
this.preparePageResource(); | ||
@@ -316,3 +315,3 @@ return jsList; | ||
getStyles: function() { | ||
getStyles: function () { | ||
return styles; | ||
@@ -325,3 +324,3 @@ }, | ||
*/ | ||
addCss: function(url) { | ||
addCss: function (url) { | ||
changed = true; | ||
@@ -332,3 +331,4 @@ var info = fis && fis.getInfo(url, true); | ||
this.load(url); | ||
} else { | ||
} | ||
else { | ||
~cssList.indexOf(url) || cssList.push(url); | ||
@@ -342,3 +342,3 @@ } | ||
*/ | ||
getCss: function() { | ||
getCss: function () { | ||
this.preparePageResource(); | ||
@@ -352,17 +352,17 @@ return cssList; | ||
*/ | ||
setFramework: function(js) { | ||
setFramework: function (js) { | ||
framework = fis ? fis.resolve(js) : js; | ||
}, | ||
getFramework: function() { | ||
getFramework: function () { | ||
return framework; | ||
}, | ||
load: function() { | ||
load: function () { | ||
fis && load.apply(this, arguments); | ||
}, | ||
resolve: function(id) { | ||
resolve: function (id) { | ||
var resolved = fis && fis.resolve(id); | ||
if (resolved) { | ||
@@ -375,3 +375,3 @@ return path.join(views, resolved); | ||
getUrl: function(id) { | ||
getUrl: function (id) { | ||
var resolved = fis && fis.resolve(id); | ||
@@ -381,11 +381,11 @@ return resolved || id; | ||
supportBigPipe: function() { | ||
supportBigPipe: function () { | ||
return !!bigpipe; | ||
}, | ||
addPagelet: function() { | ||
addPagelet: function () { | ||
return bigpipe && bigpipe.addPagelet.apply(bigpipe, arguments); | ||
}, | ||
fork: function() { | ||
fork: function () { | ||
var forked = createHanlder(res, options); | ||
@@ -396,5 +396,37 @@ forked.parent = this; | ||
preparePageResource: function(){ | ||
addResourceToList: function (res) { | ||
var me = this; | ||
if (usedSync[res.id]) { | ||
return; | ||
} | ||
usedSync[res.id] = true; | ||
if (res.pkg) { | ||
var pkg = fis.getPkgInfo(res.pkg); | ||
if (usedPkg[res.pkg] || !pkg) { | ||
return true; | ||
} | ||
pkg.has.forEach(function (has) { | ||
usedSync[has] = true; | ||
}); | ||
usedPkg[res.pkg] = true; | ||
res = pkg; | ||
if (pkg.deps) { | ||
pkg.deps.forEach(function (dep) { | ||
var file = fis.getInfo(dep, true); | ||
if (file) { | ||
me.addResourceToList(file); | ||
} | ||
}); | ||
} | ||
} | ||
if (res.type === 'js' && res.uri !== framework) { | ||
jsList.push(res.uri); | ||
} | ||
else if (res.type === 'css') | ||
cssList.push(res.uri); | ||
}, | ||
preparePageResource: function () { | ||
//检查preparePageResource后是否还有资源修改,如果没有修改则无需优化 | ||
if (!changed){ | ||
if (!changed) { | ||
return; | ||
@@ -414,27 +446,6 @@ } | ||
depList.forEach(function (dep) { | ||
var res = dep; | ||
if (usedSync[res.id]){ | ||
return; | ||
} | ||
usedSync[res.id] = true; | ||
if (res.pkg){ | ||
var pkg = fis.getPkgInfo(res.pkg); | ||
if (usedPkg[res.pkg] || !pkg){ | ||
return true; | ||
} | ||
pkg.has.forEach(function(has){ | ||
usedSync[has] = true; | ||
}); | ||
usedPkg[res.pkg] = true; | ||
res = pkg; | ||
} | ||
if (dep.type === 'js' && res.uri !== framework) | ||
jsList.push(res.uri); | ||
else if (dep.type === 'css') | ||
cssList.push(res.uri); | ||
}); | ||
depList.forEach(this.addResourceToList.bind(this)); | ||
asyncs = asyncs.filter(function (async, index) { | ||
//剔除同步资源 | ||
if (usedSync[async.id]){ | ||
if (usedSync[async.id]) { | ||
return false; | ||
@@ -446,3 +457,3 @@ } | ||
getResourceMap: function() { | ||
getResourceMap: function () { | ||
var id, rMap, res, pkg; | ||
@@ -466,7 +477,7 @@ | ||
if (res['deps']){ | ||
if (res['deps']) { | ||
// 异步资源的deps中剔除非JS资源 | ||
var deps = res['deps'].filter(function(dep){ | ||
var deps = res['deps'].filter(function (dep) { | ||
var info = fis.getInfo(dep, true); | ||
if (info && info.type === 'js'){ | ||
if (info && info.type === 'js') { | ||
return true; | ||
@@ -476,3 +487,3 @@ } | ||
if (deps.length !== 0){ | ||
if (deps.length !== 0) { | ||
rMap['res'][id].deps = deps; | ||
@@ -497,3 +508,3 @@ } | ||
filter: function(content) { | ||
filter: function (content) { | ||
@@ -507,3 +518,3 @@ content = this.filterJs(content); | ||
filterJs: function(content) { | ||
filterJs: function (content) { | ||
@@ -516,3 +527,3 @@ this.preparePageResource(); | ||
var data = {}; | ||
var loadModjs = !!framework; | ||
@@ -529,3 +540,3 @@ | ||
scripts.length && (data.embedJs = '!function() {' + | ||
scripts.join('}();\n!function() {') + '}();'); | ||
scripts.join('}();\n!function() {') + '}();'); | ||
@@ -536,3 +547,3 @@ | ||
filterCss: function(content) { | ||
filterCss: function (content) { | ||
@@ -553,3 +564,3 @@ this.preparePageResource(); | ||
destroy: function() { | ||
destroy: function () { | ||
loaded = asyncs = syncs = scripts = styles = asyncToSync = null; | ||
@@ -565,2 +576,2 @@ this.fis = this.bigpipe = this.views = null; | ||
}; | ||
}; | ||
}; |
{ | ||
"name": "yog-view", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "An express.js middleware for optimizing the order of js\\css output, and enabling render template in bigpipe mode.", | ||
@@ -28,2 +28,2 @@ "main": "index.js", | ||
} | ||
} | ||
} |
22583
573