From d4ff7da76e9ba45737d194c019d91237177518df Mon Sep 17 00:00:00 2001 From: Miroslav Jonas Date: Fri, 11 Aug 2023 17:35:51 +0200 Subject: [PATCH 1/4] 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 From 2bf882b271ebdb16b6ddf24bc0e533ac7fb42b48 Mon Sep 17 00:00:00 2001 From: Miroslav Jonas Date: Fri, 11 Aug 2023 20:54:45 +0200 Subject: [PATCH 2/4] Revert "fix(js): fix swc compilation output path handling" This reverts commit d4ff7da76e9ba45737d194c019d91237177518df. --- packages/js/src/executors/swc/swc.impl.ts | 4 +++- packages/js/src/utils/swc/compile-swc.ts | 20 ++++++++------------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/js/src/executors/swc/swc.impl.ts b/packages/js/src/executors/swc/swc.impl.ts index 2c11927a24dc7..12beeb2ee67f8 100644 --- a/packages/js/src/executors/swc/swc.impl.ts +++ b/packages/js/src/executors/swc/swc.impl.ts @@ -60,9 +60,11 @@ 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, outputPath), + destPath: relative(root, distParent), swcrcPath, }; diff --git a/packages/js/src/utils/swc/compile-swc.ts b/packages/js/src/utils/swc/compile-swc.ts index 48699b97ebe89..7ae63f3d830d5 100644 --- a/packages/js/src/utils/swc/compile-swc.ts +++ b/packages/js/src/utils/swc/compile-swc.ts @@ -1,4 +1,9 @@ -import { cacheDir, ExecutorContext, logger } from '@nx/devkit'; +import { + cacheDir, + ExecutorContext, + getPackageManagerCommand, + logger, +} from '@nx/devkit'; import { exec, execSync } from 'child_process'; import { removeSync } from 'fs-extra'; import { createAsyncIterable } from '@nx/devkit/src/utils/async-iterable'; @@ -10,17 +15,8 @@ function getSwcCmd( { swcrcPath, srcPath, destPath }: SwcCliOptions, watch = false ) { - // 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} ${ + const packageManager = getPackageManagerCommand(); + let swcCmd = `${packageManager.exec} swc ${ // TODO(jack): clean this up when we remove inline module support // Handle root project srcPath === '.' ? 'src' : srcPath From 70dcdc60524b7aa7ea09d5fb1666bcbcf1e81bc7 Mon Sep 17 00:00:00 2001 From: Miroslav Jonas Date: Fri, 11 Aug 2023 20:55:54 +0200 Subject: [PATCH 3/4] fix(js): revert and fix it again --- packages/js/src/executors/swc/swc.impl.ts | 27 +++++++++++++++-------- packages/js/src/utils/swc/compile-swc.ts | 12 ++++------ 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/packages/js/src/executors/swc/swc.impl.ts b/packages/js/src/executors/swc/swc.impl.ts index 12beeb2ee67f8..23c2b0367cba6 100644 --- a/packages/js/src/executors/swc/swc.impl.ts +++ b/packages/js/src/executors/swc/swc.impl.ts @@ -59,12 +59,19 @@ function normalizeOptions( outputPath ); + const projectRootParts = projectRoot.split('/'); + // We pop the last part of the `projectRoot` to pass + // the last part (projectDir) and the remainder (projectRootParts) to swc + const projectDir = projectRootParts.pop(); + // default to current directory if projectRootParts is []. + // Eg: when a project is at the root level, outside of layout dir + const swcCwd = projectRootParts.join('/') || '.'; 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), + srcPath: projectDir, + destPath: relative(join(root, swcCwd), outputPath), + swcCwd, swcrcPath, }; @@ -121,11 +128,13 @@ export async function* swcExecutor( if (!isInlineGraphEmpty(inlineProjectGraph)) { options.projectRoot = '.'; // set to root of workspace to include other libs for type check - options.swcCliOptions.srcPath = root.split('/').slice(0, -1).join('/'); // set to root of libraries to include other libs - options.swcCliOptions.destPath = join( - _options.outputPath, - options.swcCliOptions.srcPath - ); // new destPath is dist/{libs}/{parentLib}/{libs} + // remap paths for SWC compilation + options.swcCliOptions.srcPath = options.swcCliOptions.swcCwd; + options.swcCliOptions.swcCwd = '.'; + options.swcCliOptions.destPath = options.swcCliOptions.destPath + .split('../') + .at(-1) + .concat('/', options.swcCliOptions.srcPath); // tmp swcrc with dependencies to exclude // - buildable libraries diff --git a/packages/js/src/utils/swc/compile-swc.ts b/packages/js/src/utils/swc/compile-swc.ts index 7ae63f3d830d5..91315fd0b4d82 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,8 @@ function getSwcCmd( { swcrcPath, srcPath, destPath }: SwcCliOptions, watch = false ) { - const packageManager = getPackageManagerCommand(); - let swcCmd = `${packageManager.exec} swc ${ + const swcCLI = require.resolve('@swc/cli/bin/swc.js'); + let swcCmd = `${swcCLI} ${ // TODO(jack): clean this up when we remove inline module support // Handle root project srcPath === '.' ? 'src' : srcPath @@ -58,6 +53,7 @@ export async function compileSwc( const swcCmdLog = execSync(getSwcCmd(normalizedOptions.swcCliOptions), { encoding: 'utf8', + cwd: normalizedOptions.swcCliOptions.swcCwd, }); logger.log(swcCmdLog.replace(/\n/, '')); const isCompileSuccess = swcCmdLog.includes('Successfully compiled'); From 5ddfcff15e29d19d2164d0191cb42d3ffbacb9c4 Mon Sep 17 00:00:00 2001 From: FrozenPandaz Date: Fri, 11 Aug 2023 16:38:06 -0400 Subject: [PATCH 4/4] chore(repo): fix lint --- packages/js/.eslintrc.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/js/.eslintrc.json b/packages/js/.eslintrc.json index b14f51b5e8ac1..75ddd8453785f 100644 --- a/packages/js/.eslintrc.json +++ b/packages/js/.eslintrc.json @@ -51,6 +51,7 @@ "@babel/preset-env", "@babel/preset-typescript", "@babel/runtime", + "@swc/cli", "babel-plugin-const-enum", "babel-plugin-macros", "babel-plugin-transform-typescript-metadata"