使用r.js进行前端repuirejs的合并压缩

安装

requirejs

npm install -g requirejs

 

安装好后:

  • 找到刚刚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.js

运行

node r.js -o build.js

 

你会看到:build文件夹被创建,同步引入的文件被合并到入口文件中,异步文件保留。目录下的所有文件都被压缩。

GitHub地址:requirejs-optimize

 

参考资料: r.js 优化 RequireJS 项目(合并与压缩)

你可能感兴趣的:(js)