依據require實現的簡易的前端模塊化框架。若是你不想花時間學習require.js,也不想翻看長篇的cmd/amd規範,那麼這個mini-define就是你不錯的選擇。若是你以前用過sea.js或require.js那麼mini-define更加高效,更加輕量,更加易用。javascript
項目地址:html
define.js: https://github.com/bjtqti/study/tree/master/define前端
require.js https://github.com/bjtqti/study/tree/master/requirejava
首先定義模塊git
定義模塊 github
1.1 根據是否有依賴,有兩種狀況: 1.1.1:沒有依賴的模塊
define('id',function(){ // put your code here });
1.1.2:有依賴的模塊
define('id',['modeA','modeB'],function(A,B){ // put your code here });
1.2 根據是否須要返回處理結果給外部使用,又能夠分兩種狀況: 1.2.1有返回對象:
define('id',function(){ return { // put your code here } });
1.2.2 沒有返回對象
define('id',function(){ // put your code here });
2.1 根據請求的模塊數,能夠有兩狀況: 2.1.1.調用單個模塊 require('modeId') 2.1.2.調用多個模塊
require(['modeA','modeB']);
2.2 根據是否有回調處理,又能夠分爲兩種狀況: 2.2.1 有回調處理函數
require('modeId',function(mode){ //put your code here }); require(['modeA','modeB'],function(A,B){ //put your code here });
2.2.2 沒有回調處理
require('modeId');
而後在index.html頁面依次引用所需模塊框架
<!--核心模塊-->
<script src="lib/core/require.js"></script> <!--用於演示的模塊--> <script src="lib/main.js"></script> <script src="lib/config.js"></script> <script src="lib/init.js"></script>
最後就是用你喜歡的方式對lib目錄進行合併壓縮,生成一個min.js文件。 在發佈應用的時候,相應的index.html也須要調整一下:模塊化
<script src="lib/min.js"></script>