Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Commit

Permalink
feat: add hash module
Browse files Browse the repository at this point in the history
fix #83
  • Loading branch information
xiazeyu committed Feb 9, 2018
1 parent 80d348b commit e8d24c5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ live2d:
live2d:
enable: true
jsPath: local # 'local'(1)||'jsdelivr'(2)||'unpkg'(3)||{Your own path, String}(4)
hashLevel: soft # 'soft'(1)||'dep'(2)||'none'(3)
model:
use: live2d-widget-model-miku # {npm-module name}(1)||{folder name in live2d_models/}(2)||{Your own path, String}(3)
```
Expand Down
40 changes: 33 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ const fs = require('hexo-fs'),
path = require('path'),
url = require('url'),
_ = require('lodash'),
crypto = require('crypto'),
UglifyJS = require("uglify-js"),
onSiteRootPath = '/live2dw/'
onSiteJsPath = onSiteRootPath + 'lib/',
onSiteModelPath = onSiteRootPath + 'assets/',
pkgInfo = require('./package'),
coreJsName = 'L2Dwidget.min.js',
coreJsList = require('live2d-widget/lib/manifest'),
coreJsPath = path.dirname(require.resolve('live2d-widget/lib/manifest')),
coreJssPath = path.dirname(require.resolve('live2d-widget/lib/manifest')),
coreJsDepVer = pkgInfo.dependencies['live2d-widget'],
defaultConfig = require('live2d-widget/src/config/defaultConfig');

Expand Down Expand Up @@ -76,37 +77,62 @@ function localModelProcessor(localFolder, siteDir = onSiteModelPath){

function localJsProcessor(){
for(let f of Object.keys(coreJsList)){
addFile(url.resolve(onSiteJsPath, coreJsList[f]), path.resolve(coreJsPath, coreJsList[f]));
addFile(url.resolve(onSiteJsPath, coreJsList[f]), path.resolve(coreJssPath, coreJsList[f]));
}
return url.resolve(onSiteJsPath, coreJsName);
}

function getCoreJsMD5(){
const coreJs = path.resolve(coreJssPath, coreJsName),
rs = fs.readFileSync(coreJs),
hash = crypto.createHash('md5');
return (hash.update(rs).digest('hex'));
}

function getJsPath(){
let useHash;
if(_.hasIn(config, 'hashLevel')){
switch(config.hashLevel){
case 'soft':
useHash = `?${getCoreJsMD5()}`;
break;
case 'dep':
useHash = `?${coreJsDepVer}`;
break;
case 'none':
useHash = '';
break;
default:
useHash = `?${getCoreJsMD5()}`;
}
}else{
useHash = `?${getCoreJsMD5()}`;
}
if(_.hasIn(config, 'jsPath')){
// a. have user modified config.jsPath
switch(config.jsPath){
case 'local':
// a.1 is local
// use local(1)
return localJsProcessor();
return localJsProcessor() + useHash;
case 'jsdelivr':
// a.2 is jsdelivr online CDN
// use jsdelivr(2)
return `https://cdn.jsdelivr.net/npm/live2d-widget@${coreJsDepVer}/lib/${coreJsName}`;
return `https://cdn.jsdelivr.net/npm/live2d-widget@${coreJsDepVer}/lib/${coreJsName}${useHash}`;
case 'unpkg':
// a.3 is unpkg online CDN
// use unpkg(3)
return `https://unpkg.com/live2d-widget@${coreJsDepVer}/lib/${coreJsName}`;
return `https://unpkg.com/live2d-widget@${coreJsDepVer}/lib/${coreJsName}${useHash}`;
default:
// a.4 is custom url or path, etc.
// use custom(4), let it go~
return config.jsPath;
return config.jsPath + useHash;
}
_.unset(config, 'jsPath');
}else{
// b. don't have user modified config.jsPath
// use local(1)
return localJsProcessor();
return localJsProcessor() + useHash;
}
}

Expand Down

0 comments on commit e8d24c5

Please sign in to comment.