Skip to content

Commit

Permalink
Merge pull request #2245 from kdy1/master
Browse files Browse the repository at this point in the history
fix(cli): Resolve `jsc.baseUrl` before calling `@swc/cli`
  • Loading branch information
kamilmysliwiec committed Aug 18, 2023
2 parents 2230d3d + 7c6972b commit 1bd8775
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/compiler/swc/swc-compiler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as chalk from 'chalk';
import * as path from 'path';
import { fork } from 'child_process';
import * as chokidar from 'chokidar';
import { readFileSync } from 'fs';
Expand Down Expand Up @@ -53,7 +54,7 @@ export class SwcCompiler extends BaseCompiler {
if (extras.typeCheck) {
this.runTypeChecker(configuration, tsConfigPath, appName, extras);
}
await this.runSwc(swcOptions, extras, swcrcFilePath);
await this.runSwc(configuration, swcOptions, extras, swcrcFilePath);

if (onSuccess) {
onSuccess();
Expand All @@ -66,7 +67,7 @@ export class SwcCompiler extends BaseCompiler {
if (extras.typeCheck) {
await this.runTypeChecker(configuration, tsConfigPath, appName, extras);
}
await this.runSwc(swcOptions, extras, swcrcFilePath);
await this.runSwc(configuration, swcOptions, extras, swcrcFilePath);
if (onSuccess) {
onSuccess();
}
Expand Down Expand Up @@ -150,6 +151,7 @@ export class SwcCompiler extends BaseCompiler {
}

private async runSwc(
configuration: Required<Configuration>,
options: ReturnType<typeof swcDefaultsFactory>,
extras: SwcCompilerExtras,
swcrcFilePath?: string,
Expand All @@ -162,6 +164,17 @@ export class SwcCompiler extends BaseCompiler {
const swcRcFile = await this.getSwcRcFileContentIfExists(swcrcFilePath);
const swcOptions = this.deepMerge(options.swcOptions, swcRcFile);

// jsc.baseUrl should be resolved by the caller, if it's passed as an object.
// https://github.com/swc-project/swc/pull/7827
if (swcOptions?.jsc?.baseUrl) {
if (swcrcFilePath) {
swcOptions.jsc.baseUrl = path.join(path.dirname(swcrcFilePath), swcOptions.jsc.baseUrl)
} else {
swcOptions.jsc.baseUrl = path.join(path.dirname(configuration.sourceRoot), swcOptions.jsc.baseUrl)
}

}

await swcCli.default({
...options,
swcOptions,
Expand All @@ -178,7 +191,7 @@ export class SwcCompiler extends BaseCompiler {
} catch (err) {
console.error(
ERROR_PREFIX +
' Failed to load "@swc/cli" and "@swc/core" packages. Please, install them by running "npm i -D @swc/cli @swc/core".',
' Failed to load "@swc/cli" and "@swc/core" packages. Please, install them by running "npm i -D @swc/cli @swc/core".',
);
process.exit(1);
}
Expand All @@ -193,7 +206,7 @@ export class SwcCompiler extends BaseCompiler {
if (swcrcFilePath !== undefined) {
console.error(
ERROR_PREFIX +
` Failed to load "${swcrcFilePath}". Please, check if the file exists and is valid JSON.`,
` Failed to load "${swcrcFilePath}". Please, check if the file exists and is valid JSON.`,
);
process.exit(1);
}
Expand Down
2 changes: 2 additions & 0 deletions test/lib/compiler/swc/swc-compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ describe('SWC Compiler', () => {
});

expect(compiler['runSwc']).toHaveBeenCalledWith(
{},
'swcOptionsTest',
fixture.extras,
'swcrcPathTest',
Expand All @@ -172,6 +173,7 @@ describe('SWC Compiler', () => {
});

expect(compiler['runSwc']).toHaveBeenCalledWith(
{},
'swcOptionsTest',
fixture.extras,
'swcrcPathTest',
Expand Down

0 comments on commit 1bd8775

Please sign in to comment.