Skip to content

Commit

Permalink
Merge pull request #2206 from micalevisk/fix/preservewatch
Browse files Browse the repository at this point in the history
feat: add `--preserveWatchOutput` to 'build' command and support using this option from tsconfig file
  • Loading branch information
kamilmysliwiec authored Jul 31, 2023
2 parents ddcaaa1 + fd2f735 commit 226d506
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 12 deletions.
8 changes: 5 additions & 3 deletions actions/build.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ export class BuildAction extends AbstractAction {
watchMode: boolean,
onSuccess: (() => void) | undefined,
) {
const { WebpackCompiler } = await import('../lib/compiler/webpack-compiler')
const { WebpackCompiler } = await import(
'../lib/compiler/webpack-compiler'
);
const webpackCompiler = new WebpackCompiler(this.pluginsLoader);

const webpackPath =
Expand Down Expand Up @@ -230,12 +232,12 @@ export class BuildAction extends AbstractAction {
const isPreserveWatchOutputEnabled = options.find(
(option) =>
option.name === 'preserveWatchOutput' && option.value === true,
);
)?.value as boolean | undefined;
watchCompiler.run(
configuration,
pathToTsconfig,
appName,
{ preserveWatchOutput: !!isPreserveWatchOutputEnabled },
{ preserveWatchOutput: isPreserveWatchOutputEnabled },
onSuccess,
);
} else {
Expand Down
14 changes: 13 additions & 1 deletion commands/build.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ export class BuildCommand extends AbstractCommand {
)
.option('--type-check', 'Enable type checking (when SWC is used).')
.option('--webpackPath [path]', 'Path to webpack configuration.')
.option('--tsc', 'Use tsc for compilation.')
.option('--tsc', 'Use typescript compiler for compilation.')
.option(
'--preserveWatchOutput',
'Use "preserveWatchOutput" option when using tsc watch mode.',
)
.description('Build Nest application.')
.action(async (app: string, command: Command) => {
const options: Input[] = [];
Expand Down Expand Up @@ -67,6 +71,14 @@ export class BuildCommand extends AbstractCommand {
value: command.typeCheck,
});

options.push({
name: 'preserveWatchOutput',
value:
!!command.preserveWatchOutput &&
!!command.watch &&
!isWebpackEnabled,
});

const inputs: Input[] = [];
inputs.push({ name: 'app', value: app });
await this.action.handle(inputs, options);
Expand Down
4 changes: 2 additions & 2 deletions commands/start.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class StartCommand extends AbstractCommand {
)
.option('--webpackPath [path]', 'Path to webpack configuration.')
.option('--type-check', 'Enable type checking (when SWC is used).')
.option('--tsc', 'Use tsc for compilation.')
.option('--tsc', 'Use typescript compiler for compilation.')
.option(
'--sourceRoot [sourceRoot]',
'Points at the root of the source code for the single project in standard mode structures, or the default project in monorepo mode structures.',
Expand All @@ -35,7 +35,7 @@ export class StartCommand extends AbstractCommand {
.option('-e, --exec [binary]', 'Binary to run (default: "node").')
.option(
'--preserveWatchOutput',
'Use "preserveWatchOutput" option when tsc watch mode.',
'Use "preserveWatchOutput" option when using tsc watch mode.',
)
.description('Run Nest application.')
.action(async (app: string, command: Command) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/defaults/swc-defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const swcDefaultsFactory = (
transform: {
legacyDecorator: true,
decoratorMetadata: true,
useDefineForClassFields: false
useDefineForClassFields: false,
},
keepClassNames: true,
baseUrl: tsOptions?.baseUrl,
Expand Down
9 changes: 7 additions & 2 deletions lib/compiler/watch-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import {
import { TypeScriptBinaryLoader } from './typescript-loader';

type TypescriptWatchCompilerExtras = {
preserveWatchOutput: boolean;
/**
* If `undefined`, the value of 'preserveWatchOutput' option from tsconfig
* file will be used instead.
*/
preserveWatchOutput: boolean | undefined;
};

export class WatchCompiler extends BaseCompiler<TypescriptWatchCompilerExtras> {
Expand Down Expand Up @@ -60,7 +64,8 @@ export class WatchCompiler extends BaseCompiler<TypescriptWatchCompilerExtras> {
configPath,
{
...options,
preserveWatchOutput: extras.preserveWatchOutput,
preserveWatchOutput:
extras.preserveWatchOutput ?? options.preserveWatchOutput,
},
tsBin.sys,
createProgram,
Expand Down
5 changes: 4 additions & 1 deletion lib/runners/abstract.runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { ChildProcess, spawn, SpawnOptions } from 'child_process';
import { MESSAGES } from '../ui';

export class AbstractRunner {
constructor(protected binary: string, protected args: string[] = []) {}
constructor(
protected binary: string,
protected args: string[] = [],
) {}

public async run(
command: string,
Expand Down
5 changes: 4 additions & 1 deletion lib/schematics/abstract.collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { Schematic } from './nest.collection';
import { SchematicOption } from './schematic.option';

export abstract class AbstractCollection {
constructor(protected collection: string, protected runner: AbstractRunner) {}
constructor(
protected collection: string,
protected runner: AbstractRunner,
) {}

public async execute(
name: string,
Expand Down
5 changes: 4 additions & 1 deletion lib/schematics/schematic.option.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { normalizeToKebabOrSnakeCase } from '../utils/formatting';

export class SchematicOption {
constructor(private name: string, private value: boolean | string) {}
constructor(
private name: string,
private value: boolean | string,
) {}

get normalizedName() {
return normalizeToKebabOrSnakeCase(this.name);
Expand Down

0 comments on commit 226d506

Please sign in to comment.