使用requirejs从cdn加载jquery来优化脚本

时间:2013-12-06 12:21:07

标签: jquery requirejs cdn minify r.js

下面的main.js

requirejs.config({
    baseUrl: ".",
    shim: {
        p: ['jquery']
    },
    paths: {
        'jquery': '//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min',
        'p': 'p'
    }
});

require(['jquery', 'c'], function ($) {
    console.log($)
});

下面的c.js

define(['p'], function () {
    return {};
});

下面的build.js

({
    baseUrl: ".",
    name: "main",
    out: "main-built.js",
    optimize: "none",
    mainConfigFile: 'main.js'

})

p.js

$('body').html('p!');

构建命令:

node path/to/r.js -o ./build.js

这是我的index.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

<script src="../app/libs/requirejs/require.js" data-main="main-built"></script>

</body>
</html>

尝试打开此文件时出现错误:

Uncaught ReferenceError: $ is not defined 

有可能解决这个问题吗?

谢谢!

UDP1: 我将此添加到build.js,但它不起作用

paths: {
        jquery: "empty:"
    }

1 个答案:

答案 0 :(得分:1)

在构建配置中使用"empty:" path

({
    baseUrl: ".",
    name: "main",
    out: "main-built.js",
    optimize: "none",
    mainConfigFile: 'main.js',
    paths: {
        jquery: "empty:"
    }
})