open-browser-once-plugin
Version:
Webpack Plugin for open browser once rather than many times
39 lines (38 loc) • 986 B
JavaScript
/**
* Webpack Plugin for open browser once rather than many times
* @author 余聪
*/
import * as openBrowser from 'react-dev-utils/openBrowser';
class OpenBrowserOncePlugin {
constructor(openUrl) {
this.openUrl = openUrl;
this._isOpened = false;
if (!openUrl) {
throw new TypeError(`"openUrl" is required`);
}
}
get isOpened() {
return this._isOpened;
}
get name() {
return this.constructor.name;
}
apply(compiler) {
const openHandler = () => {
if (!this.isOpened) {
openBrowser(this.openUrl);
}
this._isOpened = true;
};
if (compiler.hooks) {
compiler.hooks.afterEmit.tap(this.name, openHandler);
}
else {
compiler.plugin('after-emit', (c, cb) => {
openHandler();
return cb();
});
}
}
}
export { OpenBrowserOncePlugin };