Skip to content
leeight edited this page Aug 6, 2014 · 4 revisions

为什么要进行包管理

通常我们在开发一个项目时,都会使用一些第三方开发的library,比如underscore、moment.js、hogan、er、echarts、esui、etpl等。而不同的library可能以不同的方式编写和提供,使用的方式也不同。

我们认为,可被复用的代码集或功能集,都应该以一致的方式编写和声明,并且发布到云端的仓库中。这样能够减少使用者的了解与学习成本,并且能够通过工具来管理。为此,EFE团队:

遵循以上规范的可复用的代码集,可以做为一个包,发布到registry,并且通过edp提供的包管理功能进行管理。

未遵循以上规范的第三方library,可以通过针对性的封装,使其遵循规范,并发布。大多数流行library都考虑了AMD的使用模式,通常只需要将其放在相应的目录结构下,并增加包声明文件package.json,即可。

查找当前存在的包

通过edp的search命令:edp search [keyword],可以查找当前存在的包。如果不输入keyword,将列出当前存在的所有包。

$ edp search under

NAME                  AUTHOR     VERSIONS   DATE

bs-edu-store          ecomfe     1.3.14     2014-02-10 02:06
store                 firede     1.3.9      2013-06-20 05:19
underscore            firede     1.5.2      2013-11-26 10:24
underscore.string     firede     2.3.1      2013-06-20 05:17

如果不想通过命令,可以直接在edp的网站上查找和浏览存在的包。

导入并使用包

在开发自己的项目时,如果想使用一些package,可以通过edp的import命令:edp import <package>,进行导入。导入包时,其依赖包也会被一并导入。

导入的包将存放在项目dep目录下,下面的资料是我们认为合理的前端项目的目录组织方式

如果通过edp提供的项目管理功能来管理项目,可以在项目目录下的任何一个子目录中运行edp import。否则,请在项目根目录下运行edp import

通过edp提供的项目管理功能来管理项目,导入包时将自动更新html文件中的加载器配置require.config({...})中的packages项。否则,在导入包后需要手工更新自己项目中的加载器配置。

如果不想更新某些html文件中的require.config({...})的配置信息,可以添加:

<!--edp:{"loaderAutoConfig": false}-->

如果只想要更新packages的配置信息,但是不想更新baseUrl,可以添加:

<!--edp:{"preserveBaseUrl": true}-->

导入包的最新版

直接输入package的名称,将导入当前存在的最新版本。

$ edp import etpl

info trying registry request attempt 1 at 17:29:50
http GET http://registry.edp.baidu.com/etpl
http 304 http://registry.edp.baidu.com/etpl
info trying registry request attempt 1 at 17:29:50
http GET http://registry.edp.baidu.com/etpl/2.1.0
http 304 http://registry.edp.baidu.com/etpl/2.1.0

$ ls dep/etpl

2.1.0

导入指定版本的包

输入edp import package@version,可以导入指定版本的包。

$ edp import etpl@2.0.8

info trying registry request attempt 1 at 17:32:02
http GET http://registry.edp.baidu.com/etpl
http 304 http://registry.edp.baidu.com/etpl
info trying registry request attempt 1 at 17:32:02
http GET http://registry.edp.baidu.com/etpl/2.0.8
http 200 http://registry.edp.baidu.com/etpl/2.0.8

$ ls dep/etpl

2.0.8

升级已导入的包

通过edp的update命令:edp update [package],可以对使用import命令导入过的包进行升级。

升级指定包

如果当前使用的包不是最新版,想升级到最新版,可以使用edp update package

为了防止对用户对第三方包手工修改的删除,升级过程将进行用户确认。但我们依然要强调:depdep/packages.manifest不建议手工修改。

$ edp update etpl

info trying registry request attempt 1 at 18:55:53
http GET http://registry.edp.baidu.com/etpl
http 304 http://registry.edp.baidu.com/etpl
etpl - current version: 2.0.8, will update to version: 2.1.0
Continue update? [Y/n]y
info trying registry request attempt 1 at 18:56:06
http GET http://registry.edp.baidu.com/etpl
http 304 http://registry.edp.baidu.com/etpl
info trying registry request attempt 1 at 18:56:06
http GET http://registry.edp.baidu.com/etpl/2.1.0
http 304 http://registry.edp.baidu.com/etpl/2.1.0

$ ls dep/etpl

2.1.0

升级所有包

不指定package,使用edp update可以根据已导入包的依赖信息,升级所有导入的包。

如果当前开发的项目是一个包,升级过程将使用package.json中声明的依赖信息。

如果通过edp提供的项目管理功能来管理项目,已导入包的依赖信息保存在.edpproj/metadata文件中。

$ edp update

info trying registry request attempt 1 at 19:15:30
http GET http://registry.edp.baidu.com/er
http 304 http://registry.edp.baidu.com/er
er - Current dependence `er` does not exist, will update to version: 3.1.0-alpha.8
Continue update? [Y/n]y
......
esui - Current dependence `esui` does not exist, will update to version: 3.1.0-alpha.8
Continue update? [Y/n]y
......

$ ls dep

er    esui    etpl    mini-event    moment    packages.manifest    underscore

Hello World

Clone this wiki locally