Skip to content

feat: enable hmr by default for new projects #4411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/common/services/commands-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export class CommandsService implements ICommandsService {
private $staticConfig: Config.IStaticConfig,
private $helpService: IHelpService,
private $extensibilityService: IExtensibilityService,
private $optionsTracker: IOptionsTracker) {
private $optionsTracker: IOptionsTracker,
private $projectDataService: IProjectDataService) {
const projectData = this.$projectDataService.getProjectData();
this.$options.setupOptions(projectData);
}

public allCommands(opts: { includeDevCommands: boolean }): string[] {
Expand Down
1 change: 1 addition & 0 deletions lib/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ interface IOptions extends IRelease, IDeviceIdentifier, IJustLaunch, IAvd, IAvai
link: boolean;
analyticsLogFile: string;
performance: Object;
setupOptions(projectData: IProjectData): void;
}

interface IEnvOptions {
Expand Down
6 changes: 6 additions & 0 deletions lib/definitions/project.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ interface INsConfig {
appPath?: string;
appResourcesPath?: string;
shared?: boolean;
isHmrEnabledByDefault?: boolean;
}

interface IProjectData extends ICreateProjectData {
Expand All @@ -99,6 +100,11 @@ interface IProjectData extends ICreateProjectData {
*/
isShared: boolean;

/**
* Defines if the project has hmr enabled by default
*/
isHmrEnabledByDefault: boolean;

/**
* Initializes project data with the given project directory. If none supplied defaults to --path option or cwd.
* @param {string} projectDir Project root directory.
Expand Down
20 changes: 20 additions & 0 deletions lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ export class Options {

public options: IDictionary<IDashedOption>;

public setupOptions(projectData: IProjectData): void {
if (this.argv.release && this.argv.hmr) {
this.$errors.failWithoutHelp("The options --release and --hmr cannot be used simultaneously.");
}

// HACK: temporary solution for 5.3.0 release (until the webpack only feature)
const parsed = require("yargs-parser")(process.argv.slice(2), { 'boolean-negation': false });
// --no-hmr -> hmr: false or --hmr false -> hmr: 'false'
const noHmr = parsed && (parsed.hmr === false || parsed.hmr === 'false');
if (noHmr) {
this.argv.hmr = false;
return;
}

if (projectData.isHmrEnabledByDefault) {
this.argv.bundle = this.argv.bundle !== undefined ? this.argv.bundle : "webpack";
this.argv.hmr = !this.argv.release;
}
}

constructor(private $errors: IErrors,
private $staticConfig: Config.IStaticConfig,
private $settingsService: ISettingsService) {
Expand Down
2 changes: 2 additions & 0 deletions lib/project-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class ProjectData implements IProjectData {
public buildXcconfigPath: string;
public podfilePath: string;
public isShared: boolean;
public isHmrEnabledByDefault: boolean;

constructor(private $fs: IFileSystem,
private $errors: IErrors,
Expand Down Expand Up @@ -135,6 +136,7 @@ export class ProjectData implements IProjectData {
this.buildXcconfigPath = path.join(this.appResourcesDirectoryPath, this.$devicePlatformsConstants.iOS, constants.BUILD_XCCONFIG_FILE_NAME);
this.podfilePath = path.join(this.appResourcesDirectoryPath, this.$devicePlatformsConstants.iOS, constants.PODFILE_NAME);
this.isShared = !!(this.nsConfig && this.nsConfig.shared);
this.isHmrEnabledByDefault = !!(this.nsConfig && this.nsConfig.isHmrEnabledByDefault);
return;
}

Expand Down
92 changes: 92 additions & 0 deletions test/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,98 @@ describe("options", () => {

});
});

describe("setupOptions", () => {
const testsWithNoHmrByDefault = [
{
name: "no nsconfig.json ",
isHmrEnabledByDefault: false,
args: [],
expectedHmr: false,
expectedBundle: false
},
{
name: "no nsconfig.json and --hmr is provided",
isHmrEnabledByDefault: false,
args: ["--hmr"],
expectedHmr: true,
expectedBundle: true
},
{
name: "no nsconfig.json and --no-hmr is provided",
isHmrEnabledByDefault: false,
args: ["--no-hmr"],
expectedHmr: false,
expectedBundle: false
},
{
name: "no nsconfig.json and --release is provided",
isHmrEnabledByDefault: false,
args: ["--release"],
expectedHmr: false,
expectedBundle: false
},
{
name: "no nsconfig.json and --bundle is provided",
isHmrEnabledByDefault: false,
args: ["--bundle"],
expectedHmr: false,
expectedBundle: true
}
];
const testsWithHmrByDefault = <any>[
{
name: "has nsconfig.json",
isHmrEnabledByDefault: true,
expectedHmr: true,
expectedBundle: true
},
{
name: "has nsconfig.json and --hmr is provided",
isHmrEnabledByDefault: true,
args: ["--hmr"],
expectedHmr: true,
expectedBundle: true
},
{
name: "has nsconfig.json and --no-hmr is provided",
isHmrEnabledByDefault: true,
args: ["--no-hmr"],
expectedHmr: false,
expectedBundle: false
},
{
name: "has nsconfig.json and --release is provided",
isHmrEnabledByDefault: true,
args: ["--release"],
expectedHmr: false,
expectedBundle: true
},
{
name: "has nsconfig.json and --bundle is provided",
isHmrEnabledByDefault: true,
args: ["--bundle"],
expectedHmr: true,
expectedBundle: true
}
];
const testCases = testsWithNoHmrByDefault.concat(testsWithHmrByDefault);

_.each(testCases, testCase => {
it(`should pass correctly when ${testCase.name}`, () => {
(testCase.args || []).forEach(arg => process.argv.push(arg));

const options = createOptions(testInjector);
const projectData = <IProjectData>{ isHmrEnabledByDefault: testCase.isHmrEnabledByDefault };
options.setupOptions(projectData);

(testCase.args || []).forEach(arg => process.argv.pop());

assert.equal(!!options.argv.hmr, !!testCase.expectedHmr);
assert.equal(!!options.argv.bundle, !!testCase.expectedBundle);
});
});
});
});

function createOptionsWithProfileDir(defaultProfileDir?: string): IOptions {
Expand Down
1 change: 1 addition & 0 deletions test/stubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ export class ProjectDataStub implements IProjectData {
public buildXcconfigPath: string;
public podfilePath: string;
public isShared: boolean;
public isHmrEnabledByDefault: boolean;

public initializeProjectData(projectDir?: string): void {
this.projectDir = this.projectDir || projectDir;
Expand Down