简体   繁体   English

Require.js + R.js优化器忽略Shim

[英]Require.js + R.js Optimizer Ignoring Shim

R.js is not loading my shim, and thus jQuery is loading before tinyMCE and tiny is being initialized before it has loaded. R.js没有加载我的垫片,因此jQuery在tinyMCE之前加载,并且在加载之前初始化了tiny。 How can I get the shim to work?: 我怎样才能让垫片起作用?:

build-js.js: 集结js.js:

var requirejs = require('requirejs');
var config = {
    mainConfigFile: '../js/main.js',
    include: [],
    name: 'main',
    out: '../js/build/build.js',
};

    requirejs.optimize(config, function (buildResponse) {
    var contents = fs.readFileSync(config.out, 'utf8');
});

main.js: main.js:

require.config({
    paths: {
        jQuery: 'common/libs/jquery/jquery-min',
        TinyMCE: 'common/libs/tinyMCE/tiny_mce',
    },
    shim: {
        'jQuery': {
            deps:['TinyMCE'],
            exports: '$',
        },
       'jQueryUi': {
            deps: ['jQuery']
        },
        'jQuerySelectmenu': {
            deps: ['jQuery', 'jQueryUi']
        },
        'jQueryAutosize': {
            depts: ['jQuery']
        },
        'Underscore': {
            exports: '_'
        },
        'Backbone': {
            deps: ['Underscore', 'jQuery'],
            exports: 'Backbone'
        }
    }
});

require(['common/src/app'], function (App) {
    App.initialize();
});

This issue is already fixed at r.js 2.1.11 此问题已在r.js 2.1.11中修复

just place 只是放置

wrapShim: true in the build config. wrapShim: true在构建配置中为wrapShim: true

github issue github问题

configuration example 配置示例

I ran into some similar issues recently that had me a little stumped. 我最近遇到了一些类似的问题让我有点难过。 I'm not familiar with the TinyMCE code, but I see that you haven't shimmed it. 我不熟悉TinyMCE代码,但我发现你还没有填充它。

Shims (generally) cannot depend on AMD style libraries. 垫片(通常)不能依赖于AMD样式库。 Not sure if TinyMCE falls into the AMD module style category or not, but if it does.. you're in trouble. 不确定TinyMCE是否属于AMD模块样式类别,但如果确实如此......那你就麻烦了。 If it doesn't, you need to shim it as well. 如果没有,你也需要垫片。

https://github.com/jrburke/requirejs/wiki/Upgrading-to-RequireJS-2.0#wiki-shim https://github.com/jrburke/requirejs/wiki/Upgrading-to-RequireJS-2.0#wiki-shim

Important caveat for "shim" config: “垫片”配置的重要警告:

Only use other "shim" modules as dependencies for shimmed scripts, or AMD libraries that have no dependencies and call define() after they also create a global (like jQuery or lodash). 只使用其他“shim”模块作为shimmed脚本的依赖项,或者没有依赖项的AMD库,并在创建全局(如jQuery或lodash)之后调用define()。 Otherwise, if you use an AMD module as a dependency for a shim config module, after a build, that AMD module may not be evaluated until after the shimmed code in the build executes, and an error will occur. 否则,如果您使用AMD模块作为shim配置模块的依赖项,则在构建之后,可能不会在构建中的填充代码执行之后评估该AMD模块,并且将发生错误。 The ultimate fix is to upgrade all the shimmed code to have optional AMD define() calls. 最终的解决方法是升级所有已调整的代码以进行可选的AMD define()调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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