js本地存储和增量更新rstoreinc插件使用

原创
2014/03/05 12:07
阅读数 605

<p>&#160;</p> <p>用增量更新这个算法写了一个requirejs插件storeinc,使用方法如下:</p> <p>首先使用修改后的r.js来进行混淆打包,构建配置如下(请看js/requirejs下的build.js):</p> <p>({</p> <p>&#160;&#160;&#160; appDir: &quot;../demo/js&quot;,</p> <p>&#160;&#160;&#160; baseUrl: &quot;./&quot;,</p> <p>&#160;&#160;&#160; dir: &quot;../demo/dist&quot;,//不走增量更新的文件存放路径</p> <p>&#160;&#160;&#160; paths: {</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; log: &quot;a&quot;,</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; storeinc:&quot;../storeinc&quot;//这里插件</p> <p>&#160;&#160;&#160; },</p> <p>&#160;&#160;&#160; storeinc: true,//说明走storeinc插件</p> <p>&#160;&#160;&#160; storedir: &quot;../demo/storeincdist&quot;,//混淆后的js存放路径,包括增量文件</p> <p>&#160;&#160;&#160; lastver:&quot;2&quot;, //上一个版本号,如果没有说明是第一次打包</p> <p>&#160;&#160;&#160; ver:'3',//新版版本号</p> <p>&#160;&#160;&#160; chunkSize:12,//增量更新块号</p> <p>&#160;&#160;&#160; modules: [</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; name: &quot;test&quot;,</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; exclude: [</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &quot;log&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;storeinc&quot;</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ]</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; },</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; name: &quot;log&quot;,</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; exclude: [</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &quot;storeinc&quot;</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ]</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; },</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; name: &quot;b&quot;,</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; exclude: [</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &quot;storeinc&quot;,&quot;log&quot;</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ]</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; },</p> <p>&#160;&#160;&#160; ]</p> <p>})</p> <p>运行node r.js -o build.js</p> <p>第一次运行时 node r.js :</p> <p>&#160;&#160;&#160; lastver:&quot;1&quot;, //上一个版本号,如果没有说明是第一次打包</p> <p>ver:'2',//新版版本号</p> <p>然后到js目录修改各个源文件,第二次运行时:</p> <p>&#160;&#160;&#160; lastver:&quot;2&quot;, //上一个版本号,如果没有说明是第一次打包</p> <p>ver:'3',//新版版本好号</p> <p>然后在index.html里面加入如下代码(请看js/requirejs下的index.html):</p> <p>&lt;script&gt;</p> <p>//storeinc相关配置</p> <p>var require = {</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ver:&quot;3&quot;,//当前版本</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; storeproxy:false,//是否使用代理方式来计算增量更新</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; paths: {</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; storeinc: '../storeinc'//增量更新插件路径</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p> <p>};</p> <p>&lt;/script&gt;</p> <p>//主资源下载,记住storeinc!前缀来启用插件</p> <p>&#160;&#160;&#160; &lt;script data-main=&quot;storeincdist/storeinc!test&quot; src=&quot;require.js&quot;&gt;&lt;/script&gt;</p> <p>&#160;&#160;&#160; &lt;script type=&quot;text/javascript&quot;&gt;</p> <p>&#160;&#160;&#160;&#160;&#160; requirejs.config({</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; paths: {</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; log: [</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'a'</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ],</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; b: [</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'b'</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ],</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; c: [</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'c'</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ],</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; d: [</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'd'</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ],</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; e: [</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'e'</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ]</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p> <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; });</p> <p>&#160;&#160;&#160; &lt;/script&gt;</p> <p>另外需要在业务里的各个依赖前都加上storeinc!如test.js:</p> <p>require(['storeinc!log','storeinc!c','storeinc!d'], function (log, modC, modD) {</p> <p>&#160;&#160;&#160; log.write('test3 run!!');</p> <p>&#160;&#160;&#160; log.write('module c\'s name is ' + modC.name);</p> <p>&#160;&#160;&#160; log.write('module d\'s name is ' + modD.name);</p> <p>});</p> <p>接下来看下效果:</p> <p>第一次我们把index.html里的ver项配置设为2,然后访问index.html,结果如下图所示:</p> <p><a href="http://static.oschina.net/uploads/img/201403/05120723_aWcl.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="wps_clip_image-29258" border="0" alt="wps_clip_image-29258" src="http://static.oschina.net/uploads/img/201403/05120723_WAR7.png" width="204" height="244" /></a></p> <p>第二次我们把index.html里的ver项配置设为3,说明这次需要的是版本为3的内容,结果如下图所示:</p> <p><a href="http://static.oschina.net/uploads/img/201403/05120724_WRLX.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="wps_clip_image-13606" border="0" alt="wps_clip_image-13606" src="http://static.oschina.net/uploads/img/201403/05120724_jMON.png" width="225" height="244" /></a></p> <p>说明访问的是增量文件,已经达到增量更新的目的</p> <p>&#160;</p> <p>&#160;</p> <p>rstoreinc github地址:<a title="https://github.com/luyongfugx/rstoreinc" href="https://github.com/luyongfugx/rstoreinc">https://github.com/luyongfugx/rstoreinc</a></p> <p>好用记得star哦:)</p> <p>另外说一下我的微博:</p> <p><a href="http://t.qq.com/waynelu"><font color="#000000">腾讯:</font>http://t.qq.com/waynelu</a></p> <p>新浪:<a title="http://weibo.com/u/1849616271" href="http://weibo.com/u/1849616271">http://weibo.com/u/1849616271</a></p>

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
12 收藏
0
分享
返回顶部
顶部