-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wepy-cli: Added aliasFields feature && mainFields feature #595
Conversation
@@ -43,8 +43,10 @@ export default { | |||
if (config.output === 'ant' && lib === 'wepy') { | |||
lib = 'wepy-ant'; | |||
} | |||
lib = resolve.resolveAlias(lib); | |||
if (path.isAbsolute(lib)) { | |||
lib = resolve.resolveAlias(lib, opath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里lib是怎么得到 string的false呢,配置中是配置的 'module-a': false
另外:require('module-a') 编译为 require('{}') 好像也不对吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果在aliasFields中有配置 'module-a': false时,{'module-a': false}会被extends到alias上,'module-a'经resolveAlias后被替换成 'false'(可参考这里)
var a = require('module-a')经由这步是会编译为 var a = {}的
如:
let code = `var a = require('module-a')`
code = code.replace(/require\(['"]([\w\d_\-\.\/@]+)['"]\)/ig, function (match, lib) {
return '{}'
})
console.log(code) // => var a = {}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
soga
packages/wepy-cli/src/compile-wpy.js
Outdated
@@ -254,7 +254,8 @@ export default { | |||
(() => { | |||
let coms = {}; | |||
rst.script.code.replace(/import\s*([\w\-\_]*)\s*from\s*['"]([\w\-\_\.\/\@]*)['"]/ig, (match, com, path) => { | |||
coms[com] = path; | |||
if (path !== 'false') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是编译前的代码,那这里的 import xxx from 'false' 是哪里来的呢。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
嗯...这里判断无效
// Count1: '../components/count', | ||
// Count2: '../components/count' | ||
// }; | ||
requires = util.unique(requires) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch
packages/wepy-cli/src/resolve.js
Outdated
this.alias = Object.assign({}, { [key]: value }, this.alias || {}); | ||
} | ||
// fields转换:"./src/index.wpy" => "src/index.wpy"后合并至fieldsAlias中 | ||
if (!path.isAbsolute(key) && path.extname(key)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
相对路径,不带后缀的怎么处理呢。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里漏了,我补一下,这种情况下理想结果应该是命中后会被替换
另外test case呢 |
if (path.isAbsolute(r)) { | ||
if (path.extname(r) === '' && util.isFile(r + ext)) { | ||
comsrc = r + ext; | ||
let lib = resolve.resolveAlias(r, opath, ext); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ext没必要传进去...
@@ -43,8 +43,10 @@ export default { | |||
if (config.output === 'ant' && lib === 'wepy') { | |||
lib = 'wepy-ant'; | |||
} | |||
lib = resolve.resolveAlias(lib); | |||
if (path.isAbsolute(lib)) { | |||
lib = resolve.resolveAlias(lib, opath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果在aliasFields中有配置 'module-a': false时,{'module-a': false}会被extends到alias上,'module-a'经resolveAlias后被替换成 'false'(可参考这里)
var a = require('module-a')经由这步是会编译为 var a = {}的
如:
let code = `var a = require('module-a')`
code = code.replace(/require\(['"]([\w\d_\-\.\/@]+)['"]\)/ig, function (match, lib) {
return '{}'
})
console.log(code) // => var a = {}
packages/wepy-cli/src/compile-wpy.js
Outdated
@@ -254,7 +254,8 @@ export default { | |||
(() => { | |||
let coms = {}; | |||
rst.script.code.replace(/import\s*([\w\-\_]*)\s*from\s*['"]([\w\-\_\.\/\@]*)['"]/ig, (match, com, path) => { | |||
coms[com] = path; | |||
if (path !== 'false') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
嗯...这里判断无效
packages/wepy-cli/src/resolve.js
Outdated
this.alias = Object.assign({}, { [key]: value }, this.alias || {}); | ||
} | ||
// fields转换:"./src/index.wpy" => "src/index.wpy"后合并至fieldsAlias中 | ||
if (!path.isAbsolute(key) && path.extname(key)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里漏了,我补一下,这种情况下理想结果应该是命中后会被替换
test case 我补一下 |
packages/wepy-cli/src/resolve.js
Outdated
// => "xyz"、"xyz-xyz" | ||
let value; | ||
if (!pkg[fields][key]) { | ||
value = 'false' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里得到string类型的'false'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
@leeyeh 看看符合要求吗 |
LGTM. 非常感谢。 |
Changes Unknown when pulling d42040d on dlhandsome:master into ** on Tencent:master**. |
Changes Unknown when pulling d42040d on dlhandsome:master into ** on Tencent:master**. |
Test:
Relevant PR: leancloud/javascript-sdk#529 |
添加了wepy-cli对aliasFields及mainFields的支持
aliasFields
resolve.aliasFields
:['wepy', 'weapp', 'browser']
(优先级递减)mainFields
resolve.mainFields
:['wepy', 'weapp', 'browser', 'module', 'main']
(优先级递减)