如何使用ES6与PhantomJS [英] How to use ES6 with PhantomJS

查看:1177
本文介绍了如何使用ES6与PhantomJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法用PhantomJS来使用ES6和模块?



我可以使用Babel将每个文件从ES6转移到ES5,但是保持并行树是尴尬的(一个在ES6中,另一个在ES5中),并将导入写入以要求ES5模块。我正在寻找一个更清洁的解决方案。



我可以删除所有导入和导出代码,将模块连接在一起,将结果转换成单个文件,然后运行PhantomJS,但是如果可能,我更愿意使用导入和导出。



我尝试使用Browserify与babelify变换将ES6依赖关系树分解为单个ES5文件,但是Browserify找不到像网页提供的PhantomJS提供的模块。我尝试忽略这些模块,放入我的 package.json

 浏览器:{
网页:false
}

但导入网页返回一个空对象而不是PhantomJS模块。



是否有一个干净的方式使用ES6模块与PhantomJS?

解决方案

Browserify的 - 排除

  browserify --exclude网页-t babelify script.js --outfile compiled.js 
phantomjs compiled.js

从依赖关系树中排除网页离开导入到位。


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?

解决方案

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.

这篇关于如何使用ES6与PhantomJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