node-cube
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -0,1 +1,4 @@ | ||
v1.0.1 | ||
* .cubeignre support ignore patten in dev model | ||
v0.1.0 | ||
@@ -2,0 +5,0 @@ * support custom processor now |
13
index.js
@@ -40,4 +40,4 @@ /*! | ||
debugRegister(cube, 'cube-coffee'); | ||
debugRegister(cube, 'cube-jsx'); | ||
} | ||
/** | ||
@@ -116,2 +116,5 @@ * [Cube description] | ||
} | ||
// load ignore | ||
this.ignoresRules = utils.loadIgnore(config.root); | ||
} | ||
@@ -150,2 +153,10 @@ /** | ||
}; | ||
Cube.prototype.checkIgnore = function (absPath) { | ||
// console.log(absPath.substr(this.config.root.length), this.ignoresRules); | ||
var res = utils.checkIgnore(absPath.substr(this.config.root.length), this.ignoresRules); | ||
// console.log(res); | ||
return res.ignore; | ||
}; | ||
/** | ||
@@ -152,0 +163,0 @@ * register a processor for cube |
@@ -33,14 +33,14 @@ /*! | ||
var fName = path.basename(modPath, origExt); | ||
origExt = origExt || '.js'; | ||
debug('tryFiles: %s, ext: %s', modPath, origExt); | ||
if (!cube.processors.map[origExt]) { | ||
var dir = path.dirname(modPath); | ||
var arr = fs.readdirSync(dir); | ||
var type = cube.processors.map[origExt || '.js']; | ||
if (!type) { | ||
// may be filename like `require('./abc.dot')` | ||
// if unknow type, switch to script type | ||
type = 'script'; | ||
fName = path.basename(modPath); | ||
origExt = '.js'; | ||
} | ||
var dir = path.dirname(modPath); | ||
var arr = fs.readdirSync(dir); | ||
var type = cube.processors.map[origExt]; | ||
var possibleExts = Object.keys(cube.processors.types[type]); | ||
var targetExt = utils.traverseFList(arr, fName, possibleExts); | ||
var targetExt = utils.getPossibleExt(arr, fName, possibleExts); | ||
if (targetExt instanceof Error) { | ||
@@ -50,4 +50,5 @@ targetExt.message = 'required module not found: `' + modPath.substr(root.length) + '`'; | ||
} | ||
debug('finally ', path.join(dir, fName + targetExt)); | ||
return path.join(dir, fName + targetExt); | ||
var finalPath = path.join(dir, fName + targetExt); | ||
debug('finally ', finalPath); | ||
return finalPath; | ||
} | ||
@@ -101,2 +102,4 @@ /** | ||
} | ||
} else { | ||
throw new Error('illegal module path, ' + modPath); | ||
} | ||
@@ -176,6 +179,7 @@ } catch (e) { | ||
function resolvePath(cube, root, base, module, ifRelease) { | ||
debug('start resolverPath', root, base, module); | ||
debug(ifRelease ? 'start resolver release path' : 'start resolver path', root, base, module); | ||
var dir = path.dirname(base); | ||
var origModule = module; | ||
var count = 0; | ||
var nodeModulePath; | ||
var p; | ||
@@ -188,4 +192,10 @@ // console.log('>>-- original path:', dir, base); | ||
count++; | ||
nodeModulePath = path.join(root, dir, '/node_modules/', module); | ||
if (cube.checkIgnore(nodeModulePath)) { | ||
console.error('[CUBE_WARN] required node_module ignored by .cubeignore config'); | ||
p = module; | ||
break; | ||
} | ||
try { | ||
p = testModPath(cube, root, path.join(root, dir, '/node_modules/', module)); | ||
p = testModPath(cube, root, nodeModulePath); | ||
debug('find node_modules:', path.join(dir, '/node_modules/', module)); | ||
@@ -623,3 +633,3 @@ break; | ||
abspath = resolvePath(cube, root, filepath, v, options.release); | ||
abspathOrigin = resolvePath(cube, root, filepath, v); | ||
abspathOrigin = options.release ? resolvePath(cube, root, filepath, v) : abspath; | ||
debug('resolved path:', abspath); | ||
@@ -626,0 +636,0 @@ } catch (err) { |
@@ -5,11 +5,20 @@ var debug = require('debug')('cube'); | ||
exports.traverseFList = function (arr, fName, possibleExts) { | ||
/** | ||
* 检查文件列表,找出可能的文件后缀。 | ||
* 如果多个文件匹配,则按processor的注册顺序优先级返回最前的可能 | ||
* @param {[type]} dirFList [description] | ||
* @param {[type]} fNameWithoutExt [description] | ||
* @param {[type]} possibleExts [description] | ||
* @return {[type]} [description] | ||
*/ | ||
exports.getPossibleExt = function (dirFList, fNameWithoutExt, possibleExts) { | ||
var tmp, ext, index; | ||
var matchedExts = []; | ||
for (var i = 0, len = arr.length; i < len; i++) { | ||
tmp = arr[i]; | ||
if (tmp.indexOf(fName + '.') !== 0) { | ||
// console.log('>>>>>>>', dirFList, fNameWithoutExt, possibleExts); | ||
for (var i = 0, len = dirFList.length; i < len; i++) { | ||
tmp = dirFList[i]; | ||
if (tmp.indexOf(fNameWithoutExt + '.') !== 0) { | ||
continue; | ||
} | ||
ext = tmp.substr(fName.length); | ||
ext = tmp.substr(fNameWithoutExt.length); | ||
index = possibleExts.indexOf(ext); | ||
@@ -56,3 +65,3 @@ if (index !== -1) { | ||
} | ||
var targetExt = exports.traverseFList(arr, fName, possibleExts); | ||
var targetExt = exports.getPossibleExt(arr, fName, possibleExts); | ||
if (targetExt instanceof Error) { | ||
@@ -92,2 +101,75 @@ targetExt.message = 'file not found: ' + qpath; | ||
return remote ? remote + ':' + file : file; | ||
}; | ||
}; | ||
/** | ||
* loading ignore config | ||
* @param {String} curPath 寻址的dir, 一般为root目录 | ||
* @return {Object} 配置信息 | ||
*/ | ||
exports.loadIgnore = function (curPath) { | ||
var ignoreRules; | ||
if (!/^(\w:|\/)/.test(curPath)) { | ||
curPath = path.join(process.cwd(), curPath); | ||
} | ||
var ignore = {skip: [], ignore: []}; | ||
var ending = process.platform.match(/^win/) ? /^\w:$/ : /^\/$/; | ||
while (!ending.test(curPath)) { | ||
// console.log('>>>>>>', curPath, ending, ending.test(curPath)); | ||
try { | ||
ignoreRules = fs.readFileSync(path.join(curPath, '.cubeignore')).toString().split(/\r?\n/g); | ||
console.log('.cubeignore found: ', path.join(curPath, '.cubeignore')); | ||
break; | ||
} catch (e) { | ||
if (e.code === 'ENOENT') { | ||
curPath = path.dirname(curPath); | ||
} else { | ||
e.message = '[CUBE] loading .cubeignore error, ' + e.message; | ||
console.log(e.message); | ||
return ignore; | ||
} | ||
} | ||
} | ||
var cate = 'skip'; | ||
ignoreRules && ignoreRules.forEach(function (v) { | ||
if (v === '[skip]') { | ||
cate = 'skip'; | ||
return; | ||
} else if (v === '[ignore]') { | ||
cate = 'ignore'; | ||
return; | ||
} | ||
if (!v) { | ||
return; | ||
} | ||
if (v.indexOf('/') === 0) { | ||
v = '^' + v; | ||
} | ||
ignore[cate].push(new RegExp(v.replace(/\./g, '\\.').replace(/\*/g, '.*'))); | ||
}); | ||
return ignore; | ||
}; | ||
/** | ||
* 检查是否忽略 | ||
* @param {String} file 文件名 | ||
* @param {Object} ignores 配置信息 | ||
* @return {Number} 1: skip, 2: ignore | ||
*/ | ||
exports.checkIgnore = function (file, ignores) { | ||
var flag = {}; | ||
var rule; | ||
['skip', 'ignore'].forEach(function (cate) { | ||
var tmp = ignores[cate]; | ||
var len = tmp.length; | ||
for (var i = 0; i < len; i++) { | ||
rule = tmp[i]; | ||
if (rule.test(file)) { | ||
flag[cate] = true; | ||
break; | ||
} | ||
} | ||
}); | ||
return flag; | ||
}; |
@@ -5,3 +5,3 @@ { | ||
"description": "a new way to write js in browser", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"homepage": "https://github.com/fishbar/cube", | ||
@@ -48,5 +48,6 @@ "repository": { | ||
"cube-stylus": "*", | ||
"cube-coffee": "*" | ||
"cube-coffee": "*", | ||
"cube-jsx": "*" | ||
}, | ||
"optionalDependencies": {} | ||
} |
@@ -8,6 +8,6 @@ Cube | ||
Cube支持三种类型: | ||
Cube管理着前端三种类型的资源: | ||
* script js/coffee | ||
* style css/stylus/less | ||
* script js/coffee/jsx/... | ||
* style css/stylus/less/... | ||
* template html/ejs/jade/... | ||
@@ -40,7 +40,7 @@ | ||
charset: 'utf-8', | ||
base: '/', // virtual path, base can be a http path: http://domain.com/project/static | ||
debug: true, // online module ,you should turn off this switch | ||
enableCss: true, // enable dynamic loading css resource | ||
version: 12345, // the code version, used for flushing client side script | ||
timeout: 15000 // loading script timeout setup | ||
base: '/', // virtual path, base can be a http path, | ||
// like: http://domain.com/project/static | ||
debug: true, // online module ,you should turn off this switch | ||
version: 12345, // the code version, used for flushing client side script | ||
timeout: 15000 // loading script timeout setup | ||
}); | ||
@@ -47,0 +47,0 @@ Cube.use('/main.js', function (App) { |
@@ -17,3 +17,3 @@ /*! | ||
// var DEBUG = false; | ||
var ENABLE_CSS = false; | ||
// var ENABLE_CSS = false; | ||
// var ENABLE_SOURCE = window.localStorage ? window.localStorage.getItem('__cube_debug__') : false; | ||
@@ -97,5 +97,7 @@ var HEAD = document.getElementsByTagName('head')[0]; | ||
} | ||
/* | ||
if (config.enableCss) { | ||
ENABLE_CSS = config.enableCss; | ||
} | ||
*/ | ||
return this; | ||
@@ -215,2 +217,3 @@ }; | ||
if (typeof param1 !== 'function') { | ||
/** | ||
if (!ENABLE_CSS) { | ||
@@ -220,2 +223,3 @@ console.warn('[Cube] dynamic loading css disabled!'); | ||
} | ||
**/ | ||
// mod cb -> namespace | ||
@@ -222,0 +226,0 @@ Cube.use(mod, function (css) { |
@@ -9,3 +9,3 @@ /*! | ||
/** | ||
* esj filter | ||
* ejs filter | ||
* the following code from ejs package | ||
@@ -12,0 +12,0 @@ */ |
72
tools.js
var xfs = require('xfs'); | ||
var path = require('path'); | ||
var requires = require('requires'); | ||
var utils = require('./lib/utils'); | ||
@@ -16,69 +17,2 @@ | ||
function loadIgnore(curPath) { | ||
var ignoreRules; | ||
if (!/^(\w:|\/)/.test(curPath)) { | ||
curPath = path.join(process.cwd(), curPath); | ||
} | ||
var ignore = {skip: [], ignore: []}; | ||
var ending = process.platform.match(/win/) ? /^\w:$/ : /^\/$/; | ||
while (!ending.test(curPath)) { | ||
try { | ||
ignoreRules = xfs.readFileSync(path.join(curPath, '.cubeignore')).toString().split(/\r?\n/g); | ||
console.log('.cubeignore found: ', path.join(curPath, '.cubeignore')); | ||
break; | ||
} catch (e) { | ||
if (e.code === 'ENOENT') { | ||
curPath = path.dirname(curPath); | ||
} else { | ||
e.message = '[CUBE] loading .cubeignore error, ' + e.message; | ||
console.log(e.message); | ||
return ignore; | ||
} | ||
} | ||
} | ||
var cate = 'skip'; | ||
ignoreRules.forEach(function (v) { | ||
if (v === '[skip]') { | ||
cate = 'skip'; | ||
return; | ||
} else if (v === '[ignore]') { | ||
cate = 'ignore'; | ||
return; | ||
} | ||
if (!v) { | ||
return; | ||
} | ||
if (v.indexOf('/') === 0) { | ||
v = '^' + v; | ||
} | ||
ignore[cate].push(new RegExp(v.replace(/\./g, '\\.').replace(/\*/g, '.*'))); | ||
}); | ||
return ignore; | ||
} | ||
/** | ||
* 检查是否忽略 | ||
* @param {String} file 文件名 | ||
* @param {Object} ignores 配置信息 | ||
* @return {Number} 1: skip, 2: ignore | ||
*/ | ||
function checkIgnore(file, ignores) { | ||
var flag = {}; | ||
var rule; | ||
['skip', 'ignore'].forEach(function (cate) { | ||
var tmp = ignores[cate]; | ||
var len = tmp.length; | ||
for (var i = 0; i < len; i++) { | ||
rule = tmp[i]; | ||
if (rule.test(file)) { | ||
flag[cate] = true; | ||
break; | ||
} | ||
} | ||
}); | ||
return flag; | ||
} | ||
function analyseNoduleModules(fpath, map, done) { | ||
@@ -207,3 +141,3 @@ var modules = xfs.readdirSync(fpath); | ||
var fileCount = 0; | ||
var ignores = loadIgnore(source); | ||
var ignores = utils.loadIgnore(source); | ||
var errors = []; | ||
@@ -221,3 +155,3 @@ var root = cube.config.root; | ||
var destFile = path.join(dest, relFile); | ||
var checked = checkIgnore(relFile, ignores); | ||
var checked = utils.checkIgnore(relFile, ignores); | ||
/* | ||
@@ -224,0 +158,0 @@ if (/\.min\.(css|js)$/.test(sourceFile)) { |
4140
309051
11
38