Skip to content
This repository has been archived by the owner on Aug 15, 2018. It is now read-only.

跨终端依赖定义 #1028

Closed
hotoo opened this issue Sep 19, 2014 · 8 comments
Closed

跨终端依赖定义 #1028

hotoo opened this issue Sep 19, 2014 · 8 comments
Labels
Milestone

Comments

@hotoo
Copy link
Member

hotoo commented Sep 19, 2014

开发者在编写代码时,可能会依赖 jQuery,但是如果开发的模块同时会被用于无线设备,会被要求依赖 zepto,这种情况该如何较好的处理?

另外还有版本问题,开发时 spm install jquery --save 安装了最新的 jQuery 版本,但实际生产环境可能使用其他版本。

这些让模块独立的方法很好,但是从产品层面建议、甚至需要统一时,该如何是好?

@hotoo hotoo added the discuss label Sep 19, 2014
@sorrycc
Copy link
Member

sorrycc commented Sep 19, 2014

构建时 ignore jquery:

$ spm build --ignore jquery

然后通过 seajs.config alias 指定 jquery

seajs.config({
  alias: {
    jquery: 'jquery/version/jquery.js'
  }
});

换成 zepto 则是:

seajs.config({
  alias: {
    jquery: 'zepto/version/zepto.js'
  }
});

@airyland
Copy link

@sorrycc 的方法真是简单粗暴,虽然感觉有点小怪,但是又没有想到什么不妥的地方。

之前遇到一两个这样的模块都处理成export带$参数的函数,这样用jQuery或者Zepto,用哪个版本都自由了,就是感觉有点丑。

module.exports = function($){
    //whatever
};

之前还有一部分处理成index.js和 src/code.js
index.js 是PC站使用,自动引用了jquery, code.js 同上面

module.exports = require('./src/code')(require('jquery'));

当要在PC站使用时

var module = require('index');

当要在手机站使用时,用code.js并手动挂上Zepto..

var module = require('./src/code')(require('zepto'));

@hotoo
Copy link
Member Author

hotoo commented Sep 21, 2014

@sorrycc 上线时不用 seajs,需要打包成 standalone 的。

@sorrycc
Copy link
Member

sorrycc commented Sep 21, 2014

@airyland 的方法也是一种选择。
@hotoo standalone 模式目前还不支持 ignore,同类需求强烈的话可以考虑支持。

但是,pkg 如果是基于 jQuery 实现的,如果硬改为 zepto 应该会存在风险吧。fork 一份改成依赖 zepto 的,然后测试通过后再作为另一个 pkg 发布到源上会不会更合适?

@hotoo
Copy link
Member Author

hotoo commented Sep 22, 2014

fork 也是一法,但是维护多份差不多的源码感觉很不喜欢。


另外就单独拿版本说事,比如模块开发时,可以依赖 jQuery 最新版本 2.1.1,但是线上实际使用的是 1.7.2,即使是在 PC 上使用,也是推荐使用统一的依赖版本。

A 公司统一使用 jQuery 1.7.2, B 公司统一使用 jQuery 2.1.1,对于这个独立的模块来说,实际上并不能非常好的共享给多个组织使用(除非模块本身没有第三方依赖)。

@afc163
Copy link
Member

afc163 commented Sep 22, 2014

jQuery 和 zepto 兼容的方案很早就试行过,后来觉得这条路走不通,而且 zepto 本身也不怎么维护了,就放弃了。

aralejs/aralejs.github.io#26
aralejs/aralejs.github.io#316


jQuery 的依赖版本方案在很多开源库上(比如各 jQuery 插件)也有类似的问题,感觉没有完美的方案(如果使用了偏门和过期的 API 的话),感觉现在的 ignore 打通所有依赖已经很良心了。

@hotoo
Copy link
Member Author

hotoo commented Sep 22, 2014

OK, 期待 standalone 也支持 ignore

@sorrycc
Copy link
Member

sorrycc commented Nov 11, 2014

突然发现 standalone 和 umd 其实已经支持 ignore 了。

比如:index.js 依赖 type 组件。

var a = require('type');

--include standalone --ignore type 构建后是:

(function() {
  var test_010_index;
  test_010_index = function() {
    var a = type;
  };
})();

require 内部文件也是类似,比如:

var a = require('type/index.js');

输出是:

(function() {
  var test_010_index;
  test_010_index = function() {
    var a = type_indexjs;
  };
})();

@sorrycc sorrycc closed this as completed Nov 11, 2014
@popomore popomore added this to the 3.3 milestone Dec 11, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants