32

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?

2
  • When you say browserify cant find PhantomJS-provided modules like webpage, do you mean that browserify is trying to compile those modules in when you do not want them, or that you want those modules compiled in and they are being ignored? Commented Apr 20, 2015 at 18:31
  • I need to require('webpage'), which causes Browserify too look for a node module called webpage. It throws an error that it can't be found. I do not need the modules compiled in. Commented Apr 20, 2015 at 19:55

2 Answers 2

13

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

Sign up to request clarification or add additional context in comments.

1 Comment

Given that PhantomJS has been discontinued, it's unlikely that we'll ever see version 2.5 be released.
1

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.

1 Comment

I don't know why this answered the question. Yes, it solves the missing ES6 features in PhantomJS temporary. However, this can't be a solution if you want to run "real" ES6 features without transpiling them first.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.