From 0aad7a1a3417b06031e81b9f4fa4ec4ff0214a82 Mon Sep 17 00:00:00 2001 From: Chau Tran Date: Tue, 22 Feb 2022 12:36:31 -0600 Subject: [PATCH] fix(js): change logic around swc cli so that outputPath isn't computed --- packages/js/src/executors/swc/swc.impl.ts | 23 ++++++++--------------- packages/js/src/utils/schema.d.ts | 3 ++- packages/js/src/utils/swc/compile-swc.ts | 13 ++++++++----- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/packages/js/src/executors/swc/swc.impl.ts b/packages/js/src/executors/swc/swc.impl.ts index b44f2b4582ba9..873afa161d338 100644 --- a/packages/js/src/executors/swc/swc.impl.ts +++ b/packages/js/src/executors/swc/swc.impl.ts @@ -39,23 +39,16 @@ function normalizeOptions( outputPath ); - /** - * Reduce outputPath to root of layout dir (libsDir/appsDir) - * in desired build output dir (default is dist) - * - * dist/packages/lib-one -> dist/packages - * build/packages/lib-one -> build/packages - * dist/packages/lib-one/nested/nested-lib-one -> dist/packages - * dist/libs/lib-one -> dist/libs - * dist/apps/app-one -> dist/apps - */ - const swcDestPath = - options.outputPath.substring(0, options.outputPath.indexOf(layoutDir)) + - layoutDir; + 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(); + const swcCwd = projectRootParts.join('/'); const swcCliOptions = { - srcPath: projectRoot, - destPath: swcDestPath, + srcPath: projectDir, + destPath: relative(join(contextRoot, swcCwd), outputPath), + swcCwd, swcrcPath: join(projectRoot, '.swcrc'), }; diff --git a/packages/js/src/utils/schema.d.ts b/packages/js/src/utils/schema.d.ts index a53af157b39d5..3fa8af2dbb0ef 100644 --- a/packages/js/src/utils/schema.d.ts +++ b/packages/js/src/utils/schema.d.ts @@ -53,8 +53,9 @@ export interface SwcExecutorOptions extends ExecutorOptions { export interface SwcCliOptions { srcPath: string; - swcrcPath: string; destPath: string; + swcrcPath: string; + swcCwd: string; } export interface NormalizedSwcExecutorOptions diff --git a/packages/js/src/utils/swc/compile-swc.ts b/packages/js/src/utils/swc/compile-swc.ts index 0b222577a10d8..df24f4d6ae603 100644 --- a/packages/js/src/utils/swc/compile-swc.ts +++ b/packages/js/src/utils/swc/compile-swc.ts @@ -10,7 +10,7 @@ function getSwcCmd( { swcrcPath, srcPath, destPath }: SwcCliOptions, watch = false ) { - let swcCmd = `npx swc ${srcPath} -d ${destPath} --source-root=${srcPath} --source-maps --no-swcrc --config-file=${swcrcPath}`; + let swcCmd = `npx swc ${srcPath} -d ${destPath} --source-maps --no-swcrc --config-file=${swcrcPath}`; return watch ? swcCmd.concat(' --watch') : swcCmd; } @@ -39,9 +39,9 @@ export async function compileSwc( ) { logger.log(`Compiling with SWC for ${context.projectName}...`); - const swcCmdLog = execSync( - getSwcCmd(normalizedOptions.swcCliOptions) - ).toString(); + const swcCmdLog = execSync(getSwcCmd(normalizedOptions.swcCliOptions), { + cwd: normalizedOptions.swcCliOptions.swcCwd, + }).toString(); logger.log(swcCmdLog.replace(/\n/, '')); const isCompileSuccess = swcCmdLog.includes('Successfully compiled'); @@ -84,7 +84,10 @@ export async function* compileSwcWatch( let stderrOnData: () => void; let watcherOnExit: () => void; - const swcWatcher = exec(getSwcCmd(normalizedOptions.swcCliOptions, true)); + const swcWatcher = exec( + getSwcCmd(normalizedOptions.swcCliOptions, true), + { cwd: normalizedOptions.swcCliOptions.swcCwd } + ); processOnExit = () => { swcWatcher.kill();