fis3-hook-lego
Advanced tools
Comparing version 0.2.1 to 0.2.2
28
index.js
@@ -8,11 +8,10 @@ /** | ||
module.exports = function(fis, opts) { | ||
var entry = module.exports = function(fis, opts) { | ||
fis.on('lookup:file', function(info, file) { | ||
// 暂时只分析 js 文件,后续 Ques 可以这里搞起 | ||
if (file.isJsLike && /^[a-zA-Z0-9_@.-]+$/.test(info.rest)) { | ||
if (file.isJsLike && info.rest) { | ||
var ret = lookup(info.rest, opts); | ||
if (ret) { | ||
info.id = info.moduleId = ret; | ||
info.id = ret; | ||
info.moduleId = | ||
ret.replace(/^[^\/]*\/?/, '').replace(/\.js$/,''); | ||
} | ||
@@ -22,1 +21,18 @@ } | ||
}; | ||
entry.defaultOptions = { | ||
paths: [ | ||
{ | ||
location: 'lego_modules', | ||
// lego_modules/pkgName/version/subFile | ||
type: 'lego' | ||
}, | ||
{ | ||
location: 'modules', | ||
// modules/mod.js | ||
// modules/mod/mod.js | ||
type: 'mod' | ||
} | ||
] | ||
}; | ||
127
lookup.js
@@ -12,10 +12,9 @@ /** | ||
fs = _.fs, | ||
lego = fis.project.getProjectPath('lego_modules/'), | ||
mod = fis.project.getProjectPath('modules/'), | ||
compare = require('compare-version'); | ||
module.exports = function(id, opts) { | ||
var ignore = opts.ignore; | ||
var ignore = opts.ignore, | ||
ret; | ||
// 剔除后缀 | ||
id = id.replace(/\.js$/, ''); | ||
@@ -33,17 +32,35 @@ if (typeof ignore === 'string') { | ||
return getModule(id) || getLegoModule(id); | ||
opts.paths.every(function(item) { | ||
if (item.type === 'lego') { | ||
ret = getLegoModule(item.location, id); | ||
} else { | ||
ret = getModule(item.location, id); | ||
} | ||
return !ret; | ||
}); | ||
return ret || null; | ||
}; | ||
/** | ||
* 从 modules 目录获取模块 | ||
* @param {String} root 目录 | ||
* @param {String} id 就是 require('zepto') 中的 zepto | ||
*/ | ||
function getModule(id) { | ||
function getModule(root, id) { | ||
var mod = fis.project.getProjectPath(root); | ||
// 不处理多级目录 | ||
// 快速判断 | ||
if (id.indexOf('\/') !== -1 | ||
|| (!getListAll(root)[id + '.js'] && !getListAll(root)[id]) | ||
) { | ||
return null; | ||
} | ||
if (_.isFile(_(mod, id + '.js'))) { | ||
fis.log.debug('lego: get %s from <modules>', id); | ||
return id; | ||
return _(root, id + '.js'); | ||
} else if (_.isFile(_(mod, id, id + '.js'))) { | ||
fis.log.debug('lego: get %s from <modules>', id + '/' + id); | ||
return id; | ||
return _(root, id, id + '.js'); | ||
} | ||
@@ -54,27 +71,28 @@ | ||
/** | ||
* 从 lego_modules 下获取模块 | ||
* @param {String} root 目录 | ||
* @param {String} id 就是 require('zepto') 中的 zepto | ||
*/ | ||
function getLegoModule(id) { | ||
var ver, versions, pkg, legoInfo, ret, | ||
tempId = id.split('@'); | ||
function getLegoModule(root, id) { | ||
var ver, versions, | ||
lego = fis.project.getProjectPath(root), | ||
match = id.match(/^\/?([^@\/]+)(@([^\/]*))?(\/(.*)+)?$/); | ||
pkgName = match && match[1] || '', | ||
ver = match && match[3] || '', | ||
subFile = match && match[5] || ''; | ||
id = tempId[0]; | ||
// 做一次快速判断 | ||
if (!pkgName || !getListAll(root)[pkgName]) { | ||
return null; | ||
} | ||
root = _(lego, id); | ||
fis.log.debug('lego: get %s from <lego_modules>', id); | ||
fis.log.debug('lego: get %s from <lego_modules>', id); | ||
if (!_.isDir(root)) { | ||
if (!_.isDir(_(lego, pkgName))) { | ||
return fis.log.error('lego: 找不到 lego 组件 %s 的目录', id); | ||
} | ||
if (ver = tempId[1]) { // 指定版本号 | ||
// ver = _(root, ver); | ||
if (!_.isDir(_(root, ver))) { | ||
return fis.log.error('lego: 找不到 lego 组件 %s 的对应的版本', id, tempId[1]); | ||
} | ||
} else { // 默认最新 | ||
versions = fs.readdirSync(root) || []; | ||
if (!ver) { // 默认最新 | ||
versions = fs.readdirSync(_(lego, pkgName)) || []; | ||
@@ -90,24 +108,49 @@ if (versions.length > 1) { | ||
pkg = _(root, ver, 'package.json'); | ||
if (!_.isDir(_(lego, pkgName, ver))) { | ||
return fis.log.error('lego: 找不到 lego 组件 %s 的对应的版本', id, ver); | ||
} | ||
try { | ||
pkg = require(pkg); | ||
legoInfo = pkg.lego; | ||
ret = _(id, ver, legoInfo.main.replace(/\.js$/, '')); | ||
} catch (ex) { | ||
// 如果不存在 package.json | ||
if (!subFile) { | ||
try { | ||
subFile = require(_(lego, pkgName, ver, 'package.json')) | ||
.lego.main.replace(/\.js$/, ''); | ||
} catch (ex) { | ||
// 如果不存在 package.json | ||
fis.log.info('lego: 组件 %s 没有 package.json', id); | ||
} | ||
fis.log.info('lego: 组件 %s 没有 package.json', id); | ||
// main | ||
// zepto/x.x.x/zepto.js 同名文件 | ||
// zepto/x.x.x/index.js index 文件 | ||
[subFile, root, 'index'].every(function(item) { | ||
subFile = item; | ||
return !item || !_.isFile(_(lego, pkgName, ver, item + '.js')); | ||
}); | ||
} | ||
if (_.isFile(_(root, ver, id + '.js'))) { // zepto/x.x.x/zepto.js 同名文件 | ||
ret = _(id, ver, id); | ||
} else if (_.isFile(_(root, ver, 'index.js'))) { // zepto/x.x.x/index.js index 文件 | ||
ret = _(id, ver, 'index'); | ||
} else { | ||
fis.log.error('lego: 没有找到 %s 的主文件', id); | ||
if (!_.isFile(_(lego, pkgName, ver, subFile + '.js'))) { | ||
return fis.log.error('lego: 找不到 lego 组件 %s 的对应的文件', id, subFile); | ||
} | ||
return _(root, pkgName, ver, subFile + '.js'); | ||
} | ||
/** | ||
* 每一个文件都会经过lookup,防止频繁的判断文件是否存在,先列出一级目录,以备快速判断 | ||
*/ | ||
var _listAll = {}; | ||
function getListAll(root) { | ||
var path; | ||
if (!_listAll[root]) { | ||
_listAll[root] = {}; | ||
path = fis.project.getProjectPath(root); | ||
if (_.isDir(path)) { | ||
fs.readdirSync(path).forEach(function(item) { | ||
_listAll[root][item] = true; | ||
}); | ||
} | ||
} | ||
return _listAll[root]; | ||
}; | ||
return ret; | ||
} |
{ | ||
"name": "fis3-hook-lego", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "lego 包管理模块查找", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -81,1 +81,9 @@ # fis3-hook-lego | ||
``` | ||
### 注意 | ||
由于有多版本的场景,lego会修改文件id,`比如require('zepto'), 产出后是 require('zepto/1.1.6/zepto')`, | ||
其他插件的配置中需要zepto时,比如 ignore: ['zepto'],查找会有问题,解决方案: | ||
1. ignore: ['zepto/1.1.6/zepto'],指定具体的版本 | ||
2. 在处理逻辑之前,调用 fis.get('idMaps'), 将zepto的id进行转化(ps:idMaps中记录了lego对文件id的修改,key值是修改前的id,value是修改后的id) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
89
8339
5
161
2