【问题标题】:Loader requireJs doesn't respect dependencies加载器 requireJs 不尊重依赖关系
【发布时间】:2013-12-16 06:02:53
【问题描述】:

我使用 requireJs 来加载我的 javascript 文件。 我导入了 lib pixi.js 和 pixi_extends.js,但是 pixi_extends 生成错误,因为 PIXI 未定义...我不明白,因为 pixi_extends 应该等待 pixi.js 上传后再运行。

Bundle也是一样,pixi也是一样的错误。

我不明白,我认为我正确地做了“deps”!

loader-index.ts:(我使用 TypeScript!)

/// <reference path="../def/require.d.ts" />
/// <reference path="Init.class.ts" />

/**
 * paths    List of the files to load. (Cannot contains references TS classes)
 *              key: New reference name of the file.
 *              path: Relative path to /public/js/ of the file.
 *
 * shim     Config about the libraries (dependencies and more).
 *          See http://requirejs.org/docs/api.html#config-shim
 */
require.config({
    //urlArgs: "t=" +  (new Date()).getTime(),
    //baseUrl: "../",
    paths: {
        /*
         ******** Load libraries ********
         */
        // Lib - jQuery
        'jquery': '../generated/lib/jquery-1.10.2.min',
        'jqueryUiCore': '../generated/lib/jquery.ui.core.min',
        'jqueryUiEffect': '../generated/lib/jquery.ui.effect.min',

        // Lib - Javascript extends
        'class': '../generated/lib/class.min',

        // Lib - Pixi
        'pixi': '../generated/lib/pixi.min',
        'pixiExtends': '../generated/lib/pixi_extends.min',

        // Lib - Socket
        'socketIo': '../generated/lib/socket.io.min',

        // Lib - Pomelo
        'pomeloclient': '../generated/lib/pomeloclient.min',

        // Lib - Path finder
        'aStar': '../generated/lib/AStar.min',

        /*
         ******** Load shared source code ********
         */
        'Message': '../generated/shared/Message.min',
        'ValidatorMessage': '../generated/shared/ValidatorMessage.min',

        /*
         ******** Load other scripts ********
         */
        'bundle': '../generated/bundle.min'
    },
    shim: {
        'jquery': {
            exports: '$'
        },
        'jqueryUiCore': {
            deps: ["jquery"],
            exports: '$'
        },
        'jqueryUiEffect': {
            deps: ["jquery"],
            exports: "$"
        },
        'pixiExtends': {
            deps: ["jquery", "pixi"]
        },
        'pomeloclient': {
            deps: ["socketIo"]
        },
        'ValidatorMessage': {
            deps: ["Message"]
        },
        'bundle': {
            deps: ["pixi", "pixiExtends", "pomeloclient"]
        }
    }
});


/**
 * [] Array of name that should be the same than those defined in the config.paths. Exception for the TS classes with reference in this file.
 */
require(
    [
        'Init.class',
        'jquery', 'jqueryUiCore', 'jqueryUiEffect',
        'class',
        'pixi', 'pixiExtends',
        'socketIo', 'pomeloclient',
        'aStar',
        'Message', 'ValidatorMessage',
        'bundle'
    ],
    (
        _init,
         $, jqueryUiCore, jqueryUiEffect,
         _class,
         _pixi, pixiExtends,
         _socketIo, _pomeloclient,
         _aStar,
         _message, _validatorMessage,
         _bundle
    )
    => {
        // Initialization.
        var init = new _init.Init();

        // Make shared source classes public, to help.
        _exports([
            _message.Message,
            _validatorMessage.ValidatorMessage
        ]);


        /**
         * Export an array of object to made them public on the browser.
         * @param   objects - Array of objects. Class of function basically.
         * @private
         */
        function _exports(objects){
            for(var i in objects){
                _export(objects[i]);
            }
        }

        /**
         *Export an object to the browser to make it public.
         * @param o     Object to export.
         * @param name  Customise the name. Optional.
         * @private
         */
        function _export(o: any, name: any = ''){
            if(!name){
                name = o.name;
            }
            window[name] = o;
        }
    }
);

【问题讨论】:

  • 我通过在 pixi.js 脚本中添加 windows['PIXI'] = PIXI 来修复它。也许这是导出它的方式,这对requireJs不好。
  • Pixi.js 脚本:(12k 行)pastebin.com/SV0XLyq6
  • 我想我知道为什么它不尊重依赖关系!这是因为我的 pixi.js 脚本没有使用 define() 函数导出自身。我认为有一种方法可以在加载不通过这种方式导出自己的脚本时自动使用define(),我必须找到它。

标签: javascript dependencies requirejs typescript pixi.js


【解决方案1】:

将以下条目添加到shim 部分就足够了:

'pixi': {
  exports: 'PIXI'
}

这会将这个库变成一个 AMD 兼容模块,它可以用作独立依赖项,也可以在其他 shim 的 deps 部分中使用。

编辑:

阅读您的 cmets 似乎这个“pixi_extends”模块是您自己的代码;你应该shim你自己的模块,它只用于旧的非AMD库。如果您想通过自定义来增强 Pixi,请执行以下操作:

define(['pixi'], function (ThePixiObject) {
  ThePixiObject.customFunction = function () {
    console.log('Pixi now has customFunction()');
  }

  // no need to return anything - we're only interested in the side-effect above
});

推荐:official documentation regarding shim


注意。另外,不需要为 jQuery 配置 shim,它已经兼容 AMD。

【讨论】:

  • 我现在重试了,我是对的,它不起作用。垫片中的出口不做任何事情。没有变化。
  • 创建了一个简单的项目来证明 pixi 可以正确填充并且可以正常工作:github.com/kryger/stackoverflow_20295954
  • 那是因为我没有完全理解require()函数的工作原理,现在好了!我认为配置就足够了,但事实并非如此,我需要使用来自@kryger 的代码来要求 pixi。花了一些时间来理解一切;)
  • 很高兴能提供帮助 - 我发现通过简化的迷你项目学习 RequireJS 会容易得多。在您规模的项目中,可能出错的地方太多了,这使得发现问题变得更加困难。
  • 是的,你是对的,尤其是很多家属,我在这里处理了近两百个班级。希望很多都合并在同一个文件中。我在这里发布的只是基本的依赖关系,实际上是库。现在我已经实现了一些非常强大的东西,并且不得不处理 domLoader 插件等等......需要时间!感谢您的帮助。
【解决方案2】:

我通过在 pixi_extends 中使用 require() 函数并从官方 pixi.js 库中删除我的更改来修复它。现在可以了。

但是用 requirejs 导出没有任何效果,我不明白。那应该在全局中导出 PIXI 值,这是应该做的,但不起作用。

加载所有内容后,我可以手动导出它,如果我想将 PIXI 设为全局,这是一个解决方案。但我不是绝对需要它,所以我会保持这种方式。

但我想了解为什么“垫片导出”不起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    相关资源
    最近更新 更多