Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/compiler/swc/swc-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
Expand Down Expand Up @@ -154,6 +157,7 @@ export class SwcCompiler extends BaseCompiler {
options: ReturnType<typeof swcDefaultsFactory>,
extras: SwcCompilerExtras,
swcrcFilePath?: string,
ignoreGlob?: string[],
) {
process.nextTick(() => console.log(SWC_LOG_PREFIX, cyan('Running...')));

Expand All @@ -173,6 +177,7 @@ export class SwcCompiler extends BaseCompiler {
swcOptions,
cliOptions: {
...options.cliOptions,
ignore: ignoreGlob,
watch: extras.watch,
},
});
Expand Down Expand Up @@ -207,6 +212,20 @@ export class SwcCompiler extends BaseCompiler {
}
}

private getTsconfigFileContentIfExists(
filePath: string,
): Record<string, unknown> {
try {
return JSON.parse(readFileSync(join(process.cwd(), filePath), 'utf8'));
} catch {
return {};
}
}

private extractExcludePattern(tsconfig: Record<string, unknown> = {}): string[] | undefined {
return tsconfig['exclude'] as string[] | undefined;
}

private deepMerge<T>(target: T, source: T): T {
if (
typeof target !== 'object' ||
Expand Down
28 changes: 28 additions & 0 deletions test/lib/compiler/swc/swc-compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('SWC Compiler', () => {
compiler['runTypeChecker'] = jest.fn();
compiler['debounce'] = debounceMock;
compiler['watchFilesInOutDir'] = jest.fn();
compiler['getTsconfigFileContentIfExists'] = jest.fn();

jest.clearAllMocks();
});
Expand Down Expand Up @@ -164,6 +165,7 @@ describe('SWC Compiler', () => {
'swcOptionsTest',
fixture.extras,
'swcrcPathTest',
undefined
);

fixture.extras.watch = false;
Expand All @@ -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']
);
});

Expand Down