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

本文介绍如何使用RequireJS优化前端项目,包括安装、配置r.js,创建build文件进行模块合并及压缩,实现同步文件合并和异步文件保留。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装

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 项目(合并与压缩)

转载于:https://www.cnblogs.com/asie-huang/p/4362671.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值