使用PhantomJS运行RequireJS/WireJS应用程序

使用PhantomJS运行RequireJS/WireJS应用程序,requirejs,phantomjs,wirejs,Requirejs,Phantomjs,Wirejs,我正在尝试执行一个使用RequireJS(2.1.8)、WireJS(0.10.2)和PhantomJS(1.9.2)的基本应用程序: 使用PhantomJS运行应用程序时(这是我的目标),WireJS无法加载(请参阅下面的错误) 当使用Chrome运行应用程序时,它会正确完成 请帮助指出WireJS在PhantomJS下正常运行所缺少的部分 以下是我的应用程序文件 1) app.html 3) wireContext.js define({ message: "Hello Worl

我正在尝试执行一个使用RequireJS(2.1.8)、WireJS(0.10.2)和PhantomJS(1.9.2)的基本应用程序:

  • 使用PhantomJS运行应用程序时(这是我的目标),WireJS无法加载(请参阅下面的错误)
  • 当使用Chrome运行应用程序时,它会正确完成
请帮助指出WireJS在PhantomJS下正常运行所缺少的部分

以下是我的应用程序文件

1) app.html

3) wireContext.js

define({
    message: "Hello World!"
});
4) phantom-runner.js

(function() {
    'use strict';

    var args = require('system').args,
        timeoutRef = undefined,
        timeLimit = 10000;

    // arg[0]: scriptName, args[1...]: arguments
    if (args.length !== 2) {
        console.error('Usage:\n  phantomjs runner.js [url-of-your-qunit-testsuite]');
        phantom.exit(1);
    }

    var url = args[1],
        page = require('webpage').create();

    // Route `console.log()` calls from within the Page context to the main Phantom context (i.e. current `this`)
    page.onConsoleMessage = function(msg) {
        console.log(msg);
    };

    page.onInitialized = function() {
        timeoutRef = setTimeout(function(){
            console.error('Test Run Failed. Timeout Exceeded. Took longer than '+ timeLimit / 1000 +' seconds.');
            phantom.exit(1);
        }, timeLimit);
    };

    page.onAlert = function(message) {
        clearTimeout(timeoutRef);
        phantom.exit(0);
    };

    page.open(url, function(status) {
        if (status !== 'success') {
            console.error('Unable to access network: ' + status);
            phantom.exit(1);
        }
    });
})();
5) 在PhantomJS下运行应用程序时出错

TypeError: 'undefined' is not a function (evaluating 'Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty)')

  <path-to-deps>/wire-0.0.1/0.10.2/lib/object.js:13
  <path-to-deps>/require-0.0.1/2.1.8/require.js:1635
  <path-to-deps>/require-0.0.1/2.1.8/require.js:871
  <path-to-deps>/require-0.0.1/2.1.8/require.js:1142
  <path-to-deps>/require-0.0.1/2.1.8/require.js:779
  <path-to-deps>/require-0.0.1/2.1.8/require.js:1169 in callGetModule
  <path-to-deps>/require-0.0.1/2.1.8/require.js:1529
  <path-to-deps>/require-0.0.1/2.1.8/require.js:1656
Error: Load timeout for modules: wire!wireContext_unnormalized2
http://requirejs.org/docs/errors.html#timeout

  <path-to-deps>/require-0.0.1/2.1.8/require.js:138 in defaultOnError
  <path-to-deps>/require-0.0.1/2.1.8/require.js:536 in onError
  <path-to-deps>/require-0.0.1/2.1.8/require.js:691 in checkLoaded
  <path-to-deps>/require-0.0.1/2.1.8/require.js:710

