Skip to content

Commit 4e388cb

Browse files
authored
feat: additional paths for SWC server compilation (#264)
1 parent 5cb11f5 commit 4e388cb

File tree

7 files changed

+67
-10
lines changed

7 files changed

+67
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ node_modules
99
dist
1010

1111
examples/**/package-lock.json
12+
.DS_Store

src/commands/build/build-service/server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ compile({
1616
logger,
1717
outputPath: ${JSON.stringify(paths.appDist)},
1818
projectPath: ${JSON.stringify(paths.appServer)},
19+
additionalPaths: ${JSON.stringify(config.server.swcOptions?.additionalPaths)},
20+
exclude: ${JSON.stringify(config.server.swcOptions?.exclude)},
1921
});`;
2022
}
2123

src/commands/dev/server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ watch(
4848
onAfterFilesEmitted: () => {
4949
process.send({type: 'Emitted'});
5050
},
51+
additionalPaths: ${JSON.stringify(config.server.swcOptions?.additionalPaths)},
52+
exclude: ${JSON.stringify(config.server.swcOptions?.exclude)},
5153
}
5254
);`;
5355
}

src/common/models/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,21 @@ export interface ServerConfig {
365365
watchThrottle?: number;
366366
inspect?: number | true;
367367
inspectBrk?: number | true;
368+
368369
/**
369370
* Compiler for server code compilation
370371
* @default 'typescript'
371372
*/
372373
compiler?: ServerCompiler;
374+
375+
/**
376+
* Additional options for SWC compilation.
377+
* Works only if `compiler` is 'swc'.
378+
*/
379+
swcOptions?: {
380+
additionalPaths?: string[];
381+
exclude?: string | string[];
382+
};
373383
}
374384
export interface ServiceConfig {
375385
target?: 'client' | 'server';

src/common/swc/compile.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,29 @@ import {elapsedTime} from '../logger/pretty-time';
33
// @ts-ignore @swc/cli is not typed
44
import {swcDir} from '@swc/cli';
55
import {EXTENSIONS_TO_COMPILE, getSwcOptionsFromTsconfig} from './utils';
6+
import type {GetSwcOptionsFromTsconfigOptions} from './utils';
67

7-
interface SwcCompileOptions {
8+
type SwcCompileOptions = Pick<GetSwcOptionsFromTsconfigOptions, 'additionalPaths' | 'exclude'> & {
89
projectPath: string;
910
outputPath: string;
1011
logger: Logger;
11-
}
12+
};
1213

13-
export async function compile({projectPath, outputPath, logger}: SwcCompileOptions): Promise<void> {
14+
export async function compile({
15+
projectPath,
16+
outputPath,
17+
logger,
18+
additionalPaths,
19+
exclude,
20+
}: SwcCompileOptions): Promise<void> {
1421
const start = process.hrtime.bigint();
1522
logger.message('Start compilation');
1623

17-
const {swcOptions, directoriesToCompile} = getSwcOptionsFromTsconfig(projectPath);
24+
const {swcOptions, directoriesToCompile} = getSwcOptionsFromTsconfig({
25+
projectPath,
26+
additionalPaths,
27+
exclude,
28+
});
1829

1930
const cliOptions = {
2031
filenames: directoriesToCompile,

src/common/swc/utils.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import path from 'path';
22
import {convert} from 'tsconfig-to-swcconfig';
33

4+
const DEFAULT_EXCLUDE = ['node_modules'];
5+
46
export const EXTENSIONS_TO_COMPILE = ['.js', '.ts', '.mts', '.mjs', '.cjs'];
57

68
function resolvePaths(paths: Record<string, string[]>, baseUrl: string) {
@@ -14,17 +16,41 @@ function resolvePaths(paths: Record<string, string[]>, baseUrl: string) {
1416
return entries;
1517
}
1618

17-
export function getSwcOptionsFromTsconfig(projectPath: string, filename = 'tsconfig.json') {
19+
export type GetSwcOptionsFromTsconfigOptions = {
20+
projectPath: string;
21+
filename?: string;
22+
additionalPaths?: string[];
23+
exclude?: string | string[];
24+
};
25+
26+
export function getSwcOptionsFromTsconfig({
27+
projectPath,
28+
filename = 'tsconfig.json',
29+
additionalPaths,
30+
exclude,
31+
}: GetSwcOptionsFromTsconfigOptions) {
1832
const swcOptions = convert(filename, projectPath);
33+
swcOptions.exclude = swcOptions.exclude || [];
1934
swcOptions.jsc = {
2035
...swcOptions.jsc,
2136
// SWC requires absolute path as baseUrl
2237
baseUrl: projectPath,
2338
};
2439

40+
let customExclude: string[] = [];
41+
if (Array.isArray(exclude)) {
42+
customExclude = exclude;
43+
} else if (exclude) {
44+
customExclude = [exclude];
45+
}
46+
47+
swcOptions.exclude = [...(swcOptions.exclude || []), ...DEFAULT_EXCLUDE, ...customExclude];
48+
2549
// SWC don't compile referenced files like tsc, so we need collect all directories to compile.
2650
const paths = swcOptions.jsc.paths || {};
27-
const directoriesToCompile = [projectPath, ...resolvePaths(paths, projectPath)];
51+
const directoriesToCompile = [
52+
...new Set([projectPath, ...resolvePaths(paths, projectPath), ...(additionalPaths || [])]),
53+
];
2854

2955
return {
3056
swcOptions,

src/common/swc/watch.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@ import type {Logger} from '../logger';
22
// @ts-ignore @swc/cli is not typed
33
import {swcDir} from '@swc/cli';
44
import {EXTENSIONS_TO_COMPILE, getSwcOptionsFromTsconfig} from './utils';
5+
import type {GetSwcOptionsFromTsconfigOptions} from './utils';
56

6-
interface SwcWatchOptions {
7+
type SwcWatchOptions = Pick<GetSwcOptionsFromTsconfigOptions, 'additionalPaths' | 'exclude'> & {
78
outputPath: string;
89
logger: Logger;
910
onAfterFilesEmitted?: () => void;
10-
}
11+
};
1112

1213
export async function watch(
1314
projectPath: string,
14-
{outputPath, logger, onAfterFilesEmitted}: SwcWatchOptions,
15+
{outputPath, logger, onAfterFilesEmitted, additionalPaths, exclude}: SwcWatchOptions,
1516
) {
1617
logger.message('Start compilation in watch mode');
17-
const {swcOptions, directoriesToCompile} = getSwcOptionsFromTsconfig(projectPath);
18+
const {swcOptions, directoriesToCompile} = getSwcOptionsFromTsconfig({
19+
projectPath,
20+
additionalPaths,
21+
exclude,
22+
});
1823

1924
const cliOptions = {
2025
filenames: directoriesToCompile,

0 commit comments

Comments
 (0)