使用r.js進行前端repuirejs的合併壓縮

安裝

requirejs

npm install -g requirejs

 

安裝好後:node

  • 找到剛剛requirejs的安裝目錄,在該目錄下找到r.js,並拷貝待壓縮合並項目的根目錄下
  • 在項目根目錄下建立build.js

build.js 示例

{
    //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

相關文章
相關標籤/搜索