From d256f97c454d527d5cfbcf33efa42f851a15b520 Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Thu, 13 May 2021 16:52:07 +1000 Subject: [PATCH 1/2] feat: support webpack 5 alongside webpack 5 --- npm/webpack-preprocessor/index.ts | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/npm/webpack-preprocessor/index.ts b/npm/webpack-preprocessor/index.ts index ccf236584ab9..05dc04bfbb72 100644 --- a/npm/webpack-preprocessor/index.ts +++ b/npm/webpack-preprocessor/index.ts @@ -331,15 +331,7 @@ const preprocessor: WebpackPreprocessor = (options: PreprocessorOptions = {}): F // to rerun the tests if (file.shouldWatch) { debug('watching') - - if (compiler.hooks) { - // TODO compile.tap takes "string | Tap" - // so seems we just need to pass plugin.name - // @ts-ignore - compiler.hooks.compile.tap(plugin, onCompile) - } else { - compiler.plugin('compile', onCompile) - } + compiler.hooks.compile.tap(plugin, onCompile) } const bundler = file.shouldWatch ? compiler.watch(watchOptions, handle) : compiler.run(handle) @@ -352,7 +344,9 @@ const preprocessor: WebpackPreprocessor = (options: PreprocessorOptions = {}): F if (file.shouldWatch) { // in this case the bundler is webpack.Compiler.Watching - (bundler as webpack.Compiler.Watching).close(cb) + if (bundler && 'close' in bundler) { + bundler.close(cb) + } } }) @@ -380,8 +374,10 @@ preprocessor.__reset = () => { bundles = {} } -function cleanseError (err: string) { - return err.replace(/\n\s*at.*/g, '').replace(/From previous event:\n?/g, '') +function cleanseError (err: string | Error) { + let msg = typeof err === 'string' ? err : err.message + + return msg.replace(/\n\s*at.*/g, '').replace(/From previous event:\n?/g, '') } export = preprocessor From d5d9020a882019450cde81881b0e9e5e752d133e Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Thu, 13 May 2021 17:07:48 +1000 Subject: [PATCH 2/2] revert code --- npm/webpack-preprocessor/index.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/npm/webpack-preprocessor/index.ts b/npm/webpack-preprocessor/index.ts index 05dc04bfbb72..11c30660e832 100644 --- a/npm/webpack-preprocessor/index.ts +++ b/npm/webpack-preprocessor/index.ts @@ -330,8 +330,14 @@ const preprocessor: WebpackPreprocessor = (options: PreprocessorOptions = {}): F // when we should watch, we hook into the 'compile' hook so we know when // to rerun the tests if (file.shouldWatch) { - debug('watching') - compiler.hooks.compile.tap(plugin, onCompile) + if (compiler.hooks) { + // TODO compile.tap takes "string | Tap" + // so seems we just need to pass plugin.name + // @ts-ignore + compiler.hooks.compile.tap(plugin, onCompile) + } else { + compiler.plugin('compile', onCompile) + } } const bundler = file.shouldWatch ? compiler.watch(watchOptions, handle) : compiler.run(handle)