Skip to content
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

Merged
merged 11 commits into from
Dec 25, 2017

Conversation

dlhandsome
Copy link
Collaborator

@dlhandsome dlhandsome commented Dec 2, 2017

添加了wepy-cli对aliasFields及mainFields的支持

aliasFields

  • default config

resolve.aliasFields['wepy', 'weapp', 'browser'](优先级递减)

  • package.json
{
  "wepy": {
      "module-a": false,  // import a from 'module-a'  => var a = {}
      "count": "./src/components/count-wepy.wpy",  // 如果alias中存在count,alias优先
      "./src/list.wpy": "./src/components/list-wepy.wpy"  // files must be replaced
  }
}

mainFields

  • default config

resolve.mainFields: ['wepy', 'weapp', 'browser', 'module', 'main'](优先级递减)

  • npm package.json
{
  "wepy": "lib/wepy-com-swiper.wpy",  // npm包将会以wepy的键值作为入口加载
  "browser": "lib/swiper.js"
}

@Gcaufy Gcaufy mentioned this pull request Dec 3, 2017
@@ -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);
Copy link
Collaborator

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('{}') 好像也不对吧

Copy link
Collaborator Author

@dlhandsome dlhandsome Dec 3, 2017

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 = {}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

soga

@@ -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')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是编译前的代码,那这里的 import xxx from 'false' 是哪里来的呢。

Copy link
Collaborator Author

@dlhandsome dlhandsome Dec 3, 2017

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

this.alias = Object.assign({}, { [key]: value }, this.alias || {});
}
// fields转换:"./src/index.wpy" => "src/index.wpy"后合并至fieldsAlias中
if (!path.isAbsolute(key) && path.extname(key)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

相对路径,不带后缀的怎么处理呢。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里漏了,我补一下,这种情况下理想结果应该是命中后会被替换

@Gcaufy
Copy link
Collaborator

Gcaufy commented Dec 3, 2017

另外test case呢

if (path.isAbsolute(r)) {
if (path.extname(r) === '' && util.isFile(r + ext)) {
comsrc = r + ext;
let lib = resolve.resolveAlias(r, opath, ext);
Copy link
Collaborator Author

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);
Copy link
Collaborator Author

@dlhandsome dlhandsome Dec 3, 2017

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 = {}

@@ -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')
Copy link
Collaborator Author

@dlhandsome dlhandsome Dec 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯...这里判断无效

this.alias = Object.assign({}, { [key]: value }, this.alias || {});
}
// fields转换:"./src/index.wpy" => "src/index.wpy"后合并至fieldsAlias中
if (!path.isAbsolute(key) && path.extname(key)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里漏了,我补一下,这种情况下理想结果应该是命中后会被替换

@dlhandsome
Copy link
Collaborator Author

test case 我补一下

// => "xyz"、"xyz-xyz"
let value;
if (!pkg[fields][key]) {
value = 'false'
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里得到string类型的'false'

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@dlhandsome
Copy link
Collaborator Author

@leeyeh 看看符合要求吗

@leeyeh
Copy link
Contributor

leeyeh commented Dec 4, 2017

LGTM.

非常感谢。

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling d42040d on dlhandsome:master into ** on Tencent:master**.

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling d42040d on dlhandsome:master into ** on Tencent:master**.

@Gcaufy
Copy link
Collaborator

Gcaufy commented Dec 25, 2017

@leeyeh @dlhandsome

Test:

$ npm i leancloud-storage --save

$ vi ./src/app.wpy
// added import
import ls from 'leancloud-storage';

$ vi ./node_modules/leancloud-storage/dist/av-weapp-min.js
// added to the first line for testing relative 
var lq = require('./node/index-live-query');

$ wepy build --no-cache

Relevant PR: leancloud/javascript-sdk#529

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants