Maison > développement back-end > tutoriel php > 自己动手,锦衣玉食

自己动手,锦衣玉食

WBOY
Libérer: 2016-06-13 12:11:39
original
901 Les gens l'ont consulté

自己动手,丰衣足食.

时下grunt非常的火啊,用着虽然很爽,但是它的配置确实很烦。如果之前没有用过,想要一下子熟练驾驭它,有一定的学习成本,而且还要装node这个大家伙,项目之初我们选择了compiler.jar这个轻量的工具进行打包。我一直在寻思着,如何编写一键打包工具。之前呢是手工的拼接好有的js文件,做成符合compiler.jar打包文件所要求的批处理文件,然后运行这个批处理,生成我们需要的js和css文件。随着js文件数量的增长,纯手工拼接这些文件的地址就变得非常考验人的耐心了,而且还容易漏掉或重复某些文件,于是“一键打包工具”的编写就变得刻不容缓了。下面是一个真实的index.html文件的一部分:

<span style="font-family: 'Microsoft YaHei';"><span style="color: #0000ff;"><span style="color: #ff00ff;">DOCTYPE html</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"><span style="color: #800000;">html</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">meta </span><span style="color: #ff0000;">charset</span><span style="color: #0000ff;">="utf-8"</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">meta </span><span style="color: #ff0000;">http-equiv</span><span style="color: #0000ff;">="X-UA-Compatible"</span><span style="color: #ff0000;"> content</span><span style="color: #0000ff;">="IE=edge,chrome=1"</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">meta </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="viewport"</span><span style="color: #ff0000;"> content</span><span style="color: #0000ff;">="target-densitydpi=device-dpi, initial-scale=1, user-scalable=0, maximum-scale=1"</span><span style="color: #0000ff;">></span>     <span style="color: #008000;"><!--</span><span style="color: #008000;">隐藏浏览器的工具栏和菜单栏,对iso系统起用</span><span style="color: #008000;">--></span>    <span style="color: #008000;"><!--用于<strong>PC上</strong>调式,不参与合并压缩</span><span style="color: #008000;">--></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/data/database.js"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="noBuild"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #008000;"><!--</span><span style="color: #008000;">Iframe加载处理</span><span style="color: #008000;">--></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/LoadMode.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/core/jQuery.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/core/underscore.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/animate/SVGIcons/snap.min.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/animate/pixi.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/core/Xut.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/core/isMobile.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/core/aaronRequire.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/core/nextTick.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/Config.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/core/lang/Object.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/core/lang/Function.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/core/lang/Array.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/core/video.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #008000;"><!--</span><span style="color: #008000;"> 自定义事件,合集处理,iframe通讯 </span><span style="color: #008000;">--></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/core/event/asEvented.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/core/message/pms.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #008000;"><!--</span><span style="color: #008000;">插件</span><span style="color: #008000;">--></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/plugin/cordova.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/plugin/readAssetsFilePlugin.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/plugin/initDatabase.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/plugin/web.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/plugin/video.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/plugin/openAppPlugin.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/plugin/tabletPlugin.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/plugin/statusbar.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/plugin/iap.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/plugin/AppStoreLink.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/plugin/downloadPlugin.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/plugin/xxteManager.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/plugin/unzipPlugin.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/plugin/readPlugin.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/plugin/deletePlugin.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #008000;"><!--</span><span style="color: #008000;"> 动画库 </span><span style="color: #008000;">--></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/animate/TweenMax.min.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/animate/plugins/ThrowPropsPlugin.min.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/animate/PptAnimation.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/animate/CanvasAnimation.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/animate/dragdrop/Draggable.min.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/animate/dragdrop/dragdrop.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/animate/iscroll.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/animate/hammer.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/animate/SVGIcons/svgicons-config.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/animate/SVGIcons/svgicons.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/animate/SpriteA.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/util/Utils.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/util/LocalStorage.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/util/ScriptLoad.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/util/ExecuteSql.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/util/PromptNotice.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/util/edge.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #008000;"><!--</span><span style="color: #008000;"> 配置文件,数据文件,结构文件 </span><span style="color: #008000;">--></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/data/Store.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/data/StoreManager.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #008000;"><!--</span><span style="color: #008000;"> 数据初始化 </span><span style="color: #008000;">--></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/Main.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/Initialize.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/scenario/SceneLayout.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/scenario/SceneFactory.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/scenario/SceneController.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/LoadScene.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/Dispatcher.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #008000;"><!--</span><span style="color: #008000;"> 工具栏 </span><span style="color: #008000;">--></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/toolbar/Navbar.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/toolbar/sToolbar.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/toolbar/fToolbar.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/toolbar/searchBar.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/toolbar/bookMark.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #008000;"><!--</span><span style="color: #008000;"> 多线程任务片 </span><span style="color: #008000;">--></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/threadTask/Buffer.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/threadTask/TaskContents.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/threadTask/TaskComponents.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/threadTask/TaskBackground.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/threadTask/TaskContainer.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/pageBase/Parser.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/pageBase/Collection.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/pageBase/MultiEvent.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/pageBase/PageBase.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/pageBase/Page.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/pageBase/Master.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #008000;"><!--</span><span style="color: #008000;"> 页面管理模块 </span><span style="color: #008000;">--></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/controller/transform/Translation.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/controller/OverrideApi.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/controller/Abstract.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/controller/Emitter.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/controller/PageMgr.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/controller/MasterMgr.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/controller/Compiler.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/controller/ViewModel.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/controller/SwitchPage.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/controller/EventDrive.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #008000;"><!--</span><span style="color: #008000;">热点管理</span><span style="color: #008000;">--></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/scheduler/AssignAutoRun.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/scheduler/AssignTrigger.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/scheduler/AssignSuspend.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/scheduler/AssignOriginal.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/scheduler/AssignRecovery.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/scheduler/ProcessControl.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/scheduler/Binding.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #008000;"><!--</span><span style="color: #008000;"> 适配器,用于处理热点 </span><span style="color: #008000;">--></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/directives/dir-Content.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/directives/dir-Widget.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/directives/dir-Media.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/directives/dir-Action.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/directives/dir-ShowNote.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>     <span style="color: #008000;"><!--</span><span style="color: #008000;">多媒体对象 </span><span style="color: #008000;">--></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/component/media/Audio.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/component/media/Video.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/component/media/AudioManager.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/component/media/VideoManager.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #008000;"><!--</span><span style="color: #008000;">文本热点</span><span style="color: #008000;">--></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/component/content/conFilter.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="lib/component/content/conAlgorithm.js"</span><span style="color: #0000ff;">></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span><br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
Copier après la connexion

