Skip to content

Commit d453726

Browse files
author
Fatme
authored
Merge pull request #4411 from NativeScript/fatme/hmr-by-default
feat: enable hmr by default for new projects
2 parents 89c3480 + a952a47 commit d453726

File tree

7 files changed

+156
-1
lines changed

7 files changed

+156
-1
lines changed

lib/common/services/commands-service.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,16 @@ export class CommandsService implements ICommandsService {
2727
private $staticConfig: Config.IStaticConfig,
2828
private $helpService: IHelpService,
2929
private $extensibilityService: IExtensibilityService,
30-
private $optionsTracker: IOptionsTracker) {
30+
private $optionsTracker: IOptionsTracker,
31+
private $projectDataService: IProjectDataService) {
32+
let projectData = null;
33+
try {
34+
projectData = this.$projectDataService.getProjectData();
35+
} catch (err) {
36+
this.$logger.trace(`Error while trying to get project data. More info: ${err}`);
37+
}
38+
39+
this.$options.setupOptions(projectData);
3140
}
3241

3342
public allCommands(opts: { includeDevCommands: boolean }): string[] {

lib/declarations.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ interface IOptions extends IRelease, IDeviceIdentifier, IJustLaunch, IAvd, IAvai
568568
link: boolean;
569569
analyticsLogFile: string;
570570
performance: Object;
571+
setupOptions(projectData: IProjectData): void;
571572
}
572573

