Skip to content

Commit 797bc83

Browse files
TINY-13550: Refactor create shared function type
Co-authored-by: ltrouton <46954949+ltrouton@users.noreply.github.com>
1 parent 940a28a commit 797bc83

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

modules/server/src/main/ts/bedrock/compiler/Compiler.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
import * as fs from 'fs';
22
import * as Webpack from '../compiler/Webpack';
33
import * as Rspack from '../compiler/Rspack';
4+
import * as Types from './Types';
45

56
export interface Compiler {
67
readonly generate: () => Promise<Buffer | string>;
78
}
89

9-
export const compile = (bundler: 'webpack' | 'rspack', tsConfigFile: string, scratchDir: string, basedir: string, exitOnCompileError: boolean, files: string[], coverage: string[], polyfills: string[]): Compiler => {
10-
const getCompileFunc = () => {
10+
export const compile = (args: { bundler: Types.Bundler; tsConfigFile: string; scratchDir: string; basedir: string; exitOnCompileError: boolean; files: string[]; coverage: string[]; polyfills: string[];
11+
}): Compiler => {
12+
const {
13+
bundler,
14+
tsConfigFile,
15+
scratchDir,
16+
basedir,
17+
exitOnCompileError,
18+
files,
19+
coverage,
20+
polyfills
21+
} = args;
22+
23+
const getCompileFunc = (): Types.CompileFn => {
1124
switch (bundler) {
1225
case 'rspack':
1326
return Rspack.compile;

modules/server/src/main/ts/bedrock/compiler/Rspack.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ExitCodes } from '../util/ExitCodes';
66
import * as Imports from './Imports';
77
import { rspack, RspackOptions } from '@rspack/core';
88
import { RspackDevServer } from '@rspack/dev-server';
9-
import { RspackCompileInfo, DevServerServeSettings } from './Types';
9+
import { RspackCompileInfo, DevServerServeSettings, CompileFn } from './Types';
1010

1111
const getWebPackConfigTs = (tsConfigFile: string, scratchFile: string, dest: string, manualMode: boolean, basedir: string): RspackOptions => {
1212
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -168,7 +168,7 @@ const getCompileInfo = (tsConfigFile: string, scratchDir: string, basedir: strin
168168
return getTsCompileInfo(tsConfigFile, scratchDir, basedir, manualMode);
169169
};
170170

171-
export const compile = async (tsConfigFile: string, scratchDir: string, basedir: string, exitOnCompileError: boolean, srcFiles: string[], _coverage: string[], polyfills: string[]): Promise<string> => {
171+
export const compile: CompileFn = async (tsConfigFile: string, scratchDir: string, basedir: string, exitOnCompileError: boolean, srcFiles: string[], _coverage: string[], polyfills: string[]): Promise<string> => {
172172
const compileInfo = await getCompileInfo(tsConfigFile, scratchDir, basedir, false);
173173
return compileTests(compileInfo, exitOnCompileError, srcFiles, polyfills);
174174
};

modules/server/src/main/ts/bedrock/compiler/Types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,14 @@ export interface CompileInfo<T> {
1717
readonly config: T;
1818
}
1919

20+
export type CompileFn = (
21+
tsConfigFile: string,
22+
scratchDir: string,
23+
basedir: string,
24+
exitOnCompileError: boolean,
25+
srcFiles: string[],
26+
coverage: string[],
27+
polyfills: string[]
28+
) => Promise<string>;
29+
30+
export type Bundler = 'rspack' | 'webpack';

modules/server/src/main/ts/bedrock/server/RunnerRoutes.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as Routes from './Routes';
66
import * as Compiler from '../compiler/Compiler';
77
import * as FileUtils from '../util/FileUtils';
88
import * as Arr from '../util/Arr';
9+
import * as Types from '../compiler/Types';
910

1011
interface PackageJson {
1112
readonly name: string;
@@ -17,22 +18,22 @@ interface WorkspaceRoot {
1718
folder: string;
1819
}
1920

20-
export const generate = async (mode: string, projectdir: string, basedir: string, configFile: string, bundler: 'webpack' | 'rspack', testfiles: string[], chunk: number,
21+
export const generate = async (mode: string, projectdir: string, basedir: string, configFile: string, bundler: Types.Bundler, testfiles: string[], chunk: number,
2122
retries: number, singleTimeout: number, stopOnFailure: boolean, basePage: string, coverage: string[], polyfills: string[]): Promise<Routes.Runner> => {
2223
const files = testfiles.map((filePath) => {
2324
return path.relative(projectdir, filePath);
2425
});
2526

26-
const testGenerator = Compiler.compile(
27+
const testGenerator = Compiler.compile({
2728
bundler,
28-
path.join(projectdir, configFile),
29-
path.join(projectdir, 'scratch'),
29+
tsConfigFile: path.join(projectdir, configFile),
30+
scratchDir: path.join(projectdir, 'scratch'),
3031
basedir,
31-
mode === 'auto',
32+
exitOnCompileError: mode === 'auto',
3233
files,
3334
coverage,
3435
polyfills
35-
);
36+
});
3637

3738
// read the project json file to determine the project name to expose resources as `/project/${name}`
3839
const pkjson: PackageJson = FileUtils.readFileAsJson(`${projectdir}/package.json`);

0 commit comments

Comments
 (0)