RequireJS not loading createjs

RequireJS not loading createjs

本文关键字:createjs loading not RequireJS      更新时间:2023-09-26

我严格遵循requirejs和preloadjs文档,但无法获得requirejs以获得对createjs的引用。这是我的配置:

requirejs.config({
    baseUrl: "script/module",
    paths: {
        createjs: "/bower_components/PreloadJS/lib/preloadjs-0.6.1.min"
    },
    shim:{
        "createjs": {
            exports: "createjs"
        }
    }
});

然后在我的mainjs:

define(
    ["createjs"],
    function (createjs) {
        //At this point all controllers are loaded and ready to go
        console.log(createjs);
        return {};
    }
);

但在记录时创建js输出此obect:

{
    noConflict: ()
    parse: parse()
    runInContext: a(b,d)
    stringify: stringify()
    __proto__: Object
}

我错过了什么?或者这是一个bug?我在require 2.1.2和preloadjs 0.6.1上。这非常令人沮丧,因为我已经使用了所有形式的垫片、重命名,甚至删除了baseUrl。我已经多次从头开始。

此外,我还尝试用"preloadjs"替换所有对"createjs"的引用。问题是全局对象"createjs"是全局命名空间中可用的对象(正如您所期望的,我可以从requirejs中访问它)。

我已经在github上打开了这个预加载js问题,但到目前为止还没有收到任何响应。https://github.com/CreateJS/PreloadJS/issues/171

谢谢你的帮助!

您可以使用已编译(缩小)的createjs代码,并在最后添加以下内容,使整个库成为一个AMD模块。

if (typeof define === "function" && define.amd) {
    define(function() {
        return window.createjs;
    });
}

在preloadjs的情况下,只需返回preloadjs变量引用,而不是createjs。

if (typeof define === "function" && define.amd) {
    define(function() {
        return window.createjs.PreloadJS;
    });
}

当然,这仍然将createjs留在全局命名空间中。