optimizing scripts by using requirejs with loading jquery from cdn

214 views Asked by At

main.js below

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 below

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

build.js below

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

})

p.js

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

Build command:

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

This is my index.html

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

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

</body>
</html>

And I got the error when tried to open this file:

Uncaught ReferenceError: $ is not defined 

Is it possible to fix this problem?

Thanks!

UDP1: I added this to build.js, but it doesn't work

paths: {
        jquery: "empty:"
    }
1

There are 1 answers

0
Louis On

Use an "empty:" path in your build config:

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