573574
interface IEnvOptions {

lib/definitions/project.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ interface INsConfig {
7474
appPath?: string;
7575
appResourcesPath?: string;
7676
shared?: boolean;
77+
useLegacyWorkflow?: boolean;
7778
}
7879

7980
interface IProjectData extends ICreateProjectData {
@@ -99,6 +100,11 @@ interface IProjectData extends ICreateProjectData {
99100
*/
100101
isShared: boolean;
101102

103+
/**
104+
* Defines if the project has hmr enabled by default
105+
*/
106+
useLegacyWorkflow: boolean;
107+
102108
/**
103109
* Initializes project data with the given project directory. If none supplied defaults to --path option or cwd.
104110
* @param {string} projectDir Project root directory.

lib/options.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,35 @@ export class Options {
2121

2222
public options: IDictionary<IDashedOption>;
2323

24+
public setupOptions(projectData: IProjectData): void {
25+
if (this.argv.release && this.argv.hmr) {
26+
this.$errors.failWithoutHelp("The options --release and --hmr cannot be used simultaneously.");
27+
}
28+
29+
// HACK: temporary solution for 5.3.0 release (until the webpack only feature)
30+
const parsed = require("yargs-parser")(process.argv.slice(2), { 'boolean-negation': false });
31+
const noBundle = parsed && (parsed.bundle === false || parsed.bundle === 'false');
32+
if (noBundle && this.argv.hmr) {
33+
this.$errors.failWithoutHelp("The options --no-bundle and --hmr cannot be used simultaneously.");
34+
}
35+
36+
if (projectData && projectData.useLegacyWorkflow === false) {
37+
this.argv.bundle = this.argv.bundle !== undefined ? this.argv.bundle : "webpack";
38+
this.argv.hmr = !this.argv.release;
39+
}
40+
41+
// --no-hmr -> hmr: false or --hmr false -> hmr: 'false'
42+
const noHmr = parsed && (parsed.hmr === false || parsed.hmr === 'false');
43+
if (noHmr) {
44+
this.argv.hmr = false;
45+
}
46+
47+
if (noBundle) {
48+
this.argv.bundle = undefined;
49+
this.argv.hmr = false;
50+
}
51+
}
52+
2453
constructor(private $errors: IErrors,
2554
private $staticConfig: Config.IStaticConfig,
2655
private $settingsService: ISettingsService) {

lib/project-data.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export class ProjectData implements IProjectData {
6161
public buildXcconfigPath: string;
6262
public podfilePath: string;
6363
public isShared: boolean;
64+
public useLegacyWorkflow: boolean;
6465

6566
constructor(private $fs: IFileSystem,
6667
private $errors: IErrors,
@@ -135,6 +136,7 @@ export class ProjectData implements IProjectData {
135136
this.buildXcconfigPath = path.join(this.appResourcesDirectoryPath, this.$devicePlatformsConstants.iOS, constants.BUILD_XCCONFIG_FILE_NAME);
136137
this.podfilePath = path.join(this.appResourcesDirectoryPath, this.$devicePlatformsConstants.iOS, constants.PODFILE_NAME);
137138
this.isShared = !!(this.nsConfig && this.nsConfig.shared);
139+
this.useLegacyWorkflow = this.nsConfig && this.nsConfig.useLegacyWorkflow;
138140
return;
139141
}
140142

test/options.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,113 @@ describe("options", () => {
261261

262262
});
263263
});
264+
265+
describe("setupOptions", () => {
266+
const testCases = [
267+
{
268+
name: "no options are provided",
269+
args: [],
270+
data: [
271+
{ useLegacyWorkflow: undefined, expectedHmr: false, expectedBundle: false },
272+
{ useLegacyWorkflow: false, expectedHmr: true, expectedBundle: true },
273+
{ useLegacyWorkflow: true, expectedHmr: false, expectedBundle: false }
274+
]
275+
},
276+
{
277+
name: " --hmr is provided",
278+
args: ["--hmr"],
279+
data: [
280+
{ useLegacyWorkflow: undefined, expectedHmr: true, expectedBundle: true },
281+
{ useLegacyWorkflow: false, expectedHmr: true, expectedBundle: true },
282+
{ useLegacyWorkflow: true, expectedHmr: true, expectedBundle: true }
283+
]
284+
},
285+
{
286+
name: " --no-hmr is provided",
287+
args: ["--no-hmr"],
288+
data: [
289+
{ useLegacyWorkflow: undefined, expectedHmr: false, expectedBundle: false },
290+
{ useLegacyWorkflow: false, expectedHmr: false, expectedBundle: true },
291+
{ useLegacyWorkflow: true, expectedHmr: false, expectedBundle: false }
292+
]
293+
},
294+
{
295+
name: " --bundle is provided",
296+
args: ["--bundle"],
297+
data: [
298+
{ useLegacyWorkflow: undefined, expectedHmr: false, expectedBundle: true },
299+
{ useLegacyWorkflow: false, expectedHmr: true, expectedBundle: true },
300+
{ useLegacyWorkflow: true, expectedHmr: false, expectedBundle: true }
301+
]
302+
},
303+
{
304+
name: " --no-bundle is provided",
305+
args: ["--no-bundle"],
306+
data: [
307+
{ useLegacyWorkflow: undefined, expectedHmr: false, expectedBundle: false },
308+
{ useLegacyWorkflow: false, expectedHmr: false, expectedBundle: false },
309+
{ useLegacyWorkflow: true, expectedHmr: false, expectedBundle: false }
310+
]
311+
},
312+
{
313+
name: " --release is provided",
314+
args: ["--release"],
315+
data: [
316+
{ useLegacyWorkflow: undefined, expectedHmr: false, expectedBundle: false },
317+
{ useLegacyWorkflow: false, expectedHmr: false, expectedBundle: true },
318+
{ useLegacyWorkflow: true, expectedHmr: false, expectedBundle: false }
319+
]
320+
}
321+
];
322+
323+
_.each([undefined, false, true], useLegacyWorkflow => {
324+
_.each(testCases, testCase => {
325+
it(`should pass correctly when ${testCase.name} and useLegacyWorkflow is ${useLegacyWorkflow}`, () => {
326+
(testCase.args || []).forEach(arg => process.argv.push(arg));
327+
328+
const options = createOptions(testInjector);
329+
const projectData = <IProjectData>{ useLegacyWorkflow };
330+
options.setupOptions(projectData);
331+
332+
(testCase.args || []).forEach(arg => process.argv.pop());
333+
334+
const data = testCase.data.find(item => item.useLegacyWorkflow === useLegacyWorkflow);
335+
336+
assert.equal(!!options.argv.hmr, !!data.expectedHmr);
337+
assert.equal(!!options.argv.bundle, !!data.expectedBundle);
338+
});
339+
});
340+
});
341+
342+
const testCasesExpectingToThrow = [
343+
{
344+
name: "--release --hmr",
345+
args: ["--release", "--hmr"],
346+
expectedError: "The options --release and --hmr cannot be used simultaneously."
347+
},
348+
{
349+
name: "--no-bundle --hmr",
350+
args: ["--no-bundle", "--hmr"],
351+
expectedError: "The options --no-bundle and --hmr cannot be used simultaneously."
352+
}
353+
];
354+
355+
_.each(testCasesExpectingToThrow, testCase => {
356+
it(`should fail when ${testCase.name}`, () => {
357+
let actualError = null;
358+
const errors = testInjector.resolve("errors");
359+
errors.failWithoutHelp = (error: string) => actualError = error;
360+
(testCase.args || []).forEach(arg => process.argv.push(arg));
361+
362+
const options = createOptions(testInjector);
363+
options.setupOptions(null);
364+
365+
(testCase.args || []).forEach(arg => process.argv.pop());
366+
367+
assert.deepEqual(actualError, testCase.expectedError);
368+
});
369+
});
370+
});
264371
});
265372

266373
function createOptionsWithProfileDir(defaultProfileDir?: string): IOptions {

test/stubs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ export class ProjectDataStub implements IProjectData {
331331
public buildXcconfigPath: string;
332332
public podfilePath: string;
333333
public isShared: boolean;
334+
public useLegacyWorkflow: boolean;
334335

335336
public initializeProjectData(projectDir?: string): void {
336337
this.projectDir = this.projectDir || projectDir;

0 commit comments

Comments
 (0)