Skip to content

Commit

Permalink
R CompilerOptions#tsconfig to cliArgs
Browse files Browse the repository at this point in the history
fixes #53
  • Loading branch information
jwloka committed Jan 25, 2025
1 parent e8e9a4c commit 4d6ff4a
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 74 deletions.
2 changes: 1 addition & 1 deletion packages/compiler/src/command.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe("addCompileCommand", () => {
};
expect(actual.project).toEqual(compilerOptions);

expect({ wildcardDirectories: {}, ...actual.tsconfig }).toEqual({
expect({ wildcardDirectories: {}, ...actual.cliArgs }).toEqual({
options: compilerOptions,
errors: [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe("createOptions", () => {
outDir: "/lib",
},

tsconfig: {
cliArgs: {
fileNames: ["/expected/one/addon.ts"],
errors: [],
options: {
Expand Down
18 changes: 9 additions & 9 deletions packages/compiler/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ const DEFAULTS: CompilerArguments & { outDir: string; project: string } = {
};

export const createOptions = (args: CompilerArguments, reporter = new NoReporter(), system = ts.sys): CompilerOptions => {
const tsconfig: ts.ParsedCommandLine = resolveTsConfig(args.project ?? DEFAULTS.project, system);
const tsConfig: ts.ParsedCommandLine = resolveTsConfig(args.project ?? DEFAULTS.project, system);
const compilationConfig = args.configFile ? resolveCompilationConfig(args.configFile, reporter, system) : undefined;

const projectDirectory =
(args?.configFile && dirname(args.configFile)) ?? (tsconfig.raw && tsconfig.raw.configFilePath && dirname(tsconfig.raw?.configFilePath));
tsconfig.options.outDir = system.resolvePath(args.buildDir ?? tsconfig.options.outDir ?? DEFAULTS.outDir);
(args?.configFile && dirname(args.configFile)) ?? (tsConfig.raw && tsConfig.raw.configFilePath && dirname(tsConfig.raw?.configFilePath));
tsConfig.options.outDir = system.resolvePath(args.buildDir ?? tsConfig.options.outDir ?? DEFAULTS.outDir);
if (projectDirectory) {
tsconfig.options = updateCompilerOptions(tsconfig.options, system, projectDirectory);
tsConfig.options = updateCompilerOptions(tsConfig.options, system, projectDirectory);
}

if (args.sourceMap !== undefined) {
tsconfig.options.sourceMap = args.sourceMap;
if (tsconfig.options.sourceMap === false) {
tsconfig.options.inlineSources = undefined;
tsConfig.options.sourceMap = args.sourceMap;
if (tsConfig.options.sourceMap === false) {
tsConfig.options.inlineSources = undefined;
}
}

Expand All @@ -52,8 +52,8 @@ export const createOptions = (args: CompilerArguments, reporter = new NoReporter
debug: args.debug ?? DEFAULTS.debug,
// TODO: Do we need lib files, or is injecting them into the system sufficient?
// files?: Record<string, string>;
tsconfig,
project: tsconfig.options,
cliArgs: tsConfig,
project: tsConfig.options,
reporter,
sourceMap: args.sourceMap ?? DEFAULTS.sourceMap,
targets: resolveTargets(targets, compilationConfig, reporter),
Expand Down
48 changes: 24 additions & 24 deletions packages/core/src/compiler/Compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe("setOptions", () => {
}).getSourceFile("src/target.ts");

new CompilerTestClass(
compileOptions(target, { project: { outDir: "/expected" }, tsconfig: { fileNames: [entry!.fileName] } }),
compileOptions(target, { project: { outDir: "/expected" }, cliArgs: { fileNames: [entry!.fileName] } }),
target
).watch();

Expand Down Expand Up @@ -188,7 +188,7 @@ describe("compile", () => {
target: ts.ScriptTarget.Latest,
configFilePath: "./tsconfig.json",
},
tsconfig: {
cliArgs: {
options: {},
fileNames: fileSystem.readDirectory("./src"),
errors: [],
Expand All @@ -213,7 +213,7 @@ describe("emitSourceFile", () => {
}).getSourceFile("src/target.ts");
const target = compileOptions(fileSystem, {
project: { declaration: true },
tsconfig: { fileNames: [entry!.fileName] },
cliArgs: { fileNames: [entry!.fileName] },
});

const actual = new CompilerTestClass(target, fileSystem).createTargetContextsIfNecessary().emitSourceFile(entry!.fileName, "*", false);
Expand All @@ -236,7 +236,7 @@ describe("emitSourceFile", () => {
}).getSourceFile("src/target.ts");
const target = compileOptions(fileSystem, {
project: { declaration: true },
tsconfig: { fileNames: [entry!.fileName] },
cliArgs: { fileNames: [entry!.fileName] },
});

const actual = new CompilerTestClass(target, fileSystem).createTargetContextsIfNecessary().emitSourceFile(entry!.fileName, "*", false);
Expand All @@ -259,7 +259,7 @@ describe("emitSourceFile", () => {
}).getSourceFile("src/target.ts");
const target = compileOptions(fileSystem, {
project: { declaration: false, sourceMap: false },
tsconfig: { fileNames: [entry!.fileName] },
cliArgs: { fileNames: [entry!.fileName] },
transpileOnly: true,
});

Expand All @@ -280,7 +280,7 @@ describe("emitSourceFile", () => {
}).getSourceFile("src/target.ts");
const target = compileOptions(fileSystem, {
project: { declaration: true, declarationMap: false, sourceMap: false },
tsconfig: { fileNames: [entry!.fileName] },
cliArgs: { fileNames: [entry!.fileName] },
sourceMap: true,
transpileOnly: true,
});
Expand All @@ -302,7 +302,7 @@ describe("emitSourceFile", () => {
}).getSourceFile("src/target.ts");
const target = compileOptions(fileSystem, {
project: { declaration: true, declarationMap: true, sourceMap: false },
tsconfig: { fileNames: [entry!.fileName] },
cliArgs: { fileNames: [entry!.fileName] },
sourceMap: true,
transpileOnly: true,
});
Expand All @@ -324,7 +324,7 @@ describe("emitSourceFile", () => {
}).getSourceFile("src/target.ts");
const target = compileOptions(fileSystem, {
project: { declaration: false, declarationMap: false, sourceMap: true },
tsconfig: { fileNames: [entry!.fileName] },
cliArgs: { fileNames: [entry!.fileName] },
sourceMap: true,
transpileOnly: true,
});
Expand All @@ -349,7 +349,7 @@ describe("emitSourceFile", () => {
}).getSourceFile("src/target.ts");
const target = compileOptions(fileSystem, {
project: { declaration: false, sourceMap: false },
tsconfig: { fileNames: [entry!.fileName] },
cliArgs: { fileNames: [entry!.fileName] },
});

const actual = new CompilerTestClass(target, fileSystem).createTargetContextsIfNecessary().emitSourceFile(entry!.fileName, "*", false);
Expand All @@ -369,7 +369,7 @@ describe("emitSourceFile", () => {
}).getSourceFile("src/target.ts");
const target = compileOptions(fileSystem, {
project: { declaration: true, declarationMap: false, sourceMap: false },
tsconfig: { fileNames: [entry!.fileName] },
cliArgs: { fileNames: [entry!.fileName] },
sourceMap: true,
});

Expand All @@ -394,7 +394,7 @@ describe("emitSourceFile", () => {
}).getSourceFile("src/target.ts");
const target = compileOptions(fileSystem, {
project: { declaration: true, declarationMap: true, sourceMap: false },
tsconfig: { fileNames: [entry!.fileName] },
cliArgs: { fileNames: [entry!.fileName] },
sourceMap: true,
});

Expand Down Expand Up @@ -422,7 +422,7 @@ describe("emitSourceFile", () => {
}).getSourceFile("src/target.ts");
const target = compileOptions(fileSystem, {
project: { declaration: false, declarationMap: false, sourceMap: true },
tsconfig: { fileNames: [entry!.fileName] },
cliArgs: { fileNames: [entry!.fileName] },
sourceMap: true,
});

Expand All @@ -446,7 +446,7 @@ describe("emitSourceFile", () => {
}).getSourceFile("src/target.ts");
const target = compileOptions(fileSystem, {
project: { declaration: false, declarationMap: false, sourceMap: false },
tsconfig: { fileNames: [entry!.fileName] },
cliArgs: { fileNames: [entry!.fileName] },
sourceMap: true,
transpileOnly: true,
});
Expand All @@ -468,7 +468,7 @@ describe("emitSourceFile", () => {
}).getSourceFile("src/target.ts");
const target = compileOptions(fileSystem, {
project: { declaration: false, declarationMap: false, sourceMap: false },
tsconfig: { fileNames: [entry!.fileName] },
cliArgs: { fileNames: [entry!.fileName] },
sourceMap: true,
});

Expand All @@ -494,7 +494,7 @@ describe("emitSourceFile", () => {
outDir: "/build",
configFilePath: "tsconfig.json",
},
tsconfig: { fileNames: [entry!.fileName] },
cliArgs: { fileNames: [entry!.fileName] },
sourceMap: true,
transpileOnly: true,
});
Expand All @@ -516,7 +516,7 @@ describe("emitSourceFile", () => {
resolveJsonModule: true,
outDir: "/build",
},
tsconfig: { fileNames: [entry!.fileName] },
cliArgs: { fileNames: [entry!.fileName] },
sourceMap: true,
});

Expand Down Expand Up @@ -547,7 +547,7 @@ describe("emitSourceFile", () => {
importHelpers: true,
strict: true,
},
tsconfig: { fileNames: [entry!.fileName] },
cliArgs: { fileNames: [entry!.fileName] },
sourceMap: true,
transpileOnly: true,
});
Expand Down Expand Up @@ -576,7 +576,7 @@ describe("emitSourceFile", () => {
importHelpers: true,
strict: true,
},
tsconfig: { fileNames: [entry!.fileName] },
cliArgs: { fileNames: [entry!.fileName] },
buildDir: "./types",
sourceMap: true,
});
Expand Down Expand Up @@ -676,7 +676,7 @@ describe("watch", () => {
targets: { "*": { writeFile: true } },
},
project: { declaration: true, outDir: "/build" },
tsconfig: { fileNames: [entry!.fileName] },
cliArgs: { fileNames: [entry!.fileName] },
watch: true,
});

Expand Down Expand Up @@ -708,7 +708,7 @@ describe("watch", () => {
targets: { "*": { writeFile: true, options: { outDir: "/build" } } },
},
project: { declaration: true },
tsconfig: { options: { outDir: "/build" }, fileNames: [entry!.fileName] },
cliArgs: { options: { outDir: "/build" }, fileNames: [entry!.fileName] },
watch: true,
});

Expand Down Expand Up @@ -744,7 +744,7 @@ describe("watch", () => {
},
project: { declaration: true },
targets: ["target1", "target2"],
tsconfig: { options: { outDir: "/build" }, fileNames: [entry!.fileName] },
cliArgs: { options: { outDir: "/build" }, fileNames: [entry!.fileName] },
watch: true,
});

Expand Down Expand Up @@ -788,7 +788,7 @@ describe("watch", () => {
},
project: { declaration: true },
targets: ["target1", "target2"],
tsconfig: { options: { outDir: "/build" }, fileNames: [entry!.fileName] },
cliArgs: { options: { outDir: "/build" }, fileNames: [entry!.fileName] },
transpileOnly: true,
watch: true,
});
Expand Down Expand Up @@ -839,7 +839,7 @@ describe("watch", () => {
},
project: { declaration: true },
targets: ["target1"],
tsconfig: {
cliArgs: {
options: { outDir: "/build" },
fileNames: ["/src/shared1.ts", "/src/shared2.ts"],
},
Expand Down Expand Up @@ -891,7 +891,7 @@ describe("watch", () => {
},
project: { declaration: true },
targets: ["target1"],
tsconfig: { options: { outDir: "/build" }, fileNames: [entry!.fileName] },
cliArgs: { options: { outDir: "/build" }, fileNames: [entry!.fileName] },
watch: true,
});

Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/compiler/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,15 @@ export class Compiler {
target?: string,
registerDependencyCallback?: (filePath: string) => void
): CompilationContext {
const { buildDir, config, configFile, project, tsconfig, watch } = compileOptions;
const { buildDir, config, configFile, project, cliArgs, watch } = compileOptions;
const { options = {}, config: targetConfig } = getTargetConfig(target, config);
return new CompilationContext({
buildDir,
project: { ...project, ...options },
projectDir: dirname(configFile ?? tsconfig.raw?.configFilePath ?? this.system.getCurrentDirectory()),
projectDir: dirname(configFile ?? cliArgs.raw?.configFilePath ?? this.system.getCurrentDirectory()),
system: this.system,
program: ts.createProgram({ rootNames: this.getRootFiles(), options: project, host: createCompileHost(project) }),
tsconfig: { ...tsconfig, options: { ...project, ...options } },
cliArgs: { ...cliArgs, options: { ...project, ...options } },
rootFiles: this.getRootFiles(),
reporter: this.reporter,
...(!!targetConfig && { config: targetConfig }),
Expand Down Expand Up @@ -357,8 +357,8 @@ export class Compiler {
}

private getRootFiles(): string[] {
return this.options?.tsconfig?.fileNames
? this.options.tsconfig.fileNames
return this.options?.cliArgs?.fileNames
? this.options.cliArgs.fileNames
: recursiveFindByFilter(this.system.resolvePath(join(dirname(this.configPath), "./src")), (path: string) =>
["ts", "tsx", "js", "jsx"].some(it => extname(path).includes(it))
);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/compiler/CompilerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface CompilerOptions {
/** Relative path to the `websmith.config.json` configuration file. */
configFile?: string;
debug?: boolean;
tsconfig: ts.ParsedCommandLine;
cliArgs: ts.ParsedCommandLine;
project: ts.CompilerOptions;
reporter: Reporter;
sourceMap?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ beforeEach(() => {
rootFiles: [],
system: testSystem,
program: testProgram,
tsconfig: { options: {}, fileNames: [], errors: [] },
cliArgs: { options: {}, fileNames: [], errors: [] },
target: "test",
});
});
Expand Down Expand Up @@ -188,7 +188,7 @@ describe("resolvePath", () => {
rootFiles: [],
system: testSystem,
program: testProgram,
tsconfig: { options: {}, fileNames: [], errors: [] },
cliArgs: { options: {}, fileNames: [], errors: [] },
target: "test",
});
});
Expand Down Expand Up @@ -277,7 +277,7 @@ describe("addAssetDependency", () => {
rootFiles: [],
system: testSystem,
program: testProgram,
tsconfig: { options: {}, fileNames: [], errors: [] },
cliArgs: { options: {}, fileNames: [], errors: [] },
target: "test",
registerDependencyCallback: target,
});
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/compiler/compilation/CompilationContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type CompilationContextOptions = {
rootFiles: string[];
system: ts.System;
target?: string;
tsconfig: ts.ParsedCommandLine;
cliArgs: ts.ParsedCommandLine;
watchCallback?: (filePath: string) => void;
registerDependencyCallback?: (filePath: string) => void;
};
Expand All @@ -38,7 +38,7 @@ export class CompilationContext implements AddonContext {
private cache: FileCache;
private languageHost: ts.LanguageServiceHost;
private reporter: Reporter;
private tsconfig: ts.ParsedCommandLine;
private cliArgs: ts.ParsedCommandLine;
private system: ts.System;
private projectDir: string;
private program: ts.Program;
Expand All @@ -50,11 +50,11 @@ export class CompilationContext implements AddonContext {
private assetCodeDependency: Map<string, string[]> = new Map();

constructor(options: CompilationContextOptions) {
const { buildDir, config, program, project, projectDir, rootFiles, system, target, tsconfig, watchCallback, registerDependencyCallback } =
const { buildDir, config, program, project, projectDir, rootFiles, system, target, cliArgs, watchCallback, registerDependencyCallback } =
options;
this.buildDir = buildDir;
this.rootFiles = rootFiles;
this.tsconfig = tsconfig;
this.cliArgs = cliArgs;
this.projectDir = projectDir;
this.transformers = {};
this.processors = [];
Expand All @@ -78,7 +78,7 @@ export class CompilationContext implements AddonContext {
}

public getConfig(): ts.ParsedCommandLine {
return this.tsconfig;
return this.cliArgs;
}

public getReporter(): Reporter {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/compile-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ export const compileOptions = (
...overrides?.project,
},
targets: overrides?.targets ?? ["*"],
tsconfig: {
cliArgs: {
options: {},
fileNames: system.readDirectory("./src"),
errors: [],
...overrides?.tsconfig,
...overrides?.cliArgs,
},
};
};
Loading

0 comments on commit 4d6ff4a

Please sign in to comment.