Skip to content

Commit 1bd8775

Browse files
Merge pull request #2245 from kdy1/master
fix(cli): Resolve `jsc.baseUrl` before calling `@swc/cli`
2 parents 2230d3d + 7c6972b commit 1bd8775

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

lib/compiler/swc/swc-compiler.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as chalk from 'chalk';
2+
import * as path from 'path';
23
import { fork } from 'child_process';
34
import * as chokidar from 'chokidar';
45
import { readFileSync } from 'fs';
@@ -53,7 +54,7 @@ export class SwcCompiler extends BaseCompiler {
5354
if (extras.typeCheck) {
5455
this.runTypeChecker(configuration, tsConfigPath, appName, extras);
5556
}
56-
await this.runSwc(swcOptions, extras, swcrcFilePath);
57+
await this.runSwc(configuration, swcOptions, extras, swcrcFilePath);
5758

5859
if (onSuccess) {
5960
onSuccess();
@@ -66,7 +67,7 @@ export class SwcCompiler extends BaseCompiler {
6667
if (extras.typeCheck) {
6768
await this.runTypeChecker(configuration, tsConfigPath, appName, extras);
6869
}
69-
await this.runSwc(swcOptions, extras, swcrcFilePath);
70+
await this.runSwc(configuration, swcOptions, extras, swcrcFilePath);
7071
if (onSuccess) {
7172
onSuccess();
7273
}
@@ -150,6 +151,7 @@ export class SwcCompiler extends BaseCompiler {
150151
}
151152

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

167+
// jsc.baseUrl should be resolved by the caller, if it's passed as an object.
168+
// https://github.com/swc-project/swc/pull/7827
169+
if (swcOptions?.jsc?.baseUrl) {
170+
if (swcrcFilePath) {
171+
swcOptions.jsc.baseUrl = path.join(path.dirname(swcrcFilePath), swcOptions.jsc.baseUrl)
172+
} else {
173+
swcOptions.jsc.baseUrl = path.join(path.dirname(configuration.sourceRoot), swcOptions.jsc.baseUrl)
174+
}
175+
176+
}
177+
165178
await swcCli.default({
166179
...options,
167180
swcOptions,
@@ -178,7 +191,7 @@ export class SwcCompiler extends BaseCompiler {
178191
} catch (err) {
179192
console.error(
180193
ERROR_PREFIX +
181-
' Failed to load "@swc/cli" and "@swc/core" packages. Please, install them by running "npm i -D @swc/cli @swc/core".',
194+
' Failed to load "@swc/cli" and "@swc/core" packages. Please, install them by running "npm i -D @swc/cli @swc/core".',
182195
);
183196
process.exit(1);
184197
}
@@ -193,7 +206,7 @@ export class SwcCompiler extends BaseCompiler {
193206
if (swcrcFilePath !== undefined) {
194207
console.error(
195208
ERROR_PREFIX +
196-
` Failed to load "${swcrcFilePath}". Please, check if the file exists and is valid JSON.`,
209+
` Failed to load "${swcrcFilePath}". Please, check if the file exists and is valid JSON.`,
197210
);
198211
process.exit(1);
199212
}

test/lib/compiler/swc/swc-compiler.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ describe('SWC Compiler', () => {
161161
});
162162

163163
expect(compiler['runSwc']).toHaveBeenCalledWith(
164+
{},
164165
'swcOptionsTest',
165166
fixture.extras,
166167
'swcrcPathTest',
@@ -172,6 +173,7 @@ describe('SWC Compiler', () => {
172173
});
173174

174175
expect(compiler['runSwc']).toHaveBeenCalledWith(
176+
{},
175177
'swcOptionsTest',
176178
fixture.extras,
177179
'swcrcPathTest',

0 commit comments

Comments
 (0)