Skip to content

Commit

Permalink
feat(cli): firstly resolve npm modules's entry file then other
Browse files Browse the repository at this point in the history
  • Loading branch information
Genuifx committed Jul 9, 2019
1 parent 7b74ec2 commit c73d907
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions packages/wxa-cli/src/helpers/dependencyResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ class DependencyResolver {
if (pret.isURI || pret.isDynamic || pret.isBase64) return {pret, source, lib};
// 处理无后缀情况
// ext = path.extname(source);
if (!isFile(source) && !pret.isWXALib) {
if (!isFile(source) && pret.isNodeModule) {
ext = this.$findNodeModuleEntryFile(lib, source);

if (ext == null) throw new Error('找不到文件 '+lib);
} else if (!isFile(source) && !pret.isWXALib) {
// not support resolve extension.
if (!needFindExt) throw new Error('文件不存在');

Expand All @@ -39,24 +43,14 @@ class DependencyResolver {
else if (pext) ext = pext;
else if (isDir(source) && isFile(source+path.sep+'index.js')) ext = path.sep+'index.js';
else {
// 非指定文件的node_modules路径依赖
let pkg = this.getPkgConfig(lib);
if (!pkg) {
throw new Error('找不到模块'+lib);
}
let main = pkg.main || 'index.js';
if (pkg.browser && typeof pkg.browser === 'string') {
main = pkg.browser;
}
if (isFile(path.join(source, main))) {
ext = path.sep+main;
} else {
throw new Error('找不到文件 '+lib);
}
ext = this.$findNodeModuleEntryFile(lib, source);

if (ext == null) throw new Error('找不到文件 '+lib);
}
} else {
ext = '';
}

source = path.resolve(source+ext);
lib += ext;

Expand All @@ -67,6 +61,22 @@ class DependencyResolver {
};
}

$findNodeModuleEntryFile(lib, source) {
// 非指定文件的node_modules路径依赖
let pkg = this.getPkgConfig(lib);
if (!pkg) {
throw new Error('找不到模块'+lib);
}

let main = pkg.main || 'index.js';
// 优先使用依赖的 browser 版本
if (pkg.browser && typeof pkg.browser === 'string') {
main = pkg.browser;
}

return isFile(path.join(source, main)) ? path.sep+main : null;
}

$resolve(lib, mdl) {
let opath = path.parse(mdl.meta.source);

Expand Down

0 comments on commit c73d907

Please sign in to comment.