Skip to content

Commit 6181592

Browse files
authored
fix(js): improve error handling during SWC compilation (#29605)
Improves our error handling for swc when using `@nx/js:swc` executor <!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> The executor fails to build if the workspace has a newer versions of `@swc/cli` e.g.(`0.6.0`) is installed since it now uses stderr instead of message to log errors. RE: swc-project/pkgs#53 ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> The executor should pass in this scenario. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #29599
1 parent 669af55 commit 6181592

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

packages/js/src/utils/swc/compile-swc.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,25 @@ export async function compileSwc(
8686
rmSync(normalizedOptions.outputPath, { recursive: true, force: true });
8787
}
8888

89-
const swcCmdLog = execSync(getSwcCmd(normalizedOptions), {
90-
encoding: 'utf8',
91-
cwd: normalizedOptions.swcCliOptions.swcCwd,
92-
windowsHide: false,
93-
});
94-
logger.log(swcCmdLog.replace(/\n/, ''));
95-
const isCompileSuccess = swcCmdLog.includes('Successfully compiled');
89+
try {
90+
const swcCmdLog = execSync(getSwcCmd(normalizedOptions), {
91+
encoding: 'utf8',
92+
cwd: normalizedOptions.swcCliOptions.swcCwd,
93+
windowsHide: false,
94+
stdio: 'pipe',
95+
});
96+
logger.log(swcCmdLog.replace(/\n/, ''));
97+
} catch (error) {
98+
logger.error('SWC compilation failed');
99+
if (error.stderr) {
100+
logger.error(error.stderr.toString());
101+
}
102+
return { success: false };
103+
}
96104

97105
if (normalizedOptions.skipTypeCheck && !normalizedOptions.isTsSolutionSetup) {
98106
await postCompilationCallback();
99-
return { success: isCompileSuccess };
107+
return { success: true };
100108
}
101109

102110
const { errors, warnings } = await runTypeCheck(
@@ -111,7 +119,7 @@ export async function compileSwc(
111119

112120
await postCompilationCallback();
113121
return {
114-
success: !hasErrors && isCompileSuccess,
122+
success: !hasErrors,
115123
outfile: normalizedOptions.mainOutputPath,
116124
};
117125
}

0 commit comments

Comments
 (0)