npm install -g requirejs
安裝好後:node
{ //The directory path to save the output. //All relative paths are relative to the build file. dir: "./build", //All modules are located relative to this path baseUrl: "./src", //the main script of the project which contain the configuration for the app //if set, all config below is not neccesory mainConfigFile: './src/js/app.js', //module you want to optimize(the entry file which require modules) //path relative to the baseUrl name: "js/app/rtest", //out: 'js/app/build.js', //used in optimize one file //fileExclusionRegExp: /^(app)\.js$/, //file which will be skipped paths: { //path of files 'lodash': 'js/lib/lodash/dist/lodash', 'jquery': 'js/lib/jquery/dist/jquery.min', 'when': 'js/lib/when/when' }, shim: { 'jquery': { exports: '$' } } }
doc of build.jsjquery
node r.js -o build.js
你會看到:build文件夾被建立,同步引入的文件被合併到入口文件中,異步文件保留。目錄下的全部文件都被壓縮。git
GitHub地址:requirejs-optimizegithub
參考資料: r.js 優化 RequireJS 項目(合併與壓縮)npm