From c90364da035d17769400880dbc258f057f279072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Wed, 28 Sep 2022 12:53:46 +0200 Subject: [PATCH] fix: disable fork ts checker when any cli plugin is enabled --- lib/compiler/defaults/webpack-defaults.ts | 160 ++++++++++++---------- 1 file changed, 85 insertions(+), 75 deletions(-) diff --git a/lib/compiler/defaults/webpack-defaults.ts b/lib/compiler/defaults/webpack-defaults.ts index 0b9ce03d6..7ac7716e9 100644 --- a/lib/compiler/defaults/webpack-defaults.ts +++ b/lib/compiler/defaults/webpack-defaults.ts @@ -14,85 +14,95 @@ export const webpackDefaultsFactory = ( isDebugEnabled = false, tsConfigFile = defaultConfiguration.compilerOptions.tsConfigPath, plugins: MultiNestCompilerPlugins, -): webpack.Configuration => ({ - entry: appendTsExtension(join(sourceRoot, entryFilename)), - devtool: isDebugEnabled ? 'inline-source-map' : false, - target: 'node', - output: { - filename: join(relativeSourceRoot, `${entryFilename}.js`), - }, - ignoreWarnings: [/^(?!CriticalDependenciesWarning$)/], - externals: [nodeExternals() as any], - externalsPresets: { node: true }, - module: { - rules: [ - { - test: /.tsx?$/, - use: [ - { - loader: 'ts-loader', - options: { - transpileOnly: !isAnyPluginRegistered(plugins), - configFile: tsConfigFile, - getCustomTransformers: (program: any) => ({ - before: plugins.beforeHooks.map((hook) => hook(program)), - after: plugins.afterHooks.map((hook) => hook(program)), - afterDeclarations: plugins.afterDeclarationsHooks.map((hook) => - hook(program), - ), - }), +): webpack.Configuration => { + const isPluginRegistered = isAnyPluginRegistered(plugins); + const webpackConfiguration: webpack.Configuration = { + entry: appendTsExtension(join(sourceRoot, entryFilename)), + devtool: isDebugEnabled ? 'inline-source-map' : false, + target: 'node', + output: { + filename: join(relativeSourceRoot, `${entryFilename}.js`), + }, + ignoreWarnings: [/^(?!CriticalDependenciesWarning$)/], + externals: [nodeExternals() as any], + externalsPresets: { node: true }, + module: { + rules: [ + { + test: /.tsx?$/, + use: [ + { + loader: 'ts-loader', + options: { + transpileOnly: !isPluginRegistered, + configFile: tsConfigFile, + getCustomTransformers: (program: any) => ({ + before: plugins.beforeHooks.map((hook) => hook(program)), + after: plugins.afterHooks.map((hook) => hook(program)), + afterDeclarations: plugins.afterDeclarationsHooks.map( + (hook) => hook(program), + ), + }), + }, }, - }, - ], - exclude: /node_modules/, - }, - ], - }, - resolve: { - extensions: ['.tsx', '.ts', '.js'], + ], + exclude: /node_modules/, + }, + ], + }, + resolve: { + extensions: ['.tsx', '.ts', '.js'], + plugins: [ + new TsconfigPathsPlugin({ + configFile: tsConfigFile, + }), + ], + }, + mode: 'none', + optimization: { + nodeEnv: false, + }, + node: { + __filename: false, + __dirname: false, + }, plugins: [ - new TsconfigPathsPlugin({ - configFile: tsConfigFile, + new webpack.IgnorePlugin({ + checkResource(resource: any) { + const lazyImports = [ + '@nestjs/microservices', + 'cache-manager', + 'class-validator', + 'class-transformer', + ]; + if (!lazyImports.includes(resource)) { + return false; + } + try { + require.resolve(resource, { + paths: [process.cwd()], + }); + } catch (err) { + return true; + } + return false; + }, }), ], - }, - mode: 'none', - optimization: { - nodeEnv: false, - }, - node: { - __filename: false, - __dirname: false, - }, - plugins: [ - new webpack.IgnorePlugin({ - checkResource(resource: any) { - const lazyImports = [ - '@nestjs/microservices', - 'cache-manager', - 'class-validator', - 'class-transformer', - ]; - if (!lazyImports.includes(resource)) { - return false; - } - try { - require.resolve(resource, { - paths: [process.cwd()], - }); - } catch (err) { - return true; - } - return false; - }, - }), - new ForkTsCheckerWebpackPlugin({ - typescript: { - configFile: tsConfigFile, - }, - }), - ], -}); + }; + + if (!isPluginRegistered) { + webpackConfiguration.plugins!.push( + new ForkTsCheckerWebpackPlugin({ + typescript: { + configFile: tsConfigFile, + }, + }), + ); + } + + return webpackConfiguration; +}; function isAnyPluginRegistered(plugins: MultiNestCompilerPlugins) { return (