简体   繁体   中英

Browserify on PhantomJS script with `require('webpage')`

How can I run browserify on a JavaScript module that requires the PhantomJS webpage module?

Since the webpage module is provided by PhantomJS, browserify can't find it and I get Error: module "webpage" not found . The --ignore and --exclude options both produce the error. The --ignore-missing option eliminates the import altogether.

Does browserify have a way to indicate that certain uses of require should not be included as a bundle dependency?

Background

I'm trying to write a PhantomJS script in ES6. I can transpile a single script into ES5 code that runs in PhantomJS, and I'm trying to use Browserify to transpile the entire dependency tree. Unfortunately, there are some dependencies it shouldn't pull in—those provided by PhantomJS—and compilation breaks because it can find those modules.

Perhaps I should be using a different tool than Browserify?

// browserify will resolve someNodeModule and include it in the bundle
var mymodule = require('someNodeModule');

var _require = require;
// browserify will ignore this, so 'webpage' is resolved at runtime inside phantomjs
var webpage = _require('webpage');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM