Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

53 align properties of websmith cli and webpack loader options #54

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"cSpell.words": ["addon", "addons", "barfoo", "json", "websmith"],
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"eslint.experimental.useFlatConfig": true,
"eslint.useFlatConfig": true,
"eslint.format.enable": true,
"eslint.lintTask.enable": true,
// For those using file-nesting, nest the new files. E.g.:
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/AddonContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface AddonContext<O = unknown> {
/**
* Returns the command line options used to run the compiler.
*/
getConfig(): ts.ParsedCommandLine;
getCliArgs(): ts.ParsedCommandLine;

/**
* Returns the reporter to display error, warning and info messages.
Expand Down
5 changes: 3 additions & 2 deletions packages/compiler/src/CompilerArguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* ---------------------------------------------------------------------------------------------
*/
export interface CompilerArguments {
buildDir?: string;
config?: string;
addons?: string;
addonsDir?: string;
configFile?: string;
debug?: boolean;
files?: string[];
project?: string;
Expand Down
21 changes: 10 additions & 11 deletions packages/compiler/src/command.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ describe("addCompileCommand", () => {
);
expect(actual.buildDir).toEqual(expect.stringContaining(path.sep));
expect(actual.watch).toBe(false);
expect(actual.config).toEqual({ configFilePath: "/websmith.config.json" });
expect(actual.config).toBeUndefined();
expect(actual.debug).toBe(false);
const compilerOptions = {
configFilePath: "/tsconfig.json",
inlineSources: undefined,
outDir: "/lib",
outDir: "/",
sourceMap: false,
};
expect(actual.project).toEqual(compilerOptions);
expect(actual.tsConfig).toEqual(compilerOptions);

expect({ wildcardDirectories: {}, ...actual.tsconfig }).toEqual({
expect({ wildcardDirectories: {}, ...actual.cliArgs }).toEqual({
options: compilerOptions,
errors: [
{
Expand Down Expand Up @@ -96,18 +96,17 @@ describe("addCompileCommand", () => {
wildcardDirectories: { [""]: 1 },
});
expect(actual.reporter).toBeDefined();
expect(actual.sourceMap).toBe(false);
expect(actual.targets).toEqual([]);
expect(actual.watch).toBe(false);
});

it("should yield config option w/ --config cli argument", () => {
it("should yield config option w/ --configFile cli argument", () => {
const testSystem = compileSystem({ files: { "./expected/websmith.config.json": "{}" } }).fileSystem;
const target = new Compiler(createOptions({}, new NoReporter(), testSystem), testSystem);

addCompileCommand(new Command(), target).parse(["--config", "./expected/websmith.config.json"], { from: "user" });
addCompileCommand(new Command(), target).parse(["--configFile", "./expected/websmith.config.json"], { from: "user" });

expect(target.getOptions().config?.configFilePath).toEqual(expect.stringContaining("/expected/websmith.config.json"));
expect(target.getOptions().configFile).toEqual(expect.stringContaining("/expected/websmith.config.json"));
});

it("should yield project option w/ --project cli argument", () => {
Expand All @@ -116,7 +115,7 @@ describe("addCompileCommand", () => {

addCompileCommand(new Command(), target).parse(["--project", "expected/tsconfig.json"], { from: "user" });

expect(target.getOptions().project.configFilePath).toEqual(expect.stringContaining("/expected/tsconfig.json"));
expect(target.getOptions().tsConfig.configFilePath).toEqual(expect.stringContaining("/expected/tsconfig.json"));
});

it("should yield sourceMap option w/ --sourceMap cli argument", () => {
Expand All @@ -125,7 +124,7 @@ describe("addCompileCommand", () => {

addCompileCommand(new Command(), target).parse(["--sourceMap"], { from: "user" });

expect(target.getOptions().sourceMap).toBe(true);
expect(target.getOptions().tsConfig.sourceMap).toBe(true);
});

it("should yield debug compiler option w/ --debug cli argument", () => {
Expand Down Expand Up @@ -153,7 +152,7 @@ describe("addCompileCommand", () => {

addCompileCommand(new Command(), target).parse(["--transpileOnly"], { from: "user" });

expect(target.getOptions().transpileOnly).toBe(true);
expect(target.getOptions().config?.transpileOnly).toBe(true);
});

it("should yield additionalArguments w/ unsupported cli arguments", () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/compiler/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const addCompileCommand = (parent = program, compiler?: Compiler): Comman
.description("Compiles typescript source code and applies addons to transform source before or after emit.")
.option("-a, --addons <addons>", "Comma-separated list of addons to apply. All found addons will be applied by default.")
.option("-f, --addonsDir <directoryPath>", 'Directory path to the "addons" folder.', "./addons")
.option("-c, --config <filePath>", 'File path to the "websmith.config.json".', "./websmith.config.json")
.option("-c, --configFile <filePath>", 'File path to the "websmith.config.json".', "./websmith.config.json")
.option("-d, --debug", "Enable the output of debug information.", false)
.option("-p, --project <projectPath>", 'Path to the configuration file, or to a folder with a "tsconfig.json".', "./tsconfig.json")
.option("-s, --sourceMap", "Enable the output of sourceMap information.", false)
Expand All @@ -48,7 +48,8 @@ export const addCompileCommand = (parent = program, compiler?: Compiler): Comman
const system = compiler?.getSystem() ?? compileSystem();
const reporter = compiler?.getReporter() ?? new DefaultReporter(system);
const options = createOptions(args, reporter, system);
const compilationConfig = resolveCompilationConfig(args.config ?? "./websmith.config.json", reporter, system);
options.configFile = args.configFile ?? "./websmith.config.json";
const compilationConfig = resolveCompilationConfig(options.configFile, reporter, system);
const unknownArgs = (command?.args ?? []).filter(arg => !command.getOptionValueSource(arg));
if (unknownArgs?.length > 0) {
options.additionalArguments = parseUnknownArguments(unknownArgs);
Expand Down
28 changes: 12 additions & 16 deletions packages/compiler/src/options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ describe("createOptions", () => {
expect(actual).toEqual(
expect.objectContaining({
debug: false,
sourceMap: false,
watch: false,
})
);
Expand All @@ -28,14 +27,13 @@ describe("createOptions", () => {
},
});

const actual = createOptions({ project: "./expected/tsconfig.json" }, new NoReporter(), target).project;
const actual = createOptions({ project: "./expected/tsconfig.json" }, new NoReporter(), target).tsConfig;

expect(actual).toEqual(
expect.objectContaining({
configFilePath: "/expected/tsconfig.json",
outDir: "/lib",
})
);
expect(actual).toEqual({
configFilePath: "/expected/tsconfig.json",
outDir: "/",
sourceMap: false,
});
});

it("should return expected path w/ custom addons directory and '*' target", () => {
Expand Down Expand Up @@ -80,10 +78,9 @@ describe("createOptions", () => {
},
});

const actual = createOptions({ config: "./websmith.config.json" }, new NoReporter(), target).config;
const actual = createOptions({ configFile: "./websmith.config.json" }, new NoReporter(), target).config;

expect(actual).toEqual({
configFilePath: "/websmith.config.json",
targets: { whatever: { addons: ["one", "two", "three"], writeFile: true } },
});
});
Expand All @@ -104,26 +101,25 @@ describe("createOptions", () => {
{ virtual: true }
);

const actual = createOptions({ config: "./websmith.config.json" }, new NoReporter(), target);
const actual = createOptions({ configFile: "./websmith.config.json" }, new NoReporter(), target);

expect(actual).toMatchObject({
buildDir: "/",
config: {
addons: ["one", "two"],
addonsDir: "/expected",
configFilePath: "/websmith.config.json",
},
project: {
tsConfig: {
configFilePath: "/tsconfig.json",
outDir: "/lib",
outDir: "/",
},

tsconfig: {
cliArgs: {
fileNames: ["/expected/one/addon.ts"],
errors: [],
options: {
configFilePath: "/tsconfig.json",
outDir: "/lib",
outDir: "/",
},
raw: {
include: ["**/*.ts"],
Expand Down
59 changes: 30 additions & 29 deletions packages/compiler/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,47 @@ import { dirname } from "path";
import ts from "typescript";
import { CompilerArguments } from "./CompilerArguments";

const DEFAULTS = {
config: "./websmith.config.json",
debug: false,
outDir: "./lib",
project: "./tsconfig.json",
sourceMap: false,
watch: false,
};

export const createOptions = (args: CompilerArguments, reporter = new NoReporter(), system = ts.sys): CompilerOptions => {
const tsconfig: ts.ParsedCommandLine = resolveTsConfig(args.project ?? DEFAULTS.project, system);
const compilationConfig = resolveCompilationConfig(args.config ?? DEFAULTS.config, reporter, system);
const { configFile, debug = false, project = "./tsconfig.json", sourceMap = false, targets, transpileOnly, watch = false } = args;

const cliArgs = resolveTsConfig(project, system);
cliArgs.options = { ...cliArgs.options };
const compilationConfig = configFile ? resolveCompilationConfig(configFile, reporter, system) : undefined;

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

if (sourceMap !== undefined) {
cliArgs.options.sourceMap = sourceMap;
if (cliArgs.options.sourceMap === false) {
delete cliArgs.options.inlineSources;
}
}

if (args.sourceMap !== undefined) {
tsconfig.options.sourceMap = args.sourceMap;
if (tsconfig.options.sourceMap === false) {
tsconfig.options.inlineSources = undefined;
const targetNames = targets?.split(",").map(target => target.trim()) ?? [];
let config = compilationConfig;
if (transpileOnly) {
if (!config) {
config = { transpileOnly: true };
} else {
config.transpileOnly = true;
}
}

return {
buildDir: args.buildDir ?? system.getCurrentDirectory(),
config: compilationConfig,
debug: args.debug ?? DEFAULTS.debug,
buildDir: cliArgs.options.outDir!,
...(config && { config }),
...(configFile && { configFile }),
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: cliArgs.options,
reporter,
sourceMap: args.sourceMap ?? DEFAULTS.sourceMap,
targets: resolveTargets(args.targets, compilationConfig, reporter),
transpileOnly: args.transpileOnly ?? compilationConfig?.transpileOnly ?? false,
watch: args.watch ?? DEFAULTS.watch,
targets: resolveTargets(targetNames, compilationConfig, reporter),
watch,
};
};
2 changes: 1 addition & 1 deletion packages/core/src/compiler/Compiler.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe("end-2-end compile", () => {

const actual = new Compiler(
compileOptions(target, {
project: { outDir: "./bin" },
tsConfig: { outDir: "./bin" },
}),
target
).compile();
Expand Down
Loading
Loading