diff --git a/packages/docusaurus/src/webpack/plugins/CleanWebpackPlugin.ts b/packages/docusaurus/src/webpack/plugins/CleanWebpackPlugin.ts index a87d2dcfda05..a619b4b0e0cc 100644 --- a/packages/docusaurus/src/webpack/plugins/CleanWebpackPlugin.ts +++ b/packages/docusaurus/src/webpack/plugins/CleanWebpackPlugin.ts @@ -30,6 +30,7 @@ // More context: https://github.com/facebook/docusaurus/pull/1839 import path from 'path'; +import fs from 'fs-extra'; import {sync as delSync} from 'del'; import type {Compiler, Stats} from 'webpack'; @@ -152,6 +153,17 @@ export default class CleanWebpackPlugin { return; } + if ( + // eslint-disable-next-line no-restricted-properties + fs.pathExistsSync(this.outputPath) && + // eslint-disable-next-line no-restricted-properties + fs.statSync(this.outputPath).isFile() + ) { + throw new Error( + `A file '${this.outputPath}' already exists. Docusaurus needs this directory to save the build output. Either remove/change the file or choose a different build directory via '--out-dir'.`, + ); + } + this.initialClean = true; this.removeFiles(this.cleanOnceBeforeBuildPatterns);