http://www.sitepoint.com/require-js-setup-time-2-minutes/

Setup Require.js in just 2 minutes. or download the code below and have it already working.

I’ve added some better screenshots below of require.js in action.

View project on GitHub

What is Require.js?

RequireJS is a JavaScript file and module loader. It is optimized for in-browser use, but it can be used in other JavaScript environments, like Rhino and Node. Using a modular script loader like RequireJS will improve the speed and quality of your code.

  • Speed – Asynchronous JavaScript Loading.
  • Manage JavaScript dependencies such as jQuery plugins.
  • File Structure your web app files.
  • When you get good you can code modules which do specific web app stuff.
  • Removes the need for including 100 script tags in your HTML.
  • Can be easily integrate with build scripts.

Does it work?

Yes. Screenshot below was taken from my dev with chrome dev tools open (disabling cache) so is naturally fast but amazingly even here you can see a performance increase.

Web App Scructure

This is a very basic structure you can use for a web app.

  • root/

    • index.html
    • js
      • vendor

        • [External JavaScript Files & jQuery Plugins]
      • app
        • main.js
        • [your modules and web app JavaScript files]
      • app.js
    • css
    • img

HTML Before:

The normal way to load scripts… modernizr goes in the head, the rest in the body.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
< !DOCTYPE html>
<head>
    <title>My Web App</title>
    <link rel="stylesheet" href="app/css/main.css"/>
    <script src="app/js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script>
</head>
<body>
 
    <div id="main" class="container"></div>
 
    <script src="app/js/vendor/jquery-1.9.1.min.js"></script>
    <script src="app/js/vendor/underscore.min.js"></script>
    <script src="app/js/vendor/backbone.min.js"></script>
    <script src="app/js/vendor/bootstrap.min.js"></script>
    <script src="app/js/main.js"></script>
</body>

HTML After:

Require.js goes in the head. Nice and neat.

1
2
3
4
5
6
7
8
9
10
11
< !DOCTYPE html>
<head>
    <title>My Web App</title>
    <link rel="stylesheet" href="app/css/main.css"/>
    <script data-main="js/app" src="js/vendor/require.js"></script>
</head>
<body>
 
    <div id="main" class="container"></div>
 
</body>

app.js

This file contains the config for require.js. If you change the directory structure this needs to match. I’m showing you the shim version, you can also load jQuery from a CDN.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Place third party dependencies in the lib folder
//
// Configure loading modules from the lib directory,
requirejs.config({
    "baseUrl": "js/vendor",
    "paths": {
      "app": "../app"
    },
    "shim": {
        "backbone": ["jquery", "underscore"],
        "bootstrap": ["jquery"]
    }
});
 
// Load the main app module to start the app
requirejs(["app/main"]);

main.js

This file contains the web app dependencies and once loaded you can start your app using whatever framework you like such as Backbone or Angular.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Load Web App JavaScript Dependencies/Plugins
define([
    "jquery",
    "modernizr",
    "underscore",
    "backbone",
    "bootstrap"
], function($)
{
    $(function()
    {
 
        //do stuff
        console.log('required plugins loaded...');
 
    });
});

Still can’t get it working?

Download Code

