diff --git a/lib/compiler/swc/swc-compiler.ts b/lib/compiler/swc/swc-compiler.ts index 8d5a87908..7f5f0af15 100644 --- a/lib/compiler/swc/swc-compiler.ts +++ b/lib/compiler/swc/swc-compiler.ts @@ -50,11 +50,14 @@ export class SwcCompiler extends BaseCompiler { appName, ); + const tsconfigContent = this.getTsconfigFileContentIfExists(tsConfigPath); + const filesToExclude = this.extractExcludePattern(tsconfigContent); + if (extras.watch) { if (extras.typeCheck) { this.runTypeChecker(configuration, tsConfigPath, appName, extras); } - await this.runSwc(swcOptions, extras, swcrcFilePath); + await this.runSwc(swcOptions, extras, swcrcFilePath, filesToExclude); if (onSuccess) { onSuccess(); @@ -67,7 +70,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(swcOptions, extras, swcrcFilePath, filesToExclude); if (onSuccess) { onSuccess(); } @@ -154,6 +157,7 @@ export class SwcCompiler extends BaseCompiler { options: ReturnType, extras: SwcCompilerExtras, swcrcFilePath?: string, + ignoreGlob?: string[], ) { process.nextTick(() => console.log(SWC_LOG_PREFIX, cyan('Running...'))); @@ -173,6 +177,7 @@ export class SwcCompiler extends BaseCompiler { swcOptions, cliOptions: { ...options.cliOptions, + ignore: ignoreGlob, watch: extras.watch, }, }); @@ -207,6 +212,20 @@ export class SwcCompiler extends BaseCompiler { } } + private getTsconfigFileContentIfExists( + filePath: string, + ): Record { + try { + return JSON.parse(readFileSync(join(process.cwd(), filePath), 'utf8')); + } catch { + return {}; + } + } + + private extractExcludePattern(tsconfig: Record = {}): string[] | undefined { + return tsconfig['exclude'] as string[] | undefined; + } + private deepMerge(target: T, source: T): T { if ( typeof target !== 'object' || diff --git a/test/lib/compiler/swc/swc-compiler.spec.ts b/test/lib/compiler/swc/swc-compiler.spec.ts index 4991ba018..7b271169f 100644 --- a/test/lib/compiler/swc/swc-compiler.spec.ts +++ b/test/lib/compiler/swc/swc-compiler.spec.ts @@ -41,6 +41,7 @@ describe('SWC Compiler', () => { compiler['runTypeChecker'] = jest.fn(); compiler['debounce'] = debounceMock; compiler['watchFilesInOutDir'] = jest.fn(); + compiler['getTsconfigFileContentIfExists'] = jest.fn(); jest.clearAllMocks(); }); @@ -164,6 +165,7 @@ describe('SWC Compiler', () => { 'swcOptionsTest', fixture.extras, 'swcrcPathTest', + undefined ); fixture.extras.watch = false; @@ -175,6 +177,32 @@ describe('SWC Compiler', () => { 'swcOptionsTest', fixture.extras, 'swcrcPathTest', + undefined + ); + }); + + it('should call runSwc and parse tsconfig', async () => { + swcDefaultsFactoryMock.mockReturnValueOnce('swcOptionsTest'); + getValueOrDefaultMock.mockReturnValueOnce('swcrcPathTest'); + (compiler['getTsconfigFileContentIfExists'] as jest.Mock).mockReturnValueOnce({ exclude: ['**/*spec.ts'] }); + + const fixture = { + extras: { + watch: false, + }, + }; + + fixture.extras.watch = true; + await callRunCompiler({ + extras: fixture.extras, + tsconfig: 'tsConfigPathTest' + }); + + expect(compiler['runSwc']).toHaveBeenCalledWith( + 'swcOptionsTest', + fixture.extras, + 'swcrcPathTest', + ['**/*spec.ts'] ); });