Skip to content

Commit

Permalink
fix(js): fix swc compilation output path handling (#18588)
Browse files Browse the repository at this point in the history
Co-authored-by: FrozenPandaz <jasonjean1993@gmail.com>
  • Loading branch information
meeroslav and FrozenPandaz committed Aug 11, 2023
1 parent ccd6e04 commit b152f20
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/js/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
27 changes: 18 additions & 9 deletions packages/js/src/executors/swc/swc.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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
Expand Down
12 changes: 4 additions & 8 deletions packages/js/src/utils/swc/compile-swc.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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
Expand Down Expand Up @@ -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');
Expand Down

1 comment on commit b152f20

@vercel
Copy link

@vercel vercel bot commented on b152f20 Aug 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev

Please sign in to comment.