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?

exupero
  • 9,136
  • 8
  • 47
  • 63
  • 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? – Andrew Odri Apr 20 '15 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. – exupero Apr 20 '15 at 19:55

2 Answers2

13

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

iplus26
  • 2,518
  • 15
  • 26
  • 11
    Given that PhantomJS [has been discontinued](https://github.com/ariya/phantomjs/issues/15344), it's unlikely that we'll ever see version 2.5 be released. – Thunderforge Jun 18 '18 at 22:54
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.

exupero
  • 9,136
  • 8
  • 47
  • 63
  • 21
    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. – dude Mar 30 '16 at 10:25