Skip to content

Commit 2b8d62c

Browse files
committed
feat: add env support to webpackCompilerService
1 parent f2caa9d commit 2b8d62c

File tree

9 files changed

+90
-39
lines changed

9 files changed

+90
-39
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"program": "${workspaceRoot}/lib/nativescript-cli.js",
1616

1717
// example commands
18-
"args": [ "run", "ios", "--path", "${workspaceRoot}/scratch/webpackApp", "--bundle"]
18+
"args": [ "run", "ios", "--path", "${workspaceRoot}/scratch/webpackApp", "--bundle", "--env.sourceMap"]
1919
// "args": [ "test", "android", "--justlaunch"]
2020
// "args": [ "platform", "add", "android@1.3.0", "--path", "cliapp"]
2121
// "args": [ "platform", "remove", "android", "--path", "cliapp"]

lib/definitions/livesync.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ declare global {
141141
/**
142142
* Describes a LiveSync operation.
143143
*/
144-
interface ILiveSyncInfo extends IProjectDir, IRelease, IOptionalSkipWatcher, IHasUseHotModuleReloadOption, IHasSyncToPreviewAppOption {
145-
webpackCompilerConfig: IWebpackCompilerConfig;
144+
interface ILiveSyncInfo extends IProjectDir, IEnvOptions, IRelease, IOptionalSkipWatcher, IHasUseHotModuleReloadOption, IHasSyncToPreviewAppOption {
146145
emulator?: boolean;
147146

148147
/**

lib/definitions/platform.d.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@ interface IPreparePlatformJSInfo extends IPreparePlatformCoreInfo, ICopyAppFiles
106106
interface IPlatformOptions extends IRelease, IHasUseHotModuleReloadOption {
107107
}
108108

109-
interface IShouldPrepareInfo extends IOptionalProjectChangesInfoComposition {
110-
platformInfo: IPreparePlatformInfo;
111-
}
112-
113109
interface IOptionalProjectChangesInfoComposition {
114110
changesInfo?: IProjectChangesInfo;
115111
}
@@ -118,10 +114,6 @@ interface IPreparePlatformCoreInfo extends IPreparePlatformInfoBase, IOptionalPr
118114
platformSpecificData: IPlatformSpecificData;
119115
}
120116

121-
interface IPreparePlatformInfo extends IPreparePlatformInfoBase, IPlatformConfig {
122-
webpackCompilerConfig: IWebpackCompilerConfig;
123-
}
124-
125117
interface IPlatformConfig {
126118
config: IPlatformOptions;
127119
}

lib/helpers/livesync-command-helper.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ export class LiveSyncCommandHelper implements ILiveSyncCommandHelper {
118118
skipWatcher: !this.$options.watch,
119119
clean: this.$options.clean,
120120
release: this.$options.release,
121-
webpackCompilerConfig: {
122-
env: this.$options.env
123-
},
121+
env: this.$options.env,
124122
timeout: this.$options.timeout,
125123
useHotModuleReload: this.$options.hmr,
126124
force: this.$options.force,

lib/services/platform/platform-watcher-service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export class PlatformWatcherService extends EventEmitter implements IPlatformWat
4545
private async startJSWatcherWithPrepare(platformData: IPlatformData, projectData: IProjectData, config: IWebpackCompilerConfig): Promise<void> {
4646
if (!this.watchersData[projectData.projectDir][platformData.platformNameLowerCase].webpackCompilerProcess) {
4747
this.$webpackCompilerService.on("webpackEmittedFiles", files => {
48-
console.log("=============== WEBPACK EMITTED FILES ============");
4948
this.emitFilesChangeEvent({ files, hasNativeChanges: false, platform: platformData.platformNameLowerCase });
5049
});
5150

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

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
66
private webpackProcesses: IDictionary<child_process.ChildProcess> = {};
77

88
constructor(
9-
private $childProcess: IChildProcess
9+
private $childProcess: IChildProcess,
10+
private $projectData: IProjectData
1011
) { super(); }
1112

1213
public async compileWithWatch(platformData: IPlatformData, projectData: IProjectData, config: IWebpackCompilerConfig): Promise<any> {
@@ -34,7 +35,6 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
3435
const files = message.emittedFiles
3536
.filter((file: string) => file.indexOf("App_Resources") === -1)
3637
.map((file: string) => path.join(platformData.appDestinationDirectoryPath, "app", file));
37-
console.log("===================== BEFORE EMIT webpack files ", files);
3838
this.emit("webpackEmittedFiles", files);
3939
}
4040
});
@@ -76,26 +76,72 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
7676
}
7777

7878
private startWebpackProcess(platformData: IPlatformData, projectData: IProjectData, config: IWebpackCompilerConfig): child_process.ChildProcess {
79+
const envData = this.buildEnvData(platformData.platformNameLowerCase, config.env);
80+
const envParams = this.buildEnvCommandLineParams(envData);
81+
7982
const args = [
8083
path.join(projectData.projectDir, "node_modules", "webpack", "bin", "webpack.js"),
8184
"--preserve-symlinks",
8285
`--config=${path.join(projectData.projectDir, "webpack.config.js")}`,
83-
`--env.${platformData.normalizedPlatformName.toLowerCase()}`
84-
// `--env.unitTesting`
86+
...envParams
8587
];
8688

8789
if (config.watch) {
8890
args.push("--watch");
8991
}
9092

91-
// TODO: provide env variables
92-
9393
const stdio = config.watch ? ["inherit", "inherit", "inherit", "ipc"] : "inherit";
9494
const childProcess = this.$childProcess.spawn("node", args, { cwd: projectData.projectDir, stdio });
9595

9696
this.webpackProcesses[platformData.platformNameLowerCase] = childProcess;
9797

9898
return childProcess;
9999
}
100+
101+
private buildEnvData(platform: string, env: any) {
102+
const envData = Object.assign({},
103+
env,
104+
{ [platform.toLowerCase()]: true }
105+
);
106+
107+
const appPath = this.$projectData.getAppDirectoryRelativePath();
108+
const appResourcesPath = this.$projectData.getAppResourcesRelativeDirectoryPath();
109+
Object.assign(envData,
110+
appPath && { appPath },
111+
appResourcesPath && { appResourcesPath }
112+
);
113+
114+
return envData;
115+
}
116+
117+
private buildEnvCommandLineParams(envData: any) {
118+
const envFlagNames = Object.keys(envData);
119+
// const snapshotEnvIndex = envFlagNames.indexOf("snapshot");
120+
// if (snapshotEnvIndex > -1 && !utils.shouldSnapshot(config)) {
121+
// logSnapshotWarningMessage($logger);
122+
// envFlagNames.splice(snapshotEnvIndex, 1);
123+
// }
124+
125+
const args: any[] = [];
126+
envFlagNames.map(item => {
127+
let envValue = envData[item];
128+
if (typeof envValue === "undefined") {
129+
return;
130+
}
131+
if (typeof envValue === "boolean") {
132+
if (envValue) {
133+
args.push(`--env.${item}`);
134+
}
135+
} else {
136+
if (!Array.isArray(envValue)) {
137+
envValue = [envValue];
138+
}
139+
140+
envValue.map((value: any) => args.push(`--env.${item}=${value}`))
141+
}
142+
});
143+
144+
return args;
145+
}
100146
}
101147
$injector.register("webpackCompilerService", WebpackCompilerService);

lib/services/workflow/workflow-data-service.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export type AddPlatformData = Pick<any, 'platformParam'> & Partial<Pick<IOptions, 'frameworkPath'>> & Partial<Pick<any, 'nativePrepare'>>;
2+
export type PreparePlatformData = Pick<any, 'nativePrepare'> & Pick<IOptions, 'env' | 'release'>;
3+
export type IOSPrepareData = PreparePlatformData & Pick<IOptions, 'teamId' | 'provision'> & Pick<any, 'mobileProvisionData'>;
24

35
export class WorkflowDataService {
46
constructor(
@@ -15,7 +17,7 @@ export class WorkflowDataService {
1517
projectData,
1618
nativePlatformData,
1719
addPlatformData: this.getAddPlatformData("ios", options),
18-
preparePlatformData: new PreparePlatformData(options),
20+
preparePlatformData: this.getIOSPrepareData(options),
1921
buildPlatformData: new IOSBuildData(options),
2022
deployPlatformData: new DeployPlatformData(options),
2123
liveSyncData: {},
@@ -25,7 +27,7 @@ export class WorkflowDataService {
2527
projectData,
2628
nativePlatformData,
2729
addPlatformData: this.getAddPlatformData("android", options),
28-
preparePlatformData: new PreparePlatformData(options),
30+
preparePlatformData: this.getPreparePlatformData(options),
2931
buildPlatformData: new AndroidBuildData(options),
3032
deployPlatformData: new DeployPlatformData(options),
3133
liveSyncData: {},
@@ -43,6 +45,23 @@ export class WorkflowDataService {
4345
platformParam: options.platformParam || platform,
4446
};
4547
}
48+
49+
private getPreparePlatformData(options: IOptions | any) {
50+
return {
51+
env: options.env,
52+
release: options.release,
53+
nativePrepare: options.nativePrepare
54+
};
55+
}
56+
57+
private getIOSPrepareData(options: IOptions | any) {
58+
return {
59+
...this.getPreparePlatformData(options),
60+
teamId: options.teamId,
61+
provision: options.provision,
62+
mobileProvisionData: options.mobileProvisionData
63+
};
64+
}
4665
}
4766
$injector.register("workflowDataService", WorkflowDataService);
4867

@@ -65,21 +84,21 @@ export class WorkflowData {
6584
// public nativePrepare = this.options.nativePrepare;
6685
// }
6786

68-
export class PreparePlatformData {
69-
constructor(protected options: IOptions | any) { }
87+
// export class PreparePlatformData {
88+
// constructor(protected options: IOptions | any) { }
7089

71-
public env = this.options.env;
72-
public release = this.options.release;
73-
public nativePrepare = this.options.nativePrepare;
74-
}
90+
// public env = this.options.env;
91+
// public release = this.options.release;
92+
// public nativePrepare = this.options.nativePrepare;
93+
// }
7594

76-
export class IOSPrepareData extends PreparePlatformData {
77-
constructor(options: IOptions | any) { super(options); }
95+
// export class IOSPrepareData extends PreparePlatformData {
96+
// constructor(options: IOptions | any) { super(options); }
7897

79-
public teamId = this.options.teamId;
80-
public provision = this.options.provision;
81-
public mobileProvisionData = this.options.mobileProvisionData;
82-
}
98+
// public teamId = this.options.teamId;
99+
// public provision = this.options.provision;
100+
// public mobileProvisionData = this.options.mobileProvisionData;
101+
// }
83102

84103
export class BuildPlatformDataBase {
85104
constructor(protected options: IOptions | any) { }

test/controllers/main-controller.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ const liveSyncInfo = {
6969
projectDir,
7070
release: false,
7171
useHotModuleReload: false,
72-
webpackCompilerConfig: {
73-
env: {},
74-
}
72+
env: {}
7573
};
7674

7775
describe("MainController", () => {

test/tns-appstore-upload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class AppStore {
104104

105105
expectPreparePlatform() {
106106
this.expectedPreparePlatformCalls = 1;
107-
this.platformService.preparePlatform = (platformInfo: IPreparePlatformInfo) => {
107+
this.platformService.preparePlatform = (platformInfo: any) => {
108108
chai.assert.equal(platformInfo.platform, "iOS");
109109
this.preparePlatformCalls++;
110110
return Promise.resolve(true);

0 commit comments

Comments
 (0)