How to use ES6 with PhantomJS

时间秒杀一切 提交于 2019-12-28 13:42:33

问题


Is there a way to use ES6 and modules with PhantomJS?

I can transpile each file from ES6 to ES5 using Babel, but it's awkward to maintain parallel trees (one in ES6 and another in ES5) and write the imports to require the ES5 modules. I'm looking for a cleaner solution.

I can remove all import and export code, concatenate the modules together, transpile the result into a single file, then run in through PhantomJS, but I'd prefer to use imports and exports if possible.

I tried using Browserify with the babelify transform to transpile the ES6 dependency tree into a single ES5 file, but Browserify can't find PhantomJS-provided modules like webpage. I've tried ignoring those modules by putting in my package.json:

"browser": {
  "webpage": false
}

but importing webpage returns an empty object instead of the PhantomJS module.

Is there a clean way to use ES6 modules with PhantomJS?


回答1:


According to PhantomJS dev's comment on GitHub, the full support of ES6 will come with PhantomJS 2.5.




回答2:


Browserify's --exclude option does what I need.

browserify --exclude webpage -t babelify script.js --outfile compiled.js
phantomjs compiled.js

That excludes webpage from the dependency tree but leaves the import in place.



来源:https://stackoverflow.com/questions/29736114/how-to-use-es6-with-phantomjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!