From deddb98e545846e44e5c5d3c2585162365dfbf18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Thu, 24 Aug 2023 12:54:40 +0200 Subject: [PATCH] fix(swc): convert relative base dir path to absolute --- lib/compiler/swc/swc-compiler.ts | 27 +++++++++------------- test/lib/compiler/swc/swc-compiler.spec.ts | 4 +--- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/lib/compiler/swc/swc-compiler.ts b/lib/compiler/swc/swc-compiler.ts index e8fb7bf25..0fc09f20e 100644 --- a/lib/compiler/swc/swc-compiler.ts +++ b/lib/compiler/swc/swc-compiler.ts @@ -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'; @@ -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(); @@ -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(); } @@ -151,7 +151,6 @@ export class SwcCompiler extends BaseCompiler { } private async runSwc( - configuration: Required, options: ReturnType, extras: SwcCompilerExtras, swcrcFilePath?: string, @@ -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({ @@ -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); } @@ -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); } diff --git a/test/lib/compiler/swc/swc-compiler.spec.ts b/test/lib/compiler/swc/swc-compiler.spec.ts index 11679ea53..4991ba018 100644 --- a/test/lib/compiler/swc/swc-compiler.spec.ts +++ b/test/lib/compiler/swc/swc-compiler.spec.ts @@ -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'; @@ -161,7 +161,6 @@ describe('SWC Compiler', () => { }); expect(compiler['runSwc']).toHaveBeenCalledWith( - {}, 'swcOptionsTest', fixture.extras, 'swcrcPathTest', @@ -173,7 +172,6 @@ describe('SWC Compiler', () => { }); expect(compiler['runSwc']).toHaveBeenCalledWith( - {}, 'swcOptionsTest', fixture.extras, 'swcrcPathTest',