2018-07-31 22:27:32 +00:00
|
|
|
// @flow
|
|
|
|
|
2018-09-05 04:44:09 +00:00
|
|
|
const fs = require("fs-extra");
|
2018-07-31 22:27:32 +00:00
|
|
|
const path = require("path");
|
|
|
|
|
|
|
|
// Note: the following type-import just resolves to `any`.
|
|
|
|
/*:: import type {Compiler} from "webpack"; */
|
|
|
|
|
2019-07-11 04:41:26 +00:00
|
|
|
const pluginName = "RemoveBuildDirectoryPlugin";
|
|
|
|
class RemoveBuildDirectoryPlugin {
|
2018-07-31 22:27:32 +00:00
|
|
|
apply(compiler /*: Compiler */) {
|
2019-07-11 04:41:26 +00:00
|
|
|
compiler.hooks.run.tap(pluginName, () => {
|
2018-07-31 22:27:32 +00:00
|
|
|
const outputPath = compiler.options.output.path;
|
|
|
|
// If a build config has no `output.path` property, and no
|
|
|
|
// `--output-path` is passed on the command line, then Webpack
|
|
|
|
// will default to building into the current directory. Removing
|
|
|
|
// the whole Git repository would be mighty rude, so we protect
|
|
|
|
// against that case.
|
|
|
|
if (fs.existsSync(path.join(outputPath, ".git"))) {
|
|
|
|
throw new Error(
|
|
|
|
"Refusing to remove build directory with a Git repository: " +
|
|
|
|
outputPath
|
|
|
|
);
|
|
|
|
}
|
2018-09-05 04:44:09 +00:00
|
|
|
console.warn("Removing contents of build directory: " + outputPath);
|
|
|
|
fs.emptyDirSync(outputPath);
|
2018-07-31 22:27:32 +00:00
|
|
|
});
|
|
|
|
}
|
2019-07-11 04:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = RemoveBuildDirectoryPlugin;
|