New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: avoid dynamic requires in lib/ folder #3208
Conversation
This patch removes all dynamic requires in Puppeteer. This should make it much simpler to bundle puppeteer/puppeteer-core packages. We used dynamic requires in a few places in lib/: - BrowserFetcher was choosing between `http` and `https` based on some runtime value. This was easy to fix with explicit `require`. - BrowserFetcher and Launcher needed to know project root to store chromium revisions and to read package name and chromium revision from package.json. (projectRoot value would be different in node6). Instead of doing a backwards logic to infer these variables, we now pass them directly from `//index.js`. With this patch, I was able to bundle Puppeteer using browserify and the following config in `package.json`: ```json "browser": { "./lib/BrowserFetcher.js": false, "ws": "./lib/BrowserWebSocket", "fs": false, "child_process": false, "rimraf": false, "readline": false } ``` (where `lib/BrowserWebSocket.js` is a courtesy of @Janpot from puppeteer#2374) And command: ```sh $ browserify -r puppeteer:./index.js > ppweb.js ``` References puppeteer#2119
ec486c8
to
f068033
Compare
@aslushnikov Just want to mention that Edit: Edit2: Edit3: new WebSocket(url, null, { perMessageDeflate: false });
ws.addEventListener('open', () => resolve(new Connection(url, ws, delay)));
ws.addEventListener('error', ({ error }) => reject(error)); this._transport.addEventListener('message', ({ data }) => this._onMessage(data));
this._transport.addEventListener('close', this._onClose.bind(this)); this._transport.addEventListener('error', () => {}); package.json: "browser": {
"./lib/BrowserFetcher.js": false,
"fs": false,
"child_process": false,
"rimraf": false,
"readline": false
} |
@Janpot Ah, how do I call it the right way? |
add a second |
🤔 Or second parameter has to be empty array: d68b4f6#diff-0c1fe9dd78cc7f29823c09b251c8aae0R32 |
@Janpot ah, unfortunately we have a Pipe transport that doesn't support |
Ah yes, indeed, that makes sense. I guess in that case you could probably go the other way around. const WebSocket = require('ws');
const EventEmitter = require('events');
class WebSocketTransport extends EventEmitter {
constructor(url) {
super();
this._ws = new WebSocket(url, [], { perMessageDeflate: false });
this._ws.addEventListener('open', event => this.emit('open'));
this._ws.addEventListener('close', event => this.emit('close'));
this._ws.addEventListener('error', event => this.emit('error', event.error));
this._ws.addEventListener('message', event => this.emit('message', event.data));
}
send(message) {
this._ws.send(message);
}
close() {
this._ws.close();
}
}
// ...
static async createForWebSocket(url, delay = 0) {
return new Promise((resolve, reject) => {
const ws = new WebSocketTransport(url);
ws.on('open', () => resolve(new Connection(url, ws, delay)));
ws.on('error', reject);
});
} |
This patch removes all dynamic requires in Puppeteer. This should
make it much simpler to bundle puppeteer/puppeteer-core packages.
We used dynamic requires in a few places in lib/:
http
andhttps
based on someruntime value. This was easy to fix with explicit
require
.chromium revisions and to read package name and chromium revision from
package.json. (projectRoot value would be different in node6).
Instead of doing a backwards logic to infer these
variables, we now pass them directly from
//index.js
.With this patch, I was able to bundle Puppeteer using browserify and
the following config in
package.json
:(where
lib/BrowserWebSocket.js
is a courtesy of @Janpot from#2374)
And command:
$ browserify -r puppeteer:./index.js > ppweb.js
References #2119