例子1

先是HTML页面

<!DOCTYPE html>
<html>
    <head>
        <title>My App</title>
        <link rel="stylesheet" type="text/css" href="css/main.css">
        <script data-main="scripts/main-built" src="scripts/require.js"></script>
    </head>
    <body>
        <h1>My App</h1>
    </body>
</html>

js都放在scripts目录下,换言之 html与scripts是平级。此目录下有r.js, require.js, one.js, two.js, three.js, main.js

上面HTML提到的main-built.js是一会儿动态生成的。

r.js 可以在这里找到https://github.com/jrburke/r.js/blob/master/dist/r.js,总共7MB,非常疯狂!听说把各种情况都hold住了, 比spm强大多了!

接着是几个JS的内容:

one.js

define(function(){
    return 1
})

two.js

define(function(){
    return 2
})

three.js

define(function(){
    return 3
})

main.js

require(["one", "two", "three"], function (one, two, three) {
    alert(one+two+three)
});

好了,我们再看一下build.js如何写

({
    baseUrl: ".",
    name: "main",
    out: "main-built.js"
})

定位到此目的,执行node r.js -o build.js

最后生成main-built.js文件,我格式化它一下,里面内容如下:

define("one",[],function(){return 1}),
define("two",[],function(){return 2}),
define("three",[],function(){return 3}),
require(["one","two","three"],function(e,t,n){alert(e+t+n)}),define("main",function(){});

最后运行服务器,发现真的能alert出6!

可以点击这里看看

例子2

就是在上面的例子里面改一下

其他变化如下:

three.js放到sub目录下,内容改成:

define(["./two"], function(a){
    return 1 + a
})

one.js

define(["./two"], function(a){
    return 1 + a
})

main.js改成

require(["one","sub/three"], function (one,  three) {
    console.log(one + three)
})

执行r.js,生成main-built.js为:

define("two", [], function() {
    return 2
}), define("one", ["./two"], function(e) {
    return 1 + e
}), define("sub/three", [], function() {
    return 30
}), require(["one", "sub/three"], function(e, t) {
    console.log(e + t)
}), define("main", function() {
});

下面合并前后的请求数比较

例子3, paths配置项的使用

目录结构改成这样,jquery自行到官网下载

main.js改成这样

require.config({
    paths: {
        jquery: "jquery/jquery-1.11.2"
    }
})
require(["one", "sub/three","jquery"], function(one, three, $) {
    console.log(one + three)
    console.log($)
});

main.js改成这样

({
    baseUrl: ".",
    name: "main",
    paths: {
        jquery: "jquery/jquery-1.11.2"
    },
    out: "main-built.js"
})

然后执行node r.js -o build.js打包命令,生成的文件就可以用了。并且发现r.js有个非常智能的地方,如果把main.js中的require语句的依赖项中的jquery去掉,再执行打包,它会将jquery源码从main-built.js中去掉。

可以点击这里看看

例子4, 让生产环境用的脚本放在另一个文件中

我们通常把我们自己工作的环境叫做发展环境, 上线的环境叫生产坏境,将它们分开是非常有好处的。我们把上面的目录复制一下,改一名字:

相对而言, 页面也改一下

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <title>My App</title>
        <link rel="stylesheet" type="text/css" href="css/main.css">
        <script data-main="develop/main-built" src="develop/avalon.js"></script>
        <!--    <script data-main="develop/main" src="develop/avalon.js"></script>-->
        <!--    <script data-main="develop/main-built" src="develop/require.js"></script>-->
    </head>
    <body>
        <h1>My App</h1>
    </body>
</html>

打包的脚本build.js变成这样,

({
    baseUrl: ".",
    paths: {
        jquery: "jquery/jquery-1.11.2"
    },
    dir: "../production",
    name: "main"
})

对比一下,有了dir,就不能使用out配置项了,你在编译时它有非常明确的提示。执行node r.js -o build.js打包命令,你的项目变成这样了

既然目录变了,我们有两个办法,1自己修改或通过脚本修改index.html引用脚本的路径,2后端配置一下,对请求进行重定向,我们通常是使用后面一种。

可以点击这里看看

例子5, shim配置项的使用

在例子4里面jquery目录添加一个jquery.aaa.js,内容如下:

jQuery.fn.aaa = function(){
    alert("aaa")
}

main.js改成

require.config({
    paths: {
        jquery: "jquery/jquery-1.11.2",
        aaa: "jquery/jquery.aaa"
    },
    shim: {
        aaa: {
            deps: ["jquery"],
            exports: "jQuery"
        }
    }
})
require(["one", "sub/three", "aaa"], function(one, three, $) {
    console.log(one + three)
    console.log($.fn.aaa)
})

build.js也跟着改成

require.config({
    paths: {
        jquery: "jquery/jquery-1.11.2",
        aaa: "jquery/jquery.aaa"
    },
    shim: {
        aaa: {
            deps: ["jquery"],
            exports: "jQuery"
        }
    }
})
require(["one", "sub/three", "aaa"], function(one, three, $) {
    console.log(one + three)
    console.log($.fn.aaa)
})

然后执行node r.js -o build.js打包命令,生成的文件就可以用了。

可以点击这里看看

如果大家还有更好的打包方式, 可以https://github.com/avalonjs/avalonjs.github.io/tree/master/zh/engineering,添加到这里,pull request给我

转载来源:http://www.cnblogs.com/rubylouvre/p/4262569.html

转载 r.js打包经验的更多相关文章

  1. requirejs 使用实例r.js打包

    在这里,请先看基础文章与相关技术文档: 安装: npm init npm install requirejs --save npm install jquery@1.11.1 --save 创建基本目 ...

  2. r.js打包注意事项 r.js打包 这个是配合require.js打包的

    这个./代表的是当前文件的父目录....打包的资源一定要在这个父目录中下面才行,,,,一定一定,要放在这个目录一下才能被正确找到. 不然只是copy了一份一模一样的文件夹和文件过去,并不会处理压缩啥的 ...

  3. r.js打包

    久闻r.js的大名,但实际没有用它做过任何东西.今天用它时,发现网上许多教程都不对.研究一下,把我的实际经验分享给大家. 例子1 先是HTML页面 <!DOCTYPE html> < ...

  4. requirejs r.js 打包报错paths fallback not supported in optimizer please provide a build config path override for jquery

    错误原因: 改为:

  5. requirejs原理深究以及r.js和gulp的打包【转】

    转自:http://blog.csdn.net/why_fly/article/details/75088378 requirejs原理 requirejs的用法和原理分析:https://githu ...

  6. requirejs 多页面,多js 打包代码,requirejs多对多打包【收藏】

    这段代码来自 http://stackoverflow.com/questions/20583812/grunt-requirejs-optimizer-for-a-multi-app-project ...

  7. requirejs 多页面,多js 打包代码,requirejs多对多打包

    这段代码来自 http://stackoverflow.com/questions/20583812/grunt-requirejs-optimizer-for-a-multi-app-project ...

  8. requireJS的优化工具 ---- r.js

    requireJS是javascript的模块加载器,是基于AMD规范实现的. r.js是其提供的对模块进行打包和构建的一个工具 下载 r.js 创建r.js 的配置文件 build.js build ...

  9. JavaScript模块化-require.js,r.js和打包发布

    在JavaScript模块化和闭包和JavaScript-Module-Pattern-In-Depth这两篇文章中,提到了模块化的基本思想,但是在实际项目中模块化和项目人员的分工,组建化开发,打包发 ...

随机推荐

  1. python 中time模块使用

    在开始之前,首先要说明这几点: 1.在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素.由于Python的time模块实现主 ...

  2. 给UILabel设置不同的字体和颜色

    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAt ...

  3. jquery学习笔记-----插件开发的编写总结

    一.对jQuery对象的扩展 ;(function($){ $.fn.extend(  { fun1:abc,fun2:1bc … } ) })(jQuery) 这里采用立即执行模式,即不用调用也能执 ...

  4. 使用HttpWebRequest发送自定义POST请求

    平时用浏览器看网页的时候,点击一下submit按钮的时候其实就是给服务器发送了一个POST请求.但是如何在自己的C#程序里面实现类似的功能呢?本文给出了一个简单的范例,可以实现类似的和web serv ...

  5. Delphi编程建议遵守的规范2---命名规范

    1.1.形参命名建议 所有形参的名称都应当表达出它的用途.如果合适的话,形参的名称最好以字母a 为前缀,例如: procedure SomeProc(aUserName:string; aUserAg ...

  6. x264 - 高品质 H.264 编码器

    转自:http://www.5i01.cn/topicdetail.php?f=510&t=3735840&r=18&last=48592660 H.264 / MPEG-4 ...

  7. android selector(转)

    Selector的结构描述: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:a ...

  8. 传引用 C(转)

    转自:http://myturn.blog.hexun.com/15584978_d.html #include <iostream> using namespace std ; void ...

  9. Cygwin的安装,卸载,以及安装gdb

    转载来源 http://10000001.blog.51cto.com/4600383/1341484   1.安装 其实Cygwin的安装时很简单的,需要的安装相应的就可以了,要详细的去网上找,很多 ...

  10. flex容器属性(一)

    一,概念 flexible box ,意为"弹性布局",用来为盒状模型提供最大的灵活性. 块级布局更侧重于垂直方向,行内布局更侧重于水平方向,于此相对的,弹性盒子布局算法是方向无关 ...