From d4ff7da76e9ba45737d194c019d91237177518df Mon Sep 17 00:00:00 2001 From: Miroslav Jonas Date: Fri, 11 Aug 2023 17:35:51 +0200 Subject: [PATCH] fix(js): fix swc compilation output path handling --- packages/js/src/executors/swc/swc.impl.ts | 4 +--- packages/js/src/utils/swc/compile-swc.ts | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/js/src/executors/swc/swc.impl.ts b/packages/js/src/executors/swc/swc.impl.ts index 12beeb2ee67f8..2c11927a24dc7 100644 --- a/packages/js/src/executors/swc/swc.impl.ts +++ b/packages/js/src/executors/swc/swc.impl.ts @@ -60,11 +60,9 @@ function normalizeOptions( ); const swcrcPath = getSwcrcPath(options, root, projectRoot); - // TODO(meeroslav): Check why this is needed in order for swc to properly nest folders - const distParent = outputPath.split('/').slice(0, -1).join('/'); const swcCliOptions = { srcPath: projectRoot, - destPath: relative(root, distParent), + destPath: relative(root, outputPath), swcrcPath, }; diff --git a/packages/js/src/utils/swc/compile-swc.ts b/packages/js/src/utils/swc/compile-swc.ts index 7ae63f3d830d5..48699b97ebe89 100644 --- a/packages/js/src/utils/swc/compile-swc.ts +++ b/packages/js/src/utils/swc/compile-swc.ts @@ -1,9 +1,4 @@ -import { - cacheDir, - ExecutorContext, - getPackageManagerCommand, - logger, -} from '@nx/devkit'; +import { cacheDir, ExecutorContext, logger } from '@nx/devkit'; import { exec, execSync } from 'child_process'; import { removeSync } from 'fs-extra'; import { createAsyncIterable } from '@nx/devkit/src/utils/async-iterable'; @@ -15,8 +10,17 @@ function getSwcCmd( { swcrcPath, srcPath, destPath }: SwcCliOptions, watch = false ) { - const packageManager = getPackageManagerCommand(); - let swcCmd = `${packageManager.exec} swc ${ + // nx-ignore-next-line + const swcCLI = require.resolve('@swc/cli/bin/swc.js'); + const firstSegmentIndex = srcPath.indexOf('/'); + if (firstSegmentIndex !== -1) { + // for swc we need to strip the destination path down to the first segment + // dist/libs/{parentLib}/{libs} -> dist/libs + // TODO(meeroslav) remove this when https://github.com/swc-project/swc/issues/3028 is fixed + destPath = destPath.slice(0, -srcPath.length + firstSegmentIndex); + } + + let swcCmd = `${swcCLI} ${ // TODO(jack): clean this up when we remove inline module support // Handle root project srcPath === '.' ? 'src' : srcPath