webpack-extension-reloader
Version:
Watch for changes and force the reload of the browser extension
41 lines (35 loc) • 1.11 kB
text/typescript
import { resolve } from "path";
import { cwd } from "process";
import { log } from "util";
import {
DEFAULT_CONFIG,
DEFAULT_PORT,
} from "../src/constants/options.constants";
import { IPluginOptions } from "../typings/webpack-extension-reloader";
import { CONFIG, HELP, MANIFEST, NO_PAGE_RELOAD, PORT } from "./args.constant";
import { SIG_EXIT } from "./events.constants";
import manual from "./manual";
export default (args: object) => {
if (args[HELP]) {
log(manual());
throw { type: SIG_EXIT, payload: 0 };
}
const config = args[CONFIG] || DEFAULT_CONFIG;
const port = args[PORT] || DEFAULT_PORT;
const manifest = args[MANIFEST] || null;
const pluginOptions: IPluginOptions = {
manifest,
port,
reloadPage: !args[NO_PAGE_RELOAD],
};
const optPath = resolve(cwd(), config);
try {
// tslint:disable-next-line: no-eval
const webpackConfig = eval("require")(optPath);
return { webpackConfig, pluginOptions };
} catch (err) {
log(`[Error] Couldn't require the file: ${optPath}`);
log(err);
throw { type: SIG_EXIT, payload: 1 };
}
};