简体   繁体   中英

RequireJS sugar for main require

My javascript file starts with a configuration...

require.config({
    baseUrl: 'scripts',
    paths: {
        'code-mirror': 'codemirror/main',
        'esprima': 'https://cdn.rawgit.com/jquery/esprima/1.2/esprima',
        'jquery': 'http://code.jquery.com/jquery-1.11.2.min',
    },
})

...and then requires...

require( ['jquery', 'code-mirror', 'profile', 'feedback', 'browser-detect'], function($, CodeMirror, undefined, feedback, browser){

...and it works! Now, I attempted to use the sugar syntax instead, like so...

require( function(require){

var $ = require('jquery'),
    CodeMirror = require('code-mirror'),
    undefined = require('profile'), // functions bound to window
    feedback = require('feedback'),
    browser = require('browser-detect');

...and it does NOT work! Why? Thank you!

The CommonJS sugar syntax is available only for define , not for require .

If you look at the source code of RequireJS, look for the regular expression named cjsRequireRegExp . This is the regular expression that is used to deal with the CommonJS syntax. Search for its occurrences: you'll see it used only for define .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM