hexo-filter-optimize
Advanced tools
Comparing version 0.2.7 to 0.3.0
@@ -0,1 +1,3 @@ | ||
/* global hexo */ | ||
'use strict'; | ||
@@ -11,3 +13,3 @@ | ||
var priority = parseInt(filterOptimize.priority); | ||
var priority = parseInt(filterOptimize.priority, 10); | ||
if (isNaN(priority)) { | ||
@@ -14,0 +16,0 @@ priority = 10; |
@@ -7,9 +7,7 @@ const Promise = require('bluebird'); | ||
list.filter(function (b) { | ||
var file = allfiles.filter(function (path) { | ||
return b.indexOf(path) >= 0; | ||
}); | ||
list.filter(b => { | ||
var file = allfiles.filter(path => b.includes(path)); | ||
if (file.length > 0) { | ||
p = p.then(function (content) { | ||
return streamRead(route.get(file[0])).then(function (str) { | ||
p = p.then(content => { | ||
return streamRead(route.get(file[0])).then(str => { | ||
return combine(content, str, concatChar, removeComments); | ||
@@ -16,0 +14,0 @@ }); |
@@ -7,4 +7,9 @@ const Promise = require('bluebird'); | ||
// check whether `path` is in `excludes` | ||
function inExcludes(path, excludes) { | ||
return excludes.some(item => minimatch(path, item, { nocase: true })); | ||
}; | ||
// the main filter function | ||
module.exports = function (s, data) { | ||
module.exports = function() { | ||
@@ -18,5 +23,3 @@ var Jsdom = jsdom.JSDOM; | ||
var config = this.config.filter_optimize; | ||
var css = config.css; | ||
var js = config.js; | ||
var removeComments = config.remove_comments; | ||
var { remove_comments, css, js } = config; | ||
@@ -26,16 +29,4 @@ var list = route.list(); | ||
// only filter html files. | ||
var htmls = list.filter(function (path) { | ||
return minimatch(path, '**/*.html', { nocase: true }); | ||
}); | ||
var htmls = list.filter(path => minimatch(path, '**/*.html', { nocase: true })); | ||
// check whether `path` is in `excludes` | ||
var inExcludes = function (path, excludes) { | ||
for (var i = 0; i < excludes.length; i++) { | ||
if (minimatch(path, excludes[i], { nocase: true })) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
css.excludes = css.excludes || []; | ||
@@ -50,14 +41,11 @@ css.inlines = css.inlines || ['css/main.css']; | ||
if (css.enable) { | ||
var inlines = list.filter(function (path) { | ||
if (inExcludes(path, css.excludes)) { | ||
return false; | ||
} | ||
return css.inlines.indexOf(path) >= 0; | ||
var inlines = list.filter(path => { | ||
return (!inExcludes(path, css.excludes) && css.inlines.includes(path)); | ||
}); | ||
if (inlines.length > 0) { | ||
first = concator.bundleFiles(inlines, list, route, '', removeComments) | ||
.then(function (content) { | ||
first = concator.bundleFiles(inlines, list, route, '', remove_comments) | ||
.then(content => { | ||
inlineCssText = content; | ||
}).catch(function (err) { | ||
}).catch(err => { | ||
logger.log('Errors when get the inline css: ', err); | ||
@@ -75,15 +63,14 @@ }); | ||
// to avoid some unnecessary css parsing information | ||
return first.then(function () { | ||
return Promise.map(htmls, function (path) { | ||
return first.then(() => { | ||
return Promise.map(htmls, path => { | ||
// console.log('processing: ' + path); | ||
var stream = route.get(path); | ||
return streamRead(stream) | ||
.then(function (str) { | ||
.then(str => { | ||
// load jsdom from the html string | ||
var dom = new Jsdom(str, { virtualConsole: vc }); | ||
var doc = dom.window && dom.window.document; | ||
if (doc == null) { | ||
return; | ||
} | ||
if (doc == null) return; | ||
var links = Array.from(doc.querySelectorAll('link')); | ||
var links = [...doc.querySelectorAll('link')]; | ||
if (links.length <= 0) { | ||
@@ -94,32 +81,17 @@ // something other static resources, skip | ||
// console.log('processing: ' + path); | ||
var regQuery = /\?v=[\d.]*$/; | ||
var cssCode = ''; | ||
var cssList = []; | ||
var hasInlines = false; | ||
var hasDelivery = false; | ||
if (config.remove_query_string) { | ||
links.filter(function (el) { return regQuery.test(el.href); }) | ||
.forEach(function (el) { | ||
el.href = el.href.replace(regQuery, ''); | ||
}); | ||
} | ||
if (css.enable) { | ||
links | ||
.filter(function (el) { return el.rel === 'stylesheet'; }) | ||
.forEach(function (el) { | ||
.filter(el => el.rel === 'stylesheet') | ||
.forEach(el => { | ||
var href = el.href; | ||
if (inExcludes(href, css.excludes)) { | ||
return; | ||
} | ||
if (inExcludes(href, css.excludes)) return; | ||
var isCssBundle = false; | ||
if (css.inlines.filter(function (p) { | ||
return href.indexOf(p) >= 0; | ||
}).length > 0) { | ||
if (css.inlines.some(p => href.includes(p))) { | ||
hasInlines = true; | ||
} else { | ||
if (href[0] === '/' && href[1] !== '/') { | ||
if (bundleCssList.indexOf(href) < 0) { | ||
if (!bundleCssList.includes(href)) { | ||
bundleCssList.push(href); | ||
@@ -129,4 +101,3 @@ isCssBundle = true; | ||
} | ||
if (!isCssBundle && bundleCssList.indexOf(href) < 0) { | ||
cssCode += 'loadCss(\'' + href + '\');'; | ||
if (!isCssBundle && !bundleCssList.includes(href)) { | ||
cssList.push(href); | ||
@@ -140,35 +111,27 @@ } | ||
if (js.bundle || config.remove_query_string) { | ||
var scripts = Array.from(doc.querySelectorAll('script')); | ||
if (js.bundle) { | ||
var scriptText = null; | ||
scripts.forEach(function (el) { | ||
var src = el.src; | ||
if (config.remove_query_string && regQuery.test(src)) { | ||
el.src = src.replace(regQuery, ''); | ||
} | ||
if (js.bundle) { | ||
var isJsBundle = false; | ||
// skip the script block defined in the <head> | ||
if (el.parentNode !== doc.head && scriptText != null | ||
// is text script block | ||
&& !src | ||
// and has the content | ||
&& el.textContent && el.textContent.length > 0) { | ||
// record it | ||
scriptText = concator.combine(scriptText, el.textContent, | ||
';', removeComments); | ||
el.remove(); | ||
} else if (src && src[0] === '/' && src[1] !== '/') { | ||
if (bundleJsList.indexOf(src) < 0) { | ||
bundleJsList.push(src); | ||
isJsBundle = true; | ||
} | ||
[...doc.querySelectorAll('script')].forEach(el => { | ||
var src = el.src, isJsBundle = false; | ||
if (inExcludes(src, js.excludes)) return; | ||
// skip the script block defined in the <head> | ||
if (el.parentNode !== doc.head && scriptText != null | ||
// is text script block | ||
&& !src | ||
// and has the content | ||
&& el.textContent && el.textContent.length > 0) { | ||
// record it | ||
scriptText = concator.combine(scriptText, el.textContent, | ||
';', remove_comments); | ||
el.remove(); | ||
} else if (src && src[0] === '/' && src[1] !== '/') { | ||
if (!bundleJsList.includes(src)) { | ||
bundleJsList.push(src); | ||
isJsBundle = true; | ||
} | ||
if (isJsBundle || bundleJsList.indexOf(src) >= 0) { | ||
if (scriptText == null) { | ||
scriptText = ''; | ||
} | ||
el.remove(); | ||
} | ||
} | ||
if (isJsBundle || bundleJsList.includes(src)) { | ||
if (scriptText == null) scriptText = ''; | ||
el.remove(); | ||
} | ||
}); | ||
@@ -178,3 +141,2 @@ | ||
var bundleJs = doc.createElement('script'); | ||
bundleJs.type = 'text/javascript'; | ||
bundleJs.src = root + 'bundle.js'; | ||
@@ -186,3 +148,2 @@ doc.body.appendChild(bundleJs); | ||
var textScript = doc.createElement('script'); | ||
textScript.type = 'text/javascript'; | ||
textScript.textContent = scriptText; | ||
@@ -198,8 +159,7 @@ doc.body.appendChild(textScript); | ||
if (bundleCssList.length > 0) { | ||
cssCode = 'loadCss(\'' + root + 'style.css\');' + cssCode; | ||
var cssCode = 'loadCss(\'' + root + 'style.css\');' + cssList.map(href => 'loadCss(\'' + href + '\');').join(''); | ||
} | ||
// eslint-disable-next-line | ||
cssCode = "function loadCss(l){var d=document,h=d.head,s=d.createElement('link');s.rel='stylesheet';s.href=l;!function e(f){if (d.body)return f();setTimeout(function(){e(f)})}(function(){h.appendChild(s);});}" | ||
cssElement.textContent = "function loadCss(l){var d=document,h=d.head,s=d.createElement('link');s.rel='stylesheet';s.href=l;!function e(f){if (d.body)return f();setTimeout(function(){e(f)})}(function(){h.appendChild(s);});}" | ||
+ cssCode; | ||
cssElement.textContent = cssCode; | ||
doc.head.appendChild(cssElement); | ||
@@ -216,3 +176,3 @@ // add the noscript block to make sure the css will be loaded | ||
} | ||
for (var i = 0; i < cssList.length; i++) { | ||
for (let i in cssList) { | ||
c = doc.createElement('link'); | ||
@@ -255,13 +215,12 @@ c.rel = 'stylesheet'; | ||
/** | ||
* make javascript bundle file | ||
*/ | ||
}).then(function () { | ||
}).then(() => { | ||
var p; | ||
if (bundleCssList.length > 0) { | ||
p = concator | ||
.bundleFiles(bundleCssList, list, route, '', removeComments) | ||
.then(function (content) { | ||
return new Promise(function (resolve) { | ||
.bundleFiles(bundleCssList, list, route, '', remove_comments) | ||
.then(content => { | ||
return new Promise(resolve => { | ||
route.set(root + 'style.css', content); | ||
@@ -276,7 +235,7 @@ resolve(); | ||
if (bundleJsList.length > 0) { | ||
p = p.then(function () { | ||
p = p.then(() => { | ||
return concator | ||
.bundleFiles(bundleJsList, list, route, ';', removeComments) | ||
.then(function (content) { | ||
return new Promise(function (resolve) { | ||
.bundleFiles(bundleJsList, list, route, ';', remove_comments) | ||
.then(content => { | ||
return new Promise(resolve => { | ||
route.set(root + 'bundle.js', content); | ||
@@ -283,0 +242,0 @@ resolve(); |
const Promise = require('bluebird'); | ||
function readStream(stream) { | ||
return new Promise(function (resolve, reject) { | ||
return new Promise((resolve, reject) => { | ||
var data = ''; | ||
stream.on('data', function (chunk) { | ||
stream.on('data', chunk => { | ||
data += chunk.toString(); | ||
}).on('end', function () { | ||
}).on('end', () => { | ||
resolve(data); | ||
@@ -11,0 +11,0 @@ }).on('error', reject); |
{ | ||
"name": "hexo-filter-optimize", | ||
"version": "0.2.7", | ||
"version": "0.3.0", | ||
"description": "A hexo plugin that optimize the pages loading speed.", | ||
@@ -26,10 +26,10 @@ "main": "index.js", | ||
"dependencies": { | ||
"bluebird": "^3.5.5", | ||
"jsdom": "^15.1.1", | ||
"bluebird": "^3.7.2", | ||
"jsdom": "^15.2.1", | ||
"minimatch": "^3.0.4" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^6.1.0", | ||
"eslint-config-theme-next": "^1.1.0" | ||
"eslint": "^6.7.2", | ||
"eslint-config-theme-next": "^1.1.3" | ||
} | ||
} |
# hexo-filter-optimize | ||
[![npm-build]][npm-build-link] [![travis-ci]][travis-ci-link] | ||
[npm-build]: https://img.shields.io/npm/v/hexo-filter-optimize.svg?style=flat | ||
[npm-build-link]: https://www.npmjs.com/package/hexo-filter-optimize | ||
[travis-ci]: https://img.shields.io/travis/theme-next/hexo-filter-optimize/master.svg?style=flat | ||
[travis-ci-link]: https://travis-ci.org/theme-next/hexo-filter-optimize | ||
[![travis-image]][travis-url] | ||
[![npm-image]][npm-url] | ||
[![lic-image]](LICENSE) | ||
@@ -19,4 +17,8 @@ A hexo plugin that optimize the pages loading speed. | ||
![size-image] | ||
[![dm-image]][npm-url] | ||
[![dt-image]][npm-url] | ||
```bash | ||
npm install hexo-filter-optimize --save | ||
npm install hexo-filter-optimize | ||
``` | ||
@@ -30,5 +32,2 @@ | ||
enable: true | ||
# remove static resource query string | ||
# - like `?v=1.0.0` | ||
remove_query_string: true | ||
# remove the surrounding comments in each of the bundled files | ||
@@ -71,1 +70,12 @@ remove_comments: false | ||
![Comparison](https://user-images.githubusercontent.com/980449/35233293-a8229c72-ffd8-11e7-8a23-3b8bc10d40c3.png) | ||
[travis-image]: https://img.shields.io/travis/theme-next/hexo-filter-optimize/master.svg?style=flat-square | ||
[npm-image]: https://img.shields.io/npm/v/hexo-filter-optimize.svg?style=flat-square | ||
[lic-image]: https://img.shields.io/npm/l/hexo-filter-optimize?style=flat-square | ||
[size-image]: https://img.shields.io/github/languages/code-size/theme-next/hexo-filter-optimize?style=flat-square | ||
[dm-image]: https://img.shields.io/npm/dm/hexo-filter-optimize?style=flat-square | ||
[dt-image]: https://img.shields.io/npm/dt/hexo-filter-optimize?style=flat-square | ||
[travis-url]: https://travis-ci.org/theme-next/hexo-filter-optimize | ||
[npm-url]: https://www.npmjs.com/package/hexo-filter-optimize |
Sorry, the diff of this file is not supported yet
79
16042
288
Updatedbluebird@^3.7.2
Updatedjsdom@^15.2.1