Browserify-shim Poltergeist / Phantom.JS严格模式问题

时间:2016-05-11 21:30:05

标签: javascript jquery phantomjs browserify browserify-shim

我正在使用Browserify制作Rails project。 Bootstrap Javascript通过browserify-shim加载。

当我启动我的项目并在Firefox中查看它时,一切都是正确的。但是,当我通过Poltergeist测试它(或者只是用PhantomJS 2.1.1开始测试)时,会发生错误:

 Capybara::Poltergeist::JavascriptError:
   One or more errors were raised in the Javascript code on the page. If you don't care about these errors, you can ignore them by setting js_errors: false in your Poltergeist configuration (see documentation for details).                                                                                                                                                          

   ReferenceError: Strict mode forbids implicit creation of global property 'jQuery'
   ReferenceError: Strict mode forbids implicit creation of global property 'jQuery'
       at http://127.0.0.1:47269/assets/application-038b5f3da112b67153daa218adfed54030ee9e0085fd9586c0bb13fa320b830e.js:12

以下是相关话题:

;jQuery = global.jQuery = require("jquery");

对我而言,对于jQuery来说,它看起来像是一个垫片线(后面有&#39的bootstrap.js代码)。

这是我的package.json:

{
  "name": "billy-bones-rails",
  "version": "0.0.1",
  "description": "Billy-bones-rails asset package",
  "main": "index.js",
  "directories": {
    "doc": "doc",
    "test": "test"
  },
  "scripts": {
    "bundle-js": "./node_modules/.bin/browserify app/assets/javascripts/index.js -o app/assets/javascripts/bundle.js",
    "watch-js": "./node_modules/.bin/watchify    app/assets/javascripts/index.js -o app/assets/javascripts/bundle.js -d -v"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/art-solopov/billy-bones-rails.git"
  },
  "author": "Artemiy Solopov",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/art-solopov/billy-bones-rails/issues"
  },
  "homepage": "https://github.com/art-solopov/billy-bones-rails#readme",
  "dependencies": {
    "babel-cli": "^6.8.0",
    "babel-preset-es2015": "^6.6.0",
    "babelify": "^7.3.0",
    "bootstrap": "^3.3.6",
    "browserify": "^13.0.0",
    "browserify-shim": "^3.8.12",
    "jquery": "^2.2.2",
    "normalize.css": "^4.0.0",
    "watchify": "^3.7.0"
  },
  "browser": {
    "bootstrap": "./node_modules/bootstrap/dist/js/bootstrap.js"
  },
  "browserify": {
    "transform": [
      "browserify-shim",
      [
        "babelify",
        {
          "presets": "es2015"
        }
      ]
    ]
  },
  "browserify-shim": {
    "bootstrap": {
      "depends": [
        "jquery:jQuery"
      ]
    }
  }
}

除了禁用Poltergeist错误报告之外,我是否可以对此错误执行任何操作?

P上。 S.应该注意的是,我只能使用PhantomJS 2.1.1复制此错误。 Firefox(46和48),Chromium和PhantomJS 1.9.0输出没有错误。

1 个答案:

答案 0 :(得分:0)

显然,问题是由babelify错误配置和browserify-shim的组合引起的。我将此添加到我的babelify配置:

"ignore": "node_modules"

这摆脱了Bootstrap模块中的顶级严格模式。