Skip to content

Commit 9849015

Browse files
committed
feat: Resolve jsc.baseUrl
1 parent 3464c93 commit 9849015

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-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
}

0 commit comments

Comments
 (0)