scrat-swig
Advanced tools
Comparing version
@@ -0,0 +0,0 @@ 'use strict'; |
1.1.0-pwa1 / 2017-12-26 | ||
================== | ||
* feat: support PWA (#16) | ||
1.0.0 / 2017-06-06 | ||
@@ -3,0 +8,0 @@ ================== |
@@ -12,2 +12,4 @@ 'use strict'; | ||
exports.Resource = Resource; | ||
exports.setServiceWorkerRegistrationJS = Resource.setServiceWorkerRegistrationJS; | ||
exports.setCombo = Resource.setCombo; | ||
exports.filters = exports.filters || filters; | ||
@@ -50,1 +52,4 @@ | ||
}; | ||
// for unittest | ||
exports.cleanOptions = Resource.cleanOptions; |
@@ -0,0 +0,0 @@ 'use strict'; |
@@ -7,2 +7,3 @@ 'use strict'; | ||
const PREFIX = 'component'; | ||
const cache = {}; | ||
let options = null; | ||
@@ -12,3 +13,5 @@ let logger = console; | ||
let domain = ''; | ||
const cache = {}; | ||
// auto include service-worker registration script when enable PWA mode. | ||
let serviceWorkerRegistrationJS = ''; | ||
let useCombo = false; | ||
@@ -47,3 +50,8 @@ /** | ||
this._useATF = false; | ||
options = Resource.loadOptions(opt); | ||
if (!options || !options.res) { | ||
options = Resource.loadOptions(opt); | ||
// if(!options.usePWA && options.combo !== undefined) useCombo = options.combo; | ||
if (options.combo !== undefined) useCombo = options.combo; | ||
} | ||
} | ||
@@ -67,2 +75,7 @@ | ||
// 单元测试模式下,同一个进程时会加载很多次不同的map,但实际上正常的项目只会有一个map | ||
Resource.cleanOptions = function() { | ||
options = null; | ||
}; | ||
Resource.setLogger = function(instance) { | ||
@@ -79,2 +92,11 @@ logger = instance; | ||
Resource.setServiceWorkerRegistrationJS = js => { | ||
serviceWorkerRegistrationJS = js; | ||
}; | ||
// 运行时动态设置combo开关 | ||
Resource.setCombo = combo => { | ||
useCombo = combo; | ||
}; | ||
/** | ||
@@ -153,2 +175,23 @@ * script标签占位 | ||
/** | ||
* 处理uri,当在运行时从combo模式切换到非combo模式下,替换静态资源serving地址,默认是和主应用同域名 | ||
* @param {Array} collect 资源集合 | ||
* @param {String} uri 资源uri | ||
*/ | ||
function pushUri(collect, uri) { | ||
const escapeRegExp = function(string) { | ||
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
}; | ||
const stripKeys = Object.keys(options.stripPrefixMulti || {}); | ||
if (!useCombo && stripKeys.length) { | ||
uri = uri.replace( | ||
new RegExp('^(' + stripKeys.map(escapeRegExp).join('|') + ')'), | ||
function(m) { return options.stripPrefixMulti[m]; } | ||
); | ||
} | ||
collect.push(uri); | ||
} | ||
/** | ||
* 收集资源 | ||
@@ -182,3 +225,3 @@ * @param {String} id 资源ID | ||
} | ||
_collect[res.type].push(res.uri); | ||
pushUri(_collect[res.type], res.uri); | ||
return true; | ||
@@ -238,3 +281,3 @@ } | ||
collect.push(res.uri); | ||
pushUri(collect, res.uri); | ||
} else { | ||
@@ -280,4 +323,9 @@ logger.error('unable to load resource [' + file + ']'); | ||
// fixbug | ||
Resource.prototype.getComboPattern = function() { | ||
return domain + (options.comboPattern || DEFAULT_COMBO_PATTERN); | ||
const comboPattern = options.comboPattern || DEFAULT_COMBO_PATTERN; | ||
return /^(https?:|)\/\//.test(comboPattern) ? comboPattern : domain + comboPattern; | ||
// return domain + (options.comboPattern || DEFAULT_COMBO_PATTERN); | ||
}; | ||
@@ -303,3 +351,3 @@ | ||
const right = '"></script>\n'; | ||
if (options.combo) { | ||
if (useCombo) { | ||
const pack = this.genComboURI(this._collect.js, options.comboMaxLen); | ||
@@ -320,3 +368,3 @@ pack.forEach(function(res) { | ||
} | ||
if (options.combo && used.length) { | ||
if (useCombo && used.length) { | ||
for (let i = 0, len = used.length; i < len; i++) { | ||
@@ -326,4 +374,9 @@ used[i] = this.comboURI(used[i]); | ||
} | ||
if (serviceWorkerRegistrationJS && options.usePWA) { | ||
// if(serviceWorkerRegistrationJS && !useCombo) { | ||
html += `<script>${serviceWorkerRegistrationJS}</script>`; | ||
} | ||
const args = [ | ||
options.combo ? 1 : 0, | ||
useCombo ? 1 : 0, | ||
'"' + this.getComboPattern() + '"', | ||
@@ -334,5 +387,7 @@ '["' + used.join('","') + '"]', | ||
const code = 'pagelet&&pagelet.init&&pagelet.init(' + args.join(',') + ');'; | ||
html += '<script>' + code + '</script>\n'; | ||
if (this._script.length) { | ||
if (options.combo) { | ||
if (useCombo) { | ||
html += '<script>' + join(this._script, '!function(){', '}();') + '</script>\n'; | ||
@@ -343,2 +398,3 @@ } else { | ||
} | ||
return html; | ||
@@ -352,3 +408,3 @@ }; | ||
const right = '"' + (defer ? ' data-defer' : '') + '>\n'; | ||
if (options.combo) { | ||
if (useCombo) { | ||
const pack = this.genComboURI(collect, options.comboMaxLen); | ||
@@ -378,3 +434,3 @@ pack.forEach(function(res) { | ||
let fn; | ||
if (options.combo) { | ||
if (useCombo) { | ||
fn = function(uri) { | ||
@@ -410,3 +466,3 @@ let content = ''; | ||
if (options.combo) { | ||
if (useCombo) { | ||
html = '<style>' + html + '</style>'; | ||
@@ -446,3 +502,3 @@ } | ||
if (options.combo && js.length) { | ||
if (useCombo && js.length) { | ||
js.forEach(function(uri, index) { | ||
@@ -452,3 +508,3 @@ js[index] = self.comboURI(uri); | ||
} | ||
if (options.combo && css.length) { | ||
if (useCombo && css.length) { | ||
css.forEach(function(uri, index) { | ||
@@ -455,0 +511,0 @@ css[index] = self.comboURI(uri); |
@@ -0,0 +0,0 @@ 'use strict'; |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ 'use strict'; |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ 'use strict'; |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ /** |
{ | ||
"name": "scrat-swig", | ||
"version": "1.0.0", | ||
"version": "1.1.0-pwa2", | ||
"description": "Extensions of swig for scrat", | ||
@@ -9,3 +9,3 @@ "main": "index.js", | ||
"test": "npm run lint -- --fix && npm run test-local", | ||
"test-local": "egg-bin test", | ||
"test-local": "TEST_MODE=unittest egg-bin test", | ||
"cov": "egg-bin cov", | ||
@@ -12,0 +12,0 @@ "ci": "npm run lint && npm run cov", |
@@ -0,0 +0,0 @@ # [scrat](https://github.com/scrat-team/scrat) 后端渲染解决方案的模板扩展 |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
45958
0.33%1115
4.4%0
-100%21
-4.55%1
Infinity%