requireJS,rjs,gulp簡易實現

clipboard.png

gulpfile.jshtml

var gulp = require("gulp");
var rjs = require("requirejs");
gulp.task('build', function(cb){
  rjs.optimize({
    //文件輸出路徑  
    dir: './vajoy',
    //定義入口文件路徑
    baseUrl: "./rjs",
    //單入口
    //mainConfigFile:"./rjs/main.js",
    //name:'main',
    //輸出路徑
    //out:'./rjs/dist/build-main.js',
    modules: [                    //要優化的模塊
      { name:'main'}            //說白了就是各頁面的入口文件,相對baseUrl的路徑,也是省略後綴「.js」
    ],
    optimize: "uglify",
    //相對baseUrl的路徑,定義依賴模塊的路徑
    paths: {    
            a: './js/a',
            jquery: './js/jquery',
    }
  }, function(buildResponse){
    // console.log('build response', buildResponse);
    cb();
  }, cb);

//運行 gulp build

main.jsjquery

// JavaScript Document
require.config({
    baseUrl: "js",
    paths: {
      "jquery":"jquery",
           "a":"a"
    }
  });
require(['jquery','a'],function($,a){
    $("#btn").on("click",function(){
        a();
        })
    })

htmlgulp

<!DOCTYPE HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>requireJS</title>
 <script src="require.js" defer async data-main="./main.js" ></script>
</head>

<body>
<input type="button" value="點擊" id="btn">
</body>
</html>

參考文章
http://www.tuicool.com/articl... r.js
http://www.cnblogs.com/2050/p... gulp
http://m.blog.csdn.net/articl... require.js文件合併async

相關文章
相關標籤/搜索