Skip to content

Commit 226d506

Browse files
Merge pull request #2206 from micalevisk/fix/preservewatch
feat: add `--preserveWatchOutput` to 'build' command and support using this option from tsconfig file
2 parents ddcaaa1 + fd2f735 commit 226d506

File tree

8 files changed

+40
-12
lines changed

8 files changed

+40
-12
lines changed

actions/build.action.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ export class BuildAction extends AbstractAction {
186186
watchMode: boolean,
187187
onSuccess: (() => void) | undefined,
188188
) {
189-
const { WebpackCompiler } = await import('../lib/compiler/webpack-compiler')
189+
const { WebpackCompiler } = await import(
190+
'../lib/compiler/webpack-compiler'
191+
);
190192
const webpackCompiler = new WebpackCompiler(this.pluginsLoader);
191193

192194
const webpackPath =
@@ -230,12 +232,12 @@ export class BuildAction extends AbstractAction {
230232
const isPreserveWatchOutputEnabled = options.find(
231233
(option) =>
232234
option.name === 'preserveWatchOutput' && option.value === true,
233-
);
235+
)?.value as boolean | undefined;
234236
watchCompiler.run(
235237
configuration,
236238
pathToTsconfig,
237239
appName,
238-
{ preserveWatchOutput: !!isPreserveWatchOutputEnabled },
240+
{ preserveWatchOutput: isPreserveWatchOutputEnabled },
239241
onSuccess,
240242
);
241243
} else {

commands/build.command.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ export class BuildCommand extends AbstractCommand {
1818
)
1919
.option('--type-check', 'Enable type checking (when SWC is used).')
2020
.option('--webpackPath [path]', 'Path to webpack configuration.')
21-
.option('--tsc', 'Use tsc for compilation.')
21+
.option('--tsc', 'Use typescript compiler for compilation.')
22+
.option(
23+
'--preserveWatchOutput',
24+
'Use "preserveWatchOutput" option when using tsc watch mode.',
25+
)
2226
.description('Build Nest application.')
2327
.action(async (app: string, command: Command) => {
2428
const options: Input[] = [];
@@ -67,6 +71,14 @@ export class BuildCommand extends AbstractCommand {
6771
value: command.typeCheck,
6872
});
6973

74+
options.push({
75+
name: 'preserveWatchOutput',
76+
value:
77+
!!command.preserveWatchOutput &&
78+
!!command.watch &&
79+
!isWebpackEnabled,
80+
});
81+
7082
const inputs: Input[] = [];
7183
inputs.push({ name: 'app', value: app });
7284
await this.action.handle(inputs, options);

commands/start.command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class StartCommand extends AbstractCommand {
2323
)
2424
.option('--webpackPath [path]', 'Path to webpack configuration.')
2525
.option('--type-check', 'Enable type checking (when SWC is used).')
26-
.option('--tsc', 'Use tsc for compilation.')
26+
.option('--tsc', 'Use typescript compiler for compilation.')
2727
.option(
2828
'--sourceRoot [sourceRoot]',
2929
'Points at the root of the source code for the single project in standard mode structures, or the default project in monorepo mode structures.',
@@ -35,7 +35,7 @@ export class StartCommand extends AbstractCommand {
3535
.option('-e, --exec [binary]', 'Binary to run (default: "node").')
3636
.option(
3737
'--preserveWatchOutput',
38-
'Use "preserveWatchOutput" option when tsc watch mode.',
38+
'Use "preserveWatchOutput" option when using tsc watch mode.',
3939
)
4040
.description('Run Nest application.')
4141
.action(async (app: string, command: Command) => {

lib/compiler/defaults/swc-defaults.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const swcDefaultsFactory = (
2525
transform: {
2626
legacyDecorator: true,
2727
decoratorMetadata: true,
28-
useDefineForClassFields: false
28+
useDefineForClassFields: false,
2929
},
3030
keepClassNames: true,
3131
baseUrl: tsOptions?.baseUrl,

lib/compiler/watch-compiler.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import {
1616
import { TypeScriptBinaryLoader } from './typescript-loader';
1717

1818
type TypescriptWatchCompilerExtras = {
19-
preserveWatchOutput: boolean;
19+
/**
20+
* If `undefined`, the value of 'preserveWatchOutput' option from tsconfig
21+
* file will be used instead.
22+
*/
23+
preserveWatchOutput: boolean | undefined;
2024
};
2125

2226
export class WatchCompiler extends BaseCompiler<TypescriptWatchCompilerExtras> {
@@ -60,7 +64,8 @@ export class WatchCompiler extends BaseCompiler<TypescriptWatchCompilerExtras> {
6064
configPath,
6165
{
6266
...options,
63-
preserveWatchOutput: extras.preserveWatchOutput,
67+
preserveWatchOutput:
68+
extras.preserveWatchOutput ?? options.preserveWatchOutput,
6469
},
6570
tsBin.sys,
6671
createProgram,

lib/runners/abstract.runner.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { ChildProcess, spawn, SpawnOptions } from 'child_process';
33
import { MESSAGES } from '../ui';
44

55
export class AbstractRunner {
6-
constructor(protected binary: string, protected args: string[] = []) {}
6+
constructor(
7+
protected binary: string,
8+
protected args: string[] = [],
9+
) {}
710

811
public async run(
912
command: string,

lib/schematics/abstract.collection.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { Schematic } from './nest.collection';
33
import { SchematicOption } from './schematic.option';
44

55
export abstract class AbstractCollection {
6-
constructor(protected collection: string, protected runner: AbstractRunner) {}
6+
constructor(
7+
protected collection: string,
8+
protected runner: AbstractRunner,
9+
) {}
710

811
public async execute(
912
name: string,

lib/schematics/schematic.option.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { normalizeToKebabOrSnakeCase } from '../utils/formatting';
22

33
export class SchematicOption {
4-
constructor(private name: string, private value: boolean | string) {}
4+
constructor(
5+
private name: string,
6+
private value: boolean | string,
7+
) {}
58

69
get normalizedName() {
710
return normalizeToKebabOrSnakeCase(this.name);

0 commit comments

Comments
 (0)