lego-bigpipe
Advanced tools
Comparing version 0.1.8 to 0.2.0-1
61
index.js
@@ -84,8 +84,9 @@ 'use strict'; | ||
if (data.body) { | ||
var ids = {}; | ||
var ids = {css: {}, js: {}}; | ||
each(data.body.units, function (unit) { | ||
each(unit.css, function (m) {ids[m + '.css.js'] = 1;}); | ||
if (data.mode === 'inline') each(unit.js, function (m) {ids[m + '.js'] = 1;}); | ||
each(unit.css, function (m) {ids.css[m + '.css.js'] = 1;}); | ||
if (data.mode === 'inline') each(unit.js, function (m) {ids.js[m + '.js'] = 1;}); | ||
}); | ||
ids = Object.keys(ids); | ||
ids.css = Object.keys(ids.css); | ||
ids.js = Object.keys(ids.js); | ||
@@ -95,21 +96,22 @@ // inline mode: put mods' content in head | ||
var mods = data.mods || {}; | ||
var contents = []; | ||
for (var i = 0; i < ids.length; i++) { | ||
var id = ids[i]; | ||
for (var i = 0; i < ids.css.length; i++) { | ||
var id = ids.css[i]; | ||
if (mods[id]) { | ||
contents.push(mods[id]); | ||
ids.splice(i--, 1); | ||
pre += tpl.script({content: mods[id]}); | ||
ids.css.splice(i--, 1); | ||
} | ||
} | ||
if (contents.length) pre += tpl.script({content: contents.join('\n ')}); | ||
each(ids.js, function (id) { | ||
if (mods[id]) pre += tpl.script({content: mods[id]}); | ||
}); | ||
} | ||
// combo mode: put mods' url of css combo in head | ||
if (config.combo && ids.length) { | ||
pre += tpl.scriptLink({ | ||
url: genUrl(ids, config) | ||
}); | ||
if (config.combo && ids.css.length) { | ||
pre += genUrl(ids.css, config).reduce(function (scripts, url) { | ||
return scripts += tpl.scriptLink({url: url}); | ||
}, ''); | ||
// put mods' url of css in head | ||
} else { | ||
each(ids, function (id) { | ||
each(ids.css, function (id) { | ||
pre += tpl.scriptLink({ | ||
@@ -180,8 +182,29 @@ url: genUrl(id, config) | ||
function genUrl(ids, config) { | ||
if (type(ids) === 'string') ids = [ids]; | ||
config = config || {}; | ||
var urlPattern = config.urlPattern || '%s'; | ||
if (type(ids) === 'string') return urlPattern.replace('%s', ids); | ||
else ids = ids.slice(); | ||
var url = ids.length > 1 && config.comboPattern || config.urlPattern; | ||
if (url) url = url.replace('%s', ids.join(';')); | ||
return url; | ||
urlPattern = config.comboPattern || urlPattern; | ||
var MAX_URL_LENGTH = 2000; | ||
var urls = []; | ||
var url = ids.shift(); | ||
var l = urlPattern.length - 2 + url.length; | ||
while (ids.length) { | ||
var id = ids.shift(); | ||
if (l + id.length < MAX_URL_LENGTH) { | ||
url += ';' + id; | ||
l += 1 + id.length; | ||
} else { | ||
urls.push(url); | ||
url = id; | ||
l = urlPattern.length - 2 + url.length; | ||
} | ||
} | ||
urls.push(url); | ||
return urls.map(function (url) { | ||
return urlPattern.replace('%s', url); | ||
}); | ||
} | ||
@@ -188,0 +211,0 @@ |
{ | ||
"name": "lego-bigpipe", | ||
"version": "0.1.8", | ||
"version": "0.2.0-1", | ||
"description": "a bigpipe builder for scrat-lego", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -213,4 +213,4 @@ // (╯°□°)╯︵oƃǝ˥ | ||
}); | ||
v.should.have.property('pre', '<!DOCTYPE html>\n<!--STATUS OK-->\n<html>\n<head>\n <meta charset="utf-8">\n <script>lego.defineCSS("u/u1/u11.css.js", "html { display: none; }");\n lego.defineCSS("u/u1/u12.css.js", "html { display: block; }");\n lego.define("u/u1/u11.js", "console.log("Hello");");\n lego.defineCSS("u/u2/u21.css.js", "body { display: none; }");</script>\n</head>\n<body>\n'); | ||
v.should.have.property('pre', '<!DOCTYPE html>\n<!--STATUS OK-->\n<html>\n<head>\n <meta charset="utf-8">\n <script>lego.defineCSS("u/u1/u11.css.js", "html { display: none; }");</script>\n <script>lego.defineCSS("u/u1/u12.css.js", "html { display: block; }");</script>\n <script>lego.defineCSS("u/u2/u21.css.js", "body { display: none; }");</script>\n <script>lego.define("u/u1/u11.js", "console.log("Hello");");</script>\n</head>\n<body>\n'); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14465
386