Skip to content

Commit 55f3f5b

Browse files
committed
feat: set webpack in production mode based on --release option
Implements: NativeScript/nativescript-dev-webpack#911
1 parent ad08b81 commit 55f3f5b

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

Diff for: lib/controllers/prepare-controller.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class PrepareController extends EventEmitter {
4141
if (prepareData.watch) {
4242
result = await this.startWatchersWithPrepare(platformData, projectData, prepareData);
4343
} else {
44-
await this.$webpackCompilerService.compileWithoutWatch(platformData, projectData, { watch: false, env: prepareData.env });
44+
await this.$webpackCompilerService.compileWithoutWatch(platformData, projectData, prepareData);
4545
await this.$prepareNativePlatformService.prepareNativePlatform(platformData, projectData, prepareData);
4646
}
4747

@@ -79,7 +79,7 @@ export class PrepareController extends EventEmitter {
7979
};
8080
}
8181

82-
await this.startJSWatcherWithPrepare(platformData, projectData, { env: prepareData.env }); // -> start watcher + initial compilation
82+
await this.startJSWatcherWithPrepare(platformData, projectData, prepareData); // -> start watcher + initial compilation
8383
const hasNativeChanges = await this.startNativeWatcherWithPrepare(platformData, projectData, prepareData); // -> start watcher + initial prepare
8484

8585
const result = { platform: platformData.platformNameLowerCase, hasNativeChanges };
@@ -97,13 +97,13 @@ export class PrepareController extends EventEmitter {
9797
return result;
9898
}
9999

100-
private async startJSWatcherWithPrepare(platformData: IPlatformData, projectData: IProjectData, config: IWebpackCompilerConfig): Promise<void> {
100+
private async startJSWatcherWithPrepare(platformData: IPlatformData, projectData: IProjectData, prepareData: IPrepareData): Promise<void> {
101101
if (!this.watchersData[projectData.projectDir][platformData.platformNameLowerCase].webpackCompilerProcess) {
102102
this.$webpackCompilerService.on(WEBPACK_COMPILATION_COMPLETE, data => {
103103
this.emitPrepareEvent({ ...data, hasNativeChanges: false, platform: platformData.platformNameLowerCase });
104104
});
105105

106-
const childProcess = await this.$webpackCompilerService.compileWithWatch(platformData, projectData, config);
106+
const childProcess = await this.$webpackCompilerService.compileWithWatch(platformData, projectData, prepareData);
107107
this.watchersData[projectData.projectDir][platformData.platformNameLowerCase].webpackCompilerProcess = childProcess;
108108
}
109109
}

Diff for: lib/services/webpack/webpack-compiler-service.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
1515
private $projectData: IProjectData,
1616
) { super(); }
1717

18-
public async compileWithWatch(platformData: IPlatformData, projectData: IProjectData, config: IWebpackCompilerConfig): Promise<any> {
18+
public async compileWithWatch(platformData: IPlatformData, projectData: IProjectData, prepareData: IPrepareData): Promise<any> {
1919
return new Promise(async (resolve, reject) => {
2020
if (this.webpackProcesses[platformData.platformNameLowerCase]) {
2121
resolve();
2222
return;
2323
}
2424

2525
let isFirstWebpackWatchCompilation = true;
26-
config.watch = true;
27-
const childProcess = await this.startWebpackProcess(platformData, projectData, config);
26+
prepareData.watch = true;
27+
const childProcess = await this.startWebpackProcess(platformData, projectData, prepareData);
2828

2929
childProcess.on("message", (message: any) => {
3030
if (message === "Webpack compilation complete.") {
@@ -69,14 +69,14 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
6969
});
7070
}
7171

72-
public async compileWithoutWatch(platformData: IPlatformData, projectData: IProjectData, config: IWebpackCompilerConfig): Promise<void> {
72+
public async compileWithoutWatch(platformData: IPlatformData, projectData: IProjectData, prepareData: IPrepareData): Promise<void> {
7373
return new Promise(async (resolve, reject) => {
7474
if (this.webpackProcesses[platformData.platformNameLowerCase]) {
7575
resolve();
7676
return;
7777
}
7878

79-
const childProcess = await this.startWebpackProcess(platformData, projectData, config);
79+
const childProcess = await this.startWebpackProcess(platformData, projectData, prepareData);
8080
childProcess.on("close", (arg: any) => {
8181
const exitCode = typeof arg === "number" ? arg : arg && arg.code;
8282
if (exitCode === 0) {
@@ -100,8 +100,8 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
100100

101101
@performanceLog()
102102
@hook('prepareJSApp')
103-
private async startWebpackProcess(platformData: IPlatformData, projectData: IProjectData, config: IWebpackCompilerConfig): Promise<child_process.ChildProcess> {
104-
const envData = this.buildEnvData(platformData.platformNameLowerCase, config.env);
103+
private async startWebpackProcess(platformData: IPlatformData, projectData: IProjectData, prepareData: IPrepareData): Promise<child_process.ChildProcess> {
104+
const envData = this.buildEnvData(platformData.platformNameLowerCase, prepareData);
105105
const envParams = this.buildEnvCommandLineParams(envData, platformData);
106106

107107
const args = [
@@ -111,19 +111,20 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
111111
...envParams
112112
];
113113

114-
if (config.watch) {
114+
if (prepareData.watch) {
115115
args.push("--watch");
116116
}
117117

118-
const stdio = config.watch ? ["inherit", "inherit", "inherit", "ipc"] : "inherit";
118+
const stdio = prepareData.watch ? ["inherit", "inherit", "inherit", "ipc"] : "inherit";
119119
const childProcess = this.$childProcess.spawn("node", args, { cwd: projectData.projectDir, stdio });
120120

121121
this.webpackProcesses[platformData.platformNameLowerCase] = childProcess;
122122

123123
return childProcess;
124124
}
125125

126-
private buildEnvData(platform: string, env: any) {
126+
private buildEnvData(platform: string, prepareData: IPrepareData) {
127+
const { env } = prepareData;
127128
const envData = Object.assign({},
128129
env,
129130
{ [platform.toLowerCase()]: true }
@@ -137,6 +138,7 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
137138
);
138139

139140
envData.verbose = this.$logger.isVerbose();
141+
envData.production = prepareData.release;
140142

141143
return envData;
142144
}

Diff for: lib/services/webpack/webpack.d.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@ import { PrepareData } from "../../data/prepare-data";
44

55
declare global {
66
interface IWebpackCompilerService extends EventEmitter {
7-
compileWithWatch(platformData: IPlatformData, projectData: IProjectData, config: IWebpackCompilerConfig): Promise<any>;
8-
compileWithoutWatch(platformData: IPlatformData, projectData: IProjectData, config: IWebpackCompilerConfig): Promise<void>;
7+
compileWithWatch(platformData: IPlatformData, projectData: IProjectData, prepareData: IPrepareData): Promise<any>;
8+
compileWithoutWatch(platformData: IPlatformData, projectData: IProjectData, prepareData: IPrepareData): Promise<void>;
99
stopWebpackCompiler(platform: string): void;
1010
}
1111

12-
interface IWebpackCompilerConfig {
13-
env: IWebpackEnvOptions;
14-
watch?: boolean;
15-
}
16-
1712
interface IWebpackEnvOptions {
1813
sourceMap?: boolean;
1914
uglify?: boolean;
15+
production?: boolean;
2016
}
2117

2218
interface IProjectChangesService {

0 commit comments

Comments
 (0)