Skip to content

Commit b5dcf44

Browse files
authored
Merge pull request #4660 from NativeScript/fatme/webpack-production-mode
feat: set webpack in production mode based on `--release` option
2 parents 75085a4 + 045df51 commit b5dcf44

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

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
}

lib/services/webpack/webpack-compiler-service.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
1414
private $logger: ILogger
1515
) { super(); }
1616

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

2424
let isFirstWebpackWatchCompilation = true;
25-
config.watch = true;
26-
const childProcess = await this.startWebpackProcess(platformData, projectData, config);
25+
prepareData.watch = true;
26+
const childProcess = await this.startWebpackProcess(platformData, projectData, prepareData);
2727

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

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

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

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

106106
const args = [
@@ -110,19 +110,20 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
110110
...envParams
111111
];
112112

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

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

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

122122
return childProcess;
123123
}
124124

125-
private buildEnvData(platform: string, env: any, projectData: IProjectData) {
125+
private buildEnvData(platform: string, projectData: IProjectData, prepareData: IPrepareData) {
126+
const { env } = prepareData;
126127
const envData = Object.assign({},
127128
env,
128129
{ [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
}

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)