后面还有很长,bug,活人不能被尿憋死,办法总比问题多。我是会一点php的,php在处理文件方面是很拿手的,是时候让它发挥点作用了。于是我想到了用php去自动提出index里边的js和css,然后按指定的格式生成批处理文件,在后台用静默方式运行这个批处理,最后把结果返回给显示器。这样我就可以坐享其成了。想想都有点小激动哟,于是简单的写了一个界面.

 

接下来就是实现功能了,先不着急编写代码,分析下需求:

1. 遍历index.html文件,提取js文件或css文件.

2. 对这文件进行过虑,因为有些是注释掉的,有些是调式用的。

3. 生成对应的批处理文件

4. 执行批处理文件

5. 显示处理结果

即然有两种情况,我就用一个工厂模式来适配,方便以后扩展其它类型,目光要放长远一点。即然工厂都有了,那索性再来一个接口,约定都必需要实现“接收请求”和“输出结果”这两个接口。再想想处理js和css都可能会有相同的功能,让它们继承一个父类可以使代码复用,于是继承也先用上。目前的设计应当可以满足我的要求了,于是开始编写php代码.

<span style="color: #000000;">php</span><span style="color: #008080;">header</span>("Content-type: text/html; charset=utf-8"<span style="color: #000000;">);</span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 根据index.html文件中引用的js,生成compressJs.bat * @author frog  * @date 2014-11-17 </span><span style="color: #008000;">*/</span><span style="color: #0000ff;">interface</span><span style="color: #000000;"> ICompress {    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 处理用户请求     * @return [type] [description]     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> request();    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 处理输出结果     * @return [type] [description]     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> render();}</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> BaseCompress {    </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$content</span><span style="color: #000000;">;    </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$outPath</span><span style="color: #000000;">;    </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$isAuto</span><span style="color: #000000;">;    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$isAuto</span>=<span style="color: #0000ff;">false</span><span style="color: #000000;">){        </span><span style="color: #800080;">$outPath</span> = '_file'<span style="color: #000000;">;        </span><span style="color: #0000ff;">if</span>(!<span style="color: #008080;">is_dir</span>(<span style="color: #800080;">$outPath</span><span style="color: #000000;">)){            </span><span style="color: #008080;">mkdir</span>(<span style="color: #800080;">$outPath</span><span style="color: #000000;">);        }        </span><span style="color: #800080;">$this</span>->outPath = <span style="color: #800080;">$outPath</span><span style="color: #000000;">;        </span><span style="color: #800080;">$this</span>->isAuto = <span style="color: #800080;">$isAuto</span><span style="color: #000000;">;    }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 运行批处理     * @return [type] [description]     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> runBat(<span style="color: #800080;">$name</span><span style="color: #000000;">){    }}</span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 压缩javascript文件 * 合并javascript文件 </span><span style="color: #008000;">*/</span><span style="color: #0000ff;">class</span> CompressJS <span style="color: #0000ff;">extends</span> BaseCompress <span style="color: #0000ff;">implements</span><span style="color: #000000;"> ICompress {    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 处理用户请求     * @return [type] [description]     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> request(){    }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 处理输出结果     * @return [type] [description]     </span><span style="color: #008000;">*/</span>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> render(){}}</span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 压缩样式文件 </span><span style="color: #008000;">*/</span><span style="color: #0000ff;">class</span> CompressCSS <span style="color: #0000ff;">extends</span> BaseCompress <span style="color: #0000ff;">implements</span><span style="color: #000000;"> ICompress {    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> request(){     }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 处理输出结果     * @return [type] [description]     </span><span style="color: #008000;">*/</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> render(){}     }</span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 工厂类 </span><span style="color: #008000;">*/</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Factory {    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> create(<span style="color: #800080;">$type</span>,<span style="color: #800080;">$isAuto</span><span style="color: #000000;">){        </span><span style="color: #800080;">$ob</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">;        </span><span style="color: #0000ff;">switch</span> (<span style="color: #800080;">$type</span><span style="color: #000000;">) {            </span><span style="color: #0000ff;">case</span> 'js':                <span style="color: #800080;">$ob</span> = <span style="color: #0000ff;">new</span> CompressJS(<span style="color: #800080;">$isAuto</span><span style="color: #000000;">);                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">case</span> 'css':                <span style="color: #800080;">$ob</span> = <span style="color: #0000ff;">new</span> CompressCSS(<span style="color: #800080;">$isAuto</span><span style="color: #000000;">);                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">default</span>:                <span style="color: #008000;">#</span><span style="color: #008000;"> code...</span>                <span style="color: #0000ff;">break</span><span style="color: #000000;">;        }        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$ob</span><span style="color: #000000;">;    }}</span>
Copier après la connexion

然后是调用处理:

<span style="color: #008000;">//</span><span style="color: #008000;">处理ajax请求</span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_POST</span>['submit'<span style="color: #000000;">])){    </span><span style="color: #008000;">//</span><span style="color: #008000;">是否自动执行批处理</span>    <span style="color: #800080;">$isAuto</span> = <span style="color: #800080;">$_POST</span>['zip'] === 'true' ? <span style="color: #0000ff;">true</span> : <span style="color: #0000ff;">false</span><span style="color: #000000;">;    </span><span style="color: #008000;">//</span><span style="color: #008000;">处理类型</span>    <span style="color: #800080;">$type</span> = <span style="color: #800080;">$_POST</span>['type'<span style="color: #000000;">];    </span><span style="color: #800080;">$c</span> = Factory::create(<span style="color: #800080;">$type</span>,<span style="color: #800080;">$isAuto</span><span style="color: #000000;">);<br>    //接口方法    </span><span style="color: #800080;">$c</span>-><span style="color: #000000;">request();    </span><span style="color: #800080;">$c</span>-><span style="color: #000000;">render();}</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{    </span><span style="color: #0000ff;">echo</span> '请使用静态页访问本程序:<a href="index.html">点此进入</a>'<span style="color: #000000;">;}</span>
Copier après la connexion

大致的骨架就出来了。具体的填码过程就比较简单了。稍微有点难度的就是执行批处理这个一方法.

是网上提供的方法,以后可能用的上,这里特意贴一下:

<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> runBat(<span style="color: #800080;">$name</span><span style="color: #000000;">){        </span><span style="color: #0000ff;">if</span>(!<span style="color: #008080;">file_exists</span>(<span style="color: #800080;">$name</span>) || !<span style="color: #800080;">$this</span>-><span style="color: #000000;">isAuto){            </span><span style="color: #0000ff;">return</span><span style="color: #000000;">;        }        </span><span style="color: #008000;">//</span><span style="color: #008000;">转入后台处理</span>        @<span style="color: #008080;">exec</span>(<span style="color: #008080;">pclose</span>(<span style="color: #008080;">popen</span>("start /B ". <span style="color: #008080;">escapeshellcmd</span>(<span style="color: #800080;">$name</span>), "r"<span style="color: #000000;">)));    }</span>
Copier après la connexion

在前面的index.html中,有一个name="noBuild"这个是我人为添加的,这是因为我要过滤这种标识的js文件,这样以后要过滤别的文件,也只要添加这个标识就可以了,不用改php代码。由于是内部使用,没有做表单项来指定工程的路径,默认就是index.html所处的上级目录即为工程目录。这样设计简化了操作,提高了效率。

关于遍历特定的文件,我推荐glob函数,很简洁的方案:

<span style="color: #800080;">$files</span> = <span style="color: #008080;">glob</span>(<span style="color: #800080;">$path</span>.'/*.css',GLOB_NOSORT);
Copier après la connexion

下面是工程目录结构图:

再来一张运行效果图:

  

最后是处理结果图:

终于不用写grunt的配置和安装node这个家伙了,再也不担心更新的时候,从svn上拉下来一堆node的东西,是时候和它们说再见了:

好像忘了展示前端代码了:

<span style="color: #008000;">/*</span><span style="color: #008000;">* * 选项卡类 * @param {string} id 选项卡的ID </span><span style="color: #008000;">*/</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> Tabs(id){    </span><span style="color: #0000ff;">var</span> node = document.querySelector('#'+<span style="color: #000000;">id);    </span><span style="color: #0000ff;">var</span> selected = node.querySelector('.selected'<span style="color: #000000;">);    </span><span style="color: #0000ff;">this</span>.selected =<span style="color: #000000;"> selected;    </span><span style="color: #0000ff;">this</span>.node =<span style="color: #000000;"> node;    </span><span style="color: #0000ff;">this</span><span style="color: #000000;">.bindEvent();}</span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 切换选项卡 * @param  {object} event 事件 * @return {[type]}   [description] </span><span style="color: #008000;">*/</span><span style="color: #000000;">Tabs.prototype.change </span>= <span style="color: #0000ff;">function</span><span style="color: #000000;">(event){    </span><span style="color: #0000ff;">var</span> element =<span style="color: #000000;"> event.target;    </span><span style="color: #0000ff;">if</span>(element.tagName.toLowerCase()=='li'<span style="color: #000000;">){        </span><span style="color: #0000ff;">if</span>(element.className=='selected'<span style="color: #000000;">){            </span><span style="color: #0000ff;">return</span><span style="color: #000000;">;        }    }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{        </span><span style="color: #0000ff;">while</span>(element != <span style="color: #0000ff;">this</span><span style="color: #000000;">.node){            element </span>=<span style="color: #000000;"> element.parentNode;            </span><span style="color: #0000ff;">if</span>(element.tagName.toLowerCase()=='li'<span style="color: #000000;">){                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;            }        }        </span><span style="color: #0000ff;">if</span>(element == <span style="color: #0000ff;">this</span>.node) <span style="color: #0000ff;">return</span><span style="color: #000000;">;    }    </span><span style="color: #0000ff;">this</span>.selected.removeAttribute('class'<span style="color: #000000;">);    element.className </span>= 'selected'<span style="color: #000000;">;    </span><span style="color: #0000ff;">this</span>.selected =<span style="color: #000000;"> element;    </span><span style="color: #0000ff;">this</span><span style="color: #000000;">.content();}Tabs.prototype.content </span>= <span style="color: #0000ff;">function</span><span style="color: #000000;">(){  </span><span style="color: #0000ff;">var</span> form =<span style="color: #000000;"> document.form1;    </span><span style="color: #0000ff;">switch</span>(<span style="color: #0000ff;">this</span><span style="color: #000000;">.getTabType()){      </span><span style="color: #0000ff;">case</span> 'js'<span style="color: #000000;">:        form.style.display </span>= 'block'<span style="color: #000000;">;        form.children[</span>0].innerHTML = '自动压缩JS'<span style="color: #000000;">;        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;      </span><span style="color: #0000ff;">case</span> 'css'<span style="color: #000000;">:        form.style.display </span>= 'block'<span style="color: #000000;">;        form.children[</span>0].innerHTML = '自动压缩CSS'<span style="color: #000000;">;        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;      </span><span style="color: #0000ff;">default</span><span style="color: #000000;">:    }}</span>
Copier après la connexion

 

会前端和后端,就是这么任性。轻轻一点,告别烦恼!造自己的工具,让别人去苦逼吧,So easy!

 

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal