node-cube
Advanced tools
Comparing version 3.0.0-beta.15 to 3.0.0-beta.16
'use strict'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const fs = require('xfs'); | ||
const async = require('async'); | ||
@@ -12,5 +13,14 @@ | ||
class Cache { | ||
constructor() { | ||
constructor(options) { | ||
this.tmpDir = options.tmpDir; | ||
/** | ||
* cacheKey 索引cache内容 | ||
*/ | ||
this._cache = {}; | ||
/** | ||
* moduleName 索引 cacheKey | ||
*/ | ||
this._modulemap = {}; | ||
this.initFromDisk(); | ||
} | ||
@@ -23,3 +33,32 @@ get(key) { | ||
this._modulemap[v.queryPath] = key; | ||
this.saveToDisk(key, v); | ||
} | ||
saveToDisk(key, v) { | ||
fs.save(path.join(this.tmpDir, key), JSON.stringify(v), () => {}); | ||
} | ||
initFromDisk() { | ||
fs.walk(this.tmpDir, (err, file, done) => { | ||
if (err) { | ||
console.error(err); | ||
} | ||
fs.readFile(file, (err, fdata) => { | ||
if (err) { | ||
console.error(err); | ||
} else { | ||
let data = JSON.parse(fdata); | ||
let key = file.substr(this.tmpDir.length); | ||
if (!this._cache[key]) { | ||
this._cache[key] = data; | ||
this._modulemap[data.queryPath] = key; | ||
} | ||
} | ||
done(); | ||
}); | ||
}, function () { | ||
console.log('[CUBE] init cache from disk'); | ||
}); | ||
} | ||
keys() { | ||
return Object.keys(this._modulemap); | ||
} | ||
getNodeModules(process, done) { | ||
@@ -30,2 +69,8 @@ let modules = this._cache; | ||
/** | ||
* 规则: | ||
* 1. 检查自己是否过期,过期丢弃 | ||
* 2. 检查自己的依赖是否存在缓存,不存在就占位 | ||
* 3. 清理自己的所有依赖 | ||
*/ | ||
async.eachLimit(mods, 10, (key, cb) => { | ||
@@ -53,3 +98,3 @@ let node = modules[key]; | ||
if (!modulesMap[v]) { | ||
process(placeHolder(node.queryPath)); | ||
process(placeHolder(v)); | ||
} | ||
@@ -56,0 +101,0 @@ }); |
@@ -168,3 +168,3 @@ 'use strict'; | ||
this.caches = new Cache(); | ||
this.caches = new Cache({tmpDir: path.join(config.root, './.cubecache')}); | ||
/** | ||
@@ -171,0 +171,0 @@ * 文件后缀名-类型映射表 |
@@ -5,3 +5,3 @@ { | ||
"description": "a new way to write js in browser", | ||
"version": "3.0.0-beta.15", | ||
"version": "3.0.0-beta.16", | ||
"homepage": "https://github.com/fishbar/cube", | ||
@@ -8,0 +8,0 @@ "repository": { |
/*! | ||
* Cube v3.0.0-beta.15 | ||
* Cube v3.0.0-beta.16 | ||
*/ | ||
(function(a,b){function c(){}function d(a,b){if(1===arguments.length)return j(a);var c=j(a);l.css(c,b,a)}function e(a,b,c){2===arguments.length&&'function'==typeof b?(c=b,b=null,l.use(a,c)):l.use(a,function(d){d=l.css(d,b,a),c&&c(d)})}function f(a){var b=a.indexOf(s);return 0<b?q[a.substr(0,b)]+a.substr(b+1):''}function g(a){for(var b,c=a.length,d=0;d<c;d++)b=a[d],-1===b.indexOf(s)&&(0===b.indexOf('./')?a[d]=b.substr(1):'/'!==b[0]&&(a[d]='/'+b));return a}function h(){if(B.length)return!1;for(var a in y)if(y.hasOwnProperty(a))return!1;return!0}function i(a,b){return'string'==typeof a&&(a=[a]),A?void(a.forEach(function(a){if(!x[a]){var c=o.createElement('script');c.type='text/javascript',c.async='true',c.charset=t;var d=f(a),e=d||r+a,g=[];m&&g.push(m),v&&(g.push('m'),g.push('ref='+b)),c.src=g.length?e+'?'+g.join('&'):e,z.appendChild(c),x[a]={exports:{},loaded:!1,fired:!1},y[a]=!0}}),h()&&k()):void B.push([a,b])}function j(b){function c(){var c=x[b];if(!c)throw new Error('Cube Error: Cannot find module \''+b+'\'');return c.fired||(c.fired=!0,c.exports=c.fn.apply(a,[c,c.exports,d,e])),c.exports}if(u)return c();try{return c()}catch(a){return p.error(a),{}}}function k(){var a,b;for(a in w)w.hasOwnProperty(a)&&(b=a.split(','),b.forEach(function(b){var c=0;j(b),w[a].forEach(function(a){var d=a(x[b].exports);d&&c++}),w[a].length===c&&delete w[a]}))}function l(a,b,c){x[a]||(x[a]={exports:{},loaded:!1,fired:!1});var d=x[a];d.fn=c,d.loaded=!0,delete y[a],i(b,a)}var m,n=window,o=document,p=console,r='',q={},s=':',t='utf-8',u=!0,v=!0,w={},x={},y={},z=o.querySelector('head'),A=!1,B=[];l.toString=function(){return'Cube:v3.0.0-beta.15'},l.init=function(a){if(a.base&&'/'!==a.base&&(r=a.base.replace(/\/$/,'')),a.remoteBase)for(var b in a.remoteBase)a.remoteBase.hasOwnProperty(b)&&(q[b]=a.remoteBase[b].replace(/\/$/,''));for(a.charset&&(t=a.charset),a.version&&(m=a.version),void 0!==a.debug&&(v=a.debug),void 0!==a.strict&&(u=a.strict),A=!0;B.length;){var c=B.shift();i(c[0],c[1])}return this},l.use=function(b,d,e){if(!b)throw new Error('Cube.use(moduleName) moduleName is undefined!');return d=d||c,'string'==typeof b&&(b=[b]),e||(b=g(b)),w[b]||(w[b]=[]),w[b].push(function(){var c=[],e=b.length,f=!1;return function(b){if(!f)return(c.push(b),c.length===e)?(f=!0,d.apply(a,c),!0):void 0}}()),i(b,'Cube.use'),this},l.register=function(a,b){return x[a]?p.error('Cube Error: Module \''+a+'\' already registered'):(x[a]={exports:b,fn:c,loaded:!0,fired:!0},this)};var C=/([^};]+)(\{[^}]+\})/g,D={};l.css=function(a,b,c){if(a){var d=c+'@'+b;if(!D[d]){D[d]=!0,b&&(a=a.replace(C,function(a,c,d){var e=c.split(',').map(function(a){return b+' '+a.trim()});return e.join(',')+d}));var e=o.createElement('style');return e.setAttribute('type','text/css'),e.setAttribute('mod',c),b&&e.setAttribute('ns',b),z.appendChild(e),e.innerHTML=a,a}}},l.debug=function(){n.localStorage&&n.addEventListener?(localStorage.cube='debug',location.reload()):p.error('Cube Error: Cannot debug, your browser does not support localStorage or addEventListener')},l.cache=function(){var a,b,c={},d={};for(a in x)x.hasOwnProperty(a)&&(b=x[a],b.loaded||(c[a]=b),b.fired||(d[a]=b));p.info('modules:',x),p.info('unloaded:',c),p.info('unfired:',d)},n.localStorage&&'debug'===localStorage.cube&&(v=!0,n.addEventListener('load',l.cache)),b=b||'Cube',a[b]?p.error('Cube Error: window.'+b+' already in using, replace the last "null" param in cube.js'):a[b]=l;var E=o.currentScript;if(E){var F=E.dataset;F.base&&(l.init(F),l.use(F.main||'index.js',function(a){a.run&&a.run()}))}})(window,null); | ||
(function(a,b){function c(){}function d(a,b){if(1===arguments.length)return j(a);var c=j(a);l.css(c,b,a)}function e(a,b,c){2===arguments.length&&'function'==typeof b?(c=b,b=null,l.use(a,c)):l.use(a,function(d){d=l.css(d,b,a),c&&c(d)})}function f(a){var b=a.indexOf(s);return 0<b?q[a.substr(0,b)]+a.substr(b+1):''}function g(a){for(var b,c=a.length,d=0;d<c;d++)b=a[d],-1===b.indexOf(s)&&(0===b.indexOf('./')?a[d]=b.substr(1):'/'!==b[0]&&(a[d]='/'+b));return a}function h(){if(B.length)return!1;for(var a in y)if(y.hasOwnProperty(a))return!1;return!0}function i(a,b){return'string'==typeof a&&(a=[a]),A?void(a.forEach(function(a){if(!x[a]){var c=o.createElement('script');c.type='text/javascript',c.async='true',c.charset=t;var d=f(a),e=d||r+a,g=[];m&&g.push(m),v&&(g.push('m'),g.push('ref='+b)),c.src=g.length?e+'?'+g.join('&'):e,z.appendChild(c),x[a]={exports:{},loaded:!1,fired:!1},y[a]=!0}}),h()&&k()):void B.push([a,b])}function j(b){function c(){var c=x[b];if(!c)throw new Error('Cube Error: Cannot find module \''+b+'\'');return c.fired||(c.fired=!0,c.exports=c.fn.apply(a,[c,c.exports,d,e])),c.exports}if(u)return c();try{return c()}catch(a){return p.error(a),{}}}function k(){var a,b;for(a in w)w.hasOwnProperty(a)&&(b=a.split(','),b.forEach(function(b){var c=0;j(b),w[a].forEach(function(a){var d=a(x[b].exports);d&&c++}),w[a].length===c&&delete w[a]}))}function l(a,b,c){x[a]||(x[a]={exports:{},loaded:!1,fired:!1});var d=x[a];d.fn=c,d.loaded=!0,delete y[a],i(b,a)}var m,n=window,o=document,p=console,r='',q={},s=':',t='utf-8',u=!0,v=!0,w={},x={},y={},z=o.querySelector('head'),A=!1,B=[];l.toString=function(){return'Cube:v3.0.0-beta.16'},l.init=function(a){if(a.base&&'/'!==a.base&&(r=a.base.replace(/\/$/,'')),a.remoteBase)for(var b in a.remoteBase)a.remoteBase.hasOwnProperty(b)&&(q[b]=a.remoteBase[b].replace(/\/$/,''));for(a.charset&&(t=a.charset),a.version&&(m=a.version),void 0!==a.debug&&(v=a.debug),void 0!==a.strict&&(u=a.strict),A=!0;B.length;){var c=B.shift();i(c[0],c[1])}return this},l.use=function(b,d,e){if(!b)throw new Error('Cube.use(moduleName) moduleName is undefined!');return d=d||c,'string'==typeof b&&(b=[b]),e||(b=g(b)),w[b]||(w[b]=[]),w[b].push(function(){var c=[],e=b.length,f=!1;return function(b){if(!f)return(c.push(b),c.length===e)?(f=!0,d.apply(a,c),!0):void 0}}()),i(b,'Cube.use'),this},l.register=function(a,b){return x[a]?p.error('Cube Error: Module \''+a+'\' already registered'):(x[a]={exports:b,fn:c,loaded:!0,fired:!0},this)};var C=/([^};]+)(\{[^}]+\})/g,D={};l.css=function(a,b,c){if(a){var d=c+'@'+b;if(!D[d]){D[d]=!0,b&&(a=a.replace(C,function(a,c,d){var e=c.split(',').map(function(a){return b+' '+a.trim()});return e.join(',')+d}));var e=o.createElement('style');return e.setAttribute('type','text/css'),e.setAttribute('mod',c),b&&e.setAttribute('ns',b),z.appendChild(e),e.innerHTML=a,a}}},l.debug=function(){n.localStorage&&n.addEventListener?(localStorage.cube='debug',location.reload()):p.error('Cube Error: Cannot debug, your browser does not support localStorage or addEventListener')},l.cache=function(){var a,b,c={},d={};for(a in x)x.hasOwnProperty(a)&&(b=x[a],b.loaded||(c[a]=b),b.fired||(d[a]=b));p.info('modules:',x),p.info('unloaded:',c),p.info('unfired:',d)},n.localStorage&&'debug'===localStorage.cube&&(v=!0,n.addEventListener('load',l.cache)),b=b||'Cube',a[b]?p.error('Cube Error: window.'+b+' already in using, replace the last "null" param in cube.js'):a[b]=l;var E=o.currentScript;if(E){var F=E.dataset;F.base&&(l.init(F),l.use(F.main||'index.js',function(a){a.run&&a.run()}))}})(window,null); |
@@ -211,24 +211,2 @@ /*! | ||
/* | ||
if (flagWrap && !result.merged && !/\/node_modules\//.test(result.queryPath)) { | ||
// 级联合并, 目前只合并 node_modules中的文件 | ||
let depsMods = []; | ||
let parent = [result.queryPath]; | ||
mergeRequire(cube, result, depsMods, parent, function () { | ||
let map = {}; | ||
let codesDeps = []; | ||
depsMods.forEach(function (data) { | ||
if (map[data.queryPath]) { | ||
return; | ||
} | ||
map[data.queryPath] = true; | ||
codesDeps.push(flagWrap ? data.codeWraped : data.code); | ||
}); | ||
code = (codesDeps.length ? codesDeps.join('\n') + '\n' : '') + code; | ||
output(); | ||
}); | ||
} else { | ||
output(); | ||
} | ||
*/ | ||
output(); | ||
@@ -252,12 +230,15 @@ | ||
'console.error("[CUBE]",' + | ||
(e.code ? '"'+ e.code.replace(/"/g, '\\"') + ': ' + e.message.replace(/"/g, '\\"') + '",' : e.message.replace(/"/g, '\\"')) + | ||
(e.file ? '"[File]: ' + e.file + '",' : '') + | ||
(e.line ? '"[Line]: ' + e.line + '",' : '') + | ||
(e.column ? '"[Column]: ' + e.column + '"' : '') + | ||
JSON.stringify( | ||
(e.code || e.name || 'UNKNOW_ERROR') + ':' + e.message + | ||
' [File]:' + e.file + | ||
' [Line]:' + (e.loc ? e.loc.line : e.line) + | ||
' [Column]: ' + (e.loc ? e.loc.column : e.column) | ||
) + | ||
');' : | ||
'[CUBE]\n' + | ||
(e.code ? e.code + ': ' + e.message + '\n' : e.message) + | ||
(e.file ? 'File: ' + e.file + '\n' : '') + | ||
(e.line ? 'Line: ' + e.line + '\n' : '') + | ||
(e.column ? 'Column: ' + e.column + '\n' : ''); | ||
(e.code || e.name || 'UNKNOW_ERROR') + e.message + | ||
' [File]:' + e.file + | ||
' [Line]:' + (e.loc ? e.loc.line : e.line) + | ||
' [Column]: ' + (e.loc ? e.loc.column : e.column); | ||
res.end(msg); | ||
@@ -264,0 +245,0 @@ } |
357917
5345
16