Test Run Failed. Timeout Exceeded. Took longer than 10 seconds.
TypeError:“undefined”不是函数(计算“Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty)”)
/wire-0.0.1/0.10.2/lib/object.js:13
/require-0.0.1/2.1.8/require.js:1635
/require-0.0.1/2.1.8/require.js:871
/require-0.0.1/2.1.8/require.js:1142
/require-0.0.1/2.1.8/require.js:779
/callGetModule中的require-0.0.1/2.1.8/require.js:1169
/require-0.0.1/2.1.8/require.js:1529
/require-0.0.1/2.1.8/require.js:1656
错误:模块的加载超时:连线!wireContext\u未规范化2
http://requirejs.org/docs/errors.html#timeout
/require-0.0.1/2.1.8/require.js:defaultOnError中的138
/require-0.0.1/2.1.8/require.js:onError中的536
/checkLoaded中的require-0.0.1/2.1.8/require.js:691
/require-0.0.1/2.1.8/require.js:710
测试运行失败。超过超时时间。花了超过10秒的时间。

你说得对,尤尼斯。由于某些原因,PhantomJS不支持
Function.prototype.bind


您可以通过使用或来polyfill
Function.prototype.bind

如问题注释中所述,此问题的根源是PhantomJS没有实现“Function.prototype.bind()”函数

正如PhantomJS和WireJS的人员所建议的,这个问题将使用ES5 polyfill解决。MDN建议的实现没有帮助,因为它是规范的部分实现。CujoJS/PolyJS中包含的实现解决了我的问题。现在,WireJS对PhantomJS很满意

以下是app.js的新版本

"use strict";

require.config({
    baseUrl: ".",

    packages: [
        { name: 'wire', location: '../../../target/deps/wire-0.0.1/0.10.2', main: 'wire' },
        { name: 'when', location: '../../../target/deps/when-0.0.1/2.4.1', main: 'when' },
        { name: 'meld', location: '../../../target/deps/meld-0.0.1/1.3.0', main: 'meld' },
        { name: 'poly', location: '../../../target/deps/poly-0.0.1/0.5.2', main: 'poly' }
    ]
});

require(["poly/function", "wire!appWireContext"], function(poly, wireContext) {
    alert(wireContext.message);
});

Cheers

PhantomJS还不支持“Function.prototype.bind”。这就是失败的原因。
TypeError: 'undefined' is not a function (evaluating 'Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty)')

  <path-to-deps>/wire-0.0.1/0.10.2/lib/object.js:13
  <path-to-deps>/require-0.0.1/2.1.8/require.js:1635
  <path-to-deps>/require-0.0.1/2.1.8/require.js:871
  <path-to-deps>/require-0.0.1/2.1.8/require.js:1142
  <path-to-deps>/require-0.0.1/2.1.8/require.js:779
  <path-to-deps>/require-0.0.1/2.1.8/require.js:1169 in callGetModule
  <path-to-deps>/require-0.0.1/2.1.8/require.js:1529
  <path-to-deps>/require-0.0.1/2.1.8/require.js:1656
Error: Load timeout for modules: wire!wireContext_unnormalized2
http://requirejs.org/docs/errors.html#timeout

  <path-to-deps>/require-0.0.1/2.1.8/require.js:138 in defaultOnError
  <path-to-deps>/require-0.0.1/2.1.8/require.js:536 in onError
  <path-to-deps>/require-0.0.1/2.1.8/require.js:691 in checkLoaded
  <path-to-deps>/require-0.0.1/2.1.8/require.js:710

Test Run Failed. Timeout Exceeded. Took longer than 10 seconds.
"use strict";

require.config({
    baseUrl: ".",

    packages: [
        { name: 'wire', location: '../../../target/deps/wire-0.0.1/0.10.2', main: 'wire' },
        { name: 'when', location: '../../../target/deps/when-0.0.1/2.4.1', main: 'when' },
        { name: 'meld', location: '../../../target/deps/meld-0.0.1/1.3.0', main: 'meld' },
        { name: 'poly', location: '../../../target/deps/poly-0.0.1/0.5.2', main: 'poly' }
    ]
});

require(["poly/function", "wire!appWireContext"], function(poly, wireContext) {
    alert(wireContext.message);
});