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

脚本中的匿名模块 #16

Closed
otakustay opened this issue Apr 18, 2014 · 3 comments
Closed

脚本中的匿名模块 #16

otakustay opened this issue Apr 18, 2014 · 3 comments

Comments

@otakustay
Copy link
Member

有这样的代码

<script>
    define({});
</script>

之后其它匿名模块的定义都会出错

当然上面的代码并不会真的这么写,可能是在一个.js文件中,然后被<script>标签引入。因此esl遇到匿名模块且无法确定模块名称时,是否适当地采取抛弃或其它措施?

@errorrik
Copy link
Contributor

这个问题出现的原因是:

当遇到define({});这种anonymous define时,loader是不知道这货是啥的,只能先记录下来。所以这种情况,loader只能这么处理,并且所有loader都是这么干的:

  1. 发起一个script src,绑定onload的处理
  2. http返回,script run。这时候define被调用,记录到某个地方
  3. script run完后,onload事件触发。在这里去complete define。步骤1中module id是已知的,所以onload里module id是已知的。拿这个id作为步骤2中define的模块id

esl的实现里,原先是:anonymous define调用时,如果已存在,则后面的都会被忽略
如果要改动,只能变成以后来者为准

换句话说,define在调用时,是没办法知道这个define是有意义还是无意义的,只能更改anonymous define的策略。

erik168 added a commit that referenced this issue Apr 23, 2014
@otakustay
Copy link
Member Author

确定个事,如果foo.jsbar.js中都有一个define('foo', func...(合并引起的),那么加载这2个js,foofactory会不会就被执行2次了呢?

@errorrik
Copy link
Contributor

errorrik commented May 5, 2014

不会,先到为准

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

No branches or pull requests

2 participants