[转载]Require.js Example – Setup Time 2 Minutes的更多相关文章

  1. require.js学习笔记(内容属于转载总结)

    <script data-main="src/app" src="src/lib/require.js"></script> backb ...

  2. 转载 r.js打包经验

    例子1 先是HTML页面 <!DOCTYPE html> <html>     <head>         <title>My App</tit ...

  3. Javascript模块化编程(三):require.js的用法 作者: 阮一峰

    声明:转载自阮一峰的网络日志 这个系列的第一部分和第二部分,介绍了Javascript模块原型和理论概念,今天介绍如何将它们用于实战. 我采用的是一个非常流行的库require.js. 一.为什么要用 ...

  4. js模块化开发——require.js的用法详细介绍(含jsonp)

    RequireJS的目标是鼓励代码的模块化,它使用了不同于传统<script>标签脚本加载步骤.可以用它回事.优化代码,但其主要的目的还是为了代码的模块化.它鼓励在使用脚本以moudle ...

  5. require.js模块化写法

    模块化 模块就是实现特定功能的一组方法.只要把不同的函数(以及记录状态的变量)简单地放在一起,就算是一个模块. 下述两种写法等价 exports 对象是当前模块的导出对象,用于导出模块公有方法和属性. ...

  6. javascript模块化编程库require.js的用法

    随着javascript的兴起,越来越多的公司开始将JS模块化,以增加开发的效率和减少重复编写代码的.更是为了能更加容易的维护日后的代码,因为现在的随着人们对交互效果的越来越强烈的需求,我们的JS代码 ...

  7. jQuery 对AMD的支持(Require.js中如何使用jQuery)

    AMD 模块 AMD(异步模块定义,Asynchronous Module Definition)格式总体的目标是为现在的开发者提供一个可用的模块化 JavaScript 的解决方案. AMD 模块格 ...

  8. require.js 简洁入门

    原文地址:http://blog.sae.sina.com.cn/archives/4382 前言 提到require.js大多数人会说提到模块化开发,AMD等等,其实require.js并没有这么多 ...

  9. require.js 最佳实践【转】

    https://www.cnblogs.com/digdeep/p/4607131.html require.js是一个js库,相关的基础知识,前面转载了两篇博文:Javascript模块化编程(re ...

随机推荐

  1. Internet History, Technology and Security (Week 5-1)

    Week 5 Technology: Internets and Packets Welcome to Week 5! This week, we'll be covering internets a ...

  2. Python入门:字符串的分片与索引、字符串的方法

    这是关于Python的第3篇文章,主要介绍下字符串的分片与索引.字符串的方法. 字符串的分片与索引: 字符串可以用过string[X]来分片与索引.分片,简言之,就是从字符串总拿出一部分,储存在另一个 ...

  3. PAT 1042 字符统计

    https://pintia.cn/problem-sets/994805260223102976/problems/994805280817135616 请编写程序,找出一段给定文字中出现最频繁的那 ...

  4. HTTP压力测试工具wrk的安装及测试

    本次在VMware虚拟机的CentOS6.3系统中进行安装wrk压测工具,具体如下: 一.预先安装需求项 为了安装顺利,不受权限的限制,首先可以把用户切换为root用户# su + 输入root用户对 ...

  5. C++ Primer 中文版 5th Edition 练习15.8和练习15.9的解答

    练习15.8:给出静态类型和动态类型的定义. 答: 静态类型:是变量声明时的类型,或者是表达式生成的类型,这样的类型在编译时已知. 动态类型:是变量或者表达式表示的内存中的对象的类型,直到运行时才可知 ...

  6. Delphi : keydown与keypress的区别,组合键

    Shift 是一个集合变量. type TShiftState = set of (ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDoubl ...

  7. 【2018ICPC青岛】

    B 题意:给n个问题,每个问题有一个固定的答案ai(<=10^5).现在有m个约束关系,每个约束关系是一个二元组(ui,vi),表示你回答ui.vi问题的答案必须一样. 现在让你输出分别修复一个 ...

  8. 【Learning】积性函数前缀和——洲阁筛(min_25写法)

    问题描述 洲阁筛解决的问题主要是\(n\)范围较大的积性函数前缀和. ​ 已知一积性函数\(f(i)\),求\(\sum_{i=1}^nf(i)\). \(n\leq10^{12}\). 求解方法 如 ...

  9. 4 MySQL程序概述(包含mysql配置文件配置原理)-学习笔记

    以下参考MySQL5.5官方简体中文参考手册完美版--用于自学复习使用 4.1 程序概述 MySQL AB提供了几种类型的程序:一般放在/安装目录/bin下 1 MYSQL服务器和服务器启动脚本 my ...

  10. WEB入门二 表格和表单

    学习内容 Ø        表格的作用和制作 Ø        表单的制作 能力目标 Ø        掌握表格的创建 Ø        掌握设置表格的常用属性: Ø        理解表单的作用 Ø ...