From 901affd6168afdba36707106d545d910a53f37b7 Mon Sep 17 00:00:00 2001 From: ZhangMJ <295692590@qq.com> Date: Tue, 14 May 2019 11:47:28 +0800 Subject: [PATCH 1/2] fix options test --- src/clean-webpack-plugin.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/clean-webpack-plugin.ts b/src/clean-webpack-plugin.ts index 7942986..c82c240 100644 --- a/src/clean-webpack-plugin.ts +++ b/src/clean-webpack-plugin.ts @@ -48,7 +48,7 @@ export interface Options { * * Use !negative patterns to exclude files * - * default: disabled + * default: [] */ cleanAfterEveryBuildPatterns?: string[]; @@ -75,7 +75,7 @@ class CleanWebpackPlugin { private outputPath: string; constructor(options: Options = {}) { - if (typeof options !== 'object' || Array.isArray(options) === true) { + if (Object.prototype.toString.call(options) !== "[object Object]") { throw new Error(`clean-webpack-plugin only accepts an options object. See: https://github.com/johnagan/clean-webpack-plugin#options-and-defaults-optional`); } From 57d2cd42d876855e4be6fb8fbe24ffe3c3dc5b66 Mon Sep 17 00:00:00 2001 From: Chris Blossom Date: Mon, 20 May 2019 17:49:04 -0700 Subject: [PATCH 2/2] use isPlainObject function --- README.md | 2 +- src/clean-webpack-plugin.ts | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 51b8a21..827749d 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ new CleanWebpackPlugin({ // // Use !negative patterns to exclude files // - // default: disabled + // default: [] cleanAfterEveryBuildPatterns: ['static*.*', '!static1.js'], // Allow clean patterns outside of process.cwd() diff --git a/src/clean-webpack-plugin.ts b/src/clean-webpack-plugin.ts index c82c240..b13f1f8 100644 --- a/src/clean-webpack-plugin.ts +++ b/src/clean-webpack-plugin.ts @@ -62,6 +62,16 @@ export interface Options { dangerouslyAllowCleanPatternsOutsideProject?: boolean; } +// Copied from https://github.com/sindresorhus/is-plain-obj/blob/97480673cf12145b32ec2ee924980d66572e8a86/index.js +function isPlainObject(value: unknown): boolean { + if (Object.prototype.toString.call(value) !== '[object Object]') { + return false; + } + + const prototype = Object.getPrototypeOf(value); + return prototype === null || prototype === Object.getPrototypeOf({}); +} + class CleanWebpackPlugin { private readonly dry: boolean; private readonly verbose: boolean; @@ -75,7 +85,7 @@ class CleanWebpackPlugin { private outputPath: string; constructor(options: Options = {}) { - if (Object.prototype.toString.call(options) !== "[object Object]") { + if (isPlainObject(options) === false) { throw new Error(`clean-webpack-plugin only accepts an options object. See: https://github.com/johnagan/clean-webpack-plugin#options-and-defaults-optional`); }