標籤:
上面release是執行命令 node r.js -o build.js 產生的,需要切換到目錄require/tools下面,也就是 有r.js和build.js的目錄,才能執行命令
代碼目錄如上:
main.html代碼如下:
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>Hello RequireJS</title> </head> <body> <h1>Hello RequireJS</h1> <script type="text/javascript" src="js/lib/require.js"></script> <!-- main.js:requireJS 配置資訊 --> <script type="text/javascript" src="js/config.js"></script> <!-- <script type="text/javascript" src="js/combine.js"></script> --> <script type="text/javascript">
/*如果沒有配置資訊,載入one,two模組,要找到他們的存放路徑*/ /* require(["./js/mod/one", "./js/mod/two"], function (one, two) { console.log(‘載入模組:‘, one.name, two.name ); console.log("bootstrap the application"); });*/ require(["one", "two"], function (one, two) { console.log(‘載入模組:‘, one.name, two.name ); console.log("bootstrap the application"); }); </script> </body></html>
config.js代碼如下,配置資訊,便於 require引入這些模組 one,two,three,如上代碼
// requireJS的簡單配置,更詳細的配置資訊請看 http://requirejs.org/docs/api.html#configrequirejs.config({ baseUrl: "./js", //相對於當前 Html的路徑 paths: { one: "mod/one", two: "mod/two", three: "mod/three" }});
one模組代碼:
define(function(){ return {name:"one"}});
two模組代碼:其中two模組引入了three模組
define(function(require){ /*var three = require(‘./three‘);*/ var three = require(‘three‘); console.log(‘載入模組:‘+three.name); return {name:"two"}});
three模組代碼:
define(function(){ return {name:"three"}});
build.js是r.js壓縮代碼的依據,代碼如下:
({ appDir:"../src", dir:"../release", mainConfigFile:"../src/js/config.js", paths:{ one:"mod/one", two:"mod/two", three:"mod/three" }, modules:[{ name:"combine", include:[ ‘one‘, ‘two‘ ] }], optimize:"uglify" })
appDir
應用程式的最頂層目錄。可選的,如果設定了的話,r.js 會認為指令碼在這個路徑的子目錄中,應用程式的檔案都會被拷貝到輸出目錄(dir 定義的路徑)。如果不設定,則使用下面的 baseUrl 路徑。
baseUrl
預設情況下,所有的模組都是相對於這個路徑的。如果沒有設定,則模組的載入是相對於 build 檔案所在的目錄。另外,如果設定了appDir,那麼 baseUrl 應該定義為相對於 appDir 的路徑。
dir
輸出目錄的路徑。如果不設定,則預設為和 build 檔案同級的 build 目錄。
optimize
JavaScript 代碼最佳化方式。可設定的值:
- "uglify:使用 UglifyJS 壓縮代碼,預設值;
- "uglify2":使用 2.1.2+ 版本進行壓縮;
- "closure": 使用 Google‘s Closure Compiler 進行壓縮合并,需要 Java 環境;
- "closure.keepLines":使用 Closure Compiler 進行壓縮合并並保留換行;
- "none":不做壓縮合并;
optimizeCss
CSS 代碼最佳化方式,可選的值有:
- "standard":標準的壓縮方式;
- "standard.keepLines":保留換行;
- "standard.keepComments":保留注釋;
- "standard.keepComments.keepLines":保留換行;
- "none":不壓縮;
mainConfigFile
如果不想重複定義的話,可以使用這個參數配置 RequireJS 的設定檔路徑。
removeCombined
刪除之前壓縮合并的檔案,預設值 false。
fileExclusionRegExp
要排除的檔案的正則匹配的運算式。
modules
定義要被最佳化的模組數組。每一項是模組最佳化的配置,常用的幾個參數如下:
name:模組名;
create:如果不存在,是否建立。預設 false;
include:額外引入的模組,和 name 定義的模組一起壓縮合并;
exclude:要排除的模組。有些模組有公用的相依模組,在合并的時候每個都會壓縮排去,例如一些基礎庫。使用 exclude 就可以把這些模組在壓縮在一個更早之前載入的模組中,其它模組不用重複引入。
代碼目錄如下
main-build.js,production都是運行 node r.js -o build.js產生的,需要切換到目錄test/develop下面,也就是 有r.js和build.js的目錄,才能執行命令
<!DOCTYPE html><html> <head> <title>My App</title> <!-- <link rel="stylesheet" type="text/css" href="css/main.css"> --> <script data-main="main-build" src="require.js"></script> </head> <body> <h1>My App</h1> </body></html>
data-main屬性的作用是,指定網頁程式的主模組。在上例中,就是js目錄下面的main.js,這個檔案會第一個被require.js載入。由於require.js預設的檔案尾碼名是js,所以可以把main.js簡寫成main。
build.js 如下:out是壓縮為一個.js檔案,dir是將本地代碼,重新複製一份代碼,作為上線代碼,out和dir不能同時出現,同時使用會報衝突。
({ baseUrl:‘.‘, name:‘main‘, paths:{jquery:‘jquery/jquery-1.11.0.min‘}, out:‘main-build.js‘ /*dir:‘../production‘*/})
main.js 配置:方便require載入模組
理論上,require.js載入的模組,必須是按照AMD規範、用define()函數定義的模組。但是實際上,雖然已經有一部分流行的函數庫(比如jQuery)符合AMD規範,更多的庫並不符合。那麼,require.js是否能夠載入非規範的模組呢?
回答是可以的。
這樣的模組在用require()載入之前,要先用require.config()方法,定義它們的一些特徵。
舉例來說,underscore和backbone這兩個庫,都沒有採用AMD規範編寫。如果要載入它們的話,必須先定義它們的特徵。
require.config({ paths:{ jquery:‘jquery/jquery-1.11.0.min‘ },
shim: {
}
})require([‘one‘,‘sub/three‘,‘jquery‘],function(one,three){ alert(one+three);});
requireJs和r.js壓縮公用程式