From 9849015a4eb05f9af2e7509d492fee1696b2a146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 17 Aug 2023 23:12:10 +0900 Subject: [PATCH 1/2] feat: Resolve `jsc.baseUrl` --- lib/compiler/swc/swc-compiler.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/compiler/swc/swc-compiler.ts b/lib/compiler/swc/swc-compiler.ts index 6036fb5be..e8fb7bf25 100644 --- a/lib/compiler/swc/swc-compiler.ts +++ b/lib/compiler/swc/swc-compiler.ts @@ -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'; @@ -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(); @@ -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(); } @@ -150,6 +151,7 @@ export class SwcCompiler extends BaseCompiler { } private async runSwc( + configuration: Required, options: ReturnType, extras: SwcCompilerExtras, swcrcFilePath?: string, @@ -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, @@ -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); } @@ -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); } From 7c6972bd141894d32edac894ecc7aa9b138edc81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 18 Aug 2023 04:24:59 +0900 Subject: [PATCH 2/2] test: Update test refs --- test/lib/compiler/swc/swc-compiler.spec.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/lib/compiler/swc/swc-compiler.spec.ts b/test/lib/compiler/swc/swc-compiler.spec.ts index 37b1d859b..11679ea53 100644 --- a/test/lib/compiler/swc/swc-compiler.spec.ts +++ b/test/lib/compiler/swc/swc-compiler.spec.ts @@ -161,6 +161,7 @@ describe('SWC Compiler', () => { }); expect(compiler['runSwc']).toHaveBeenCalledWith( + {}, 'swcOptionsTest', fixture.extras, 'swcrcPathTest', @@ -172,6 +173,7 @@ describe('SWC Compiler', () => { }); expect(compiler['runSwc']).toHaveBeenCalledWith( + {}, 'swcOptionsTest', fixture.extras, 'swcrcPathTest',