Skip to content

Commit

Permalink
fix(swc): convert relative base dir path to absolute
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Aug 24, 2023
1 parent 8d3f739 commit deddb98
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
27 changes: 11 additions & 16 deletions lib/compiler/swc/swc-compiler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as chalk from 'chalk';
import * as path from 'path';
import { fork } from 'child_process';
import * as chokidar from 'chokidar';
import { readFileSync } from 'fs';
import { join, isAbsolute } from 'path';
import * as path from 'path';
import { isAbsolute, join } from 'path';
import * as ts from 'typescript';
import { Configuration } from '../../configuration';
import { ERROR_PREFIX } from '../../ui';
Expand Down Expand Up @@ -54,7 +54,7 @@ export class SwcCompiler extends BaseCompiler {
if (extras.typeCheck) {
this.runTypeChecker(configuration, tsConfigPath, appName, extras);
}
await this.runSwc(configuration, swcOptions, extras, swcrcFilePath);
await this.runSwc(swcOptions, extras, swcrcFilePath);

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

private async runSwc(
configuration: Required<Configuration>,
options: ReturnType<typeof swcDefaultsFactory>,
extras: SwcCompilerExtras,
swcrcFilePath?: string,
Expand All @@ -164,15 +163,11 @@ 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)
}

if (swcOptions?.jsc?.baseUrl && !isAbsolute(swcOptions?.jsc?.baseUrl)) {
// jsc.baseUrl should be resolved by the caller, if it's passed as an object.
// https://github.com/swc-project/swc/pull/7827
const rootDir = process.cwd();
swcOptions.jsc.baseUrl = path.join(rootDir, swcOptions.jsc.baseUrl);
}

await swcCli.default({
Expand All @@ -191,7 +186,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 @@ -206,7 +201,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
4 changes: 1 addition & 3 deletions test/lib/compiler/swc/swc-compiler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SwcCompiler } from '../../../../lib/compiler/swc/swc-compiler';
import { PluginsLoader } from '../../../../lib/compiler/plugins/plugins-loader';
import { SwcCompiler } from '../../../../lib/compiler/swc/swc-compiler';

import * as swcDefaults from '../../../../lib/compiler/defaults/swc-defaults';
import * as getValueOrDefault from '../../../../lib/compiler/helpers/get-value-or-default';
Expand Down Expand Up @@ -161,7 +161,6 @@ describe('SWC Compiler', () => {
});

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

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

0 comments on commit deddb98

Please sign in to comment.