diff --git a/packages/core/config/config.interface.ts b/packages/core/config/config.interface.ts index 996979e6c..f12e6bd86 100644 --- a/packages/core/config/config.interface.ts +++ b/packages/core/config/config.interface.ts @@ -147,7 +147,7 @@ export interface NativeScriptConfig { */ id?: string; /** - * App's main entry file (currently ignored - set it in package.json main field) + * App's main entry file - this setting overrides the value set in package.json */ main?: string; /** diff --git a/packages/webpack5/__tests__/helpers/platform.spec.ts b/packages/webpack5/__tests__/helpers/platform.spec.ts new file mode 100644 index 000000000..58424c525 --- /dev/null +++ b/packages/webpack5/__tests__/helpers/platform.spec.ts @@ -0,0 +1,49 @@ +import { env } from '../../src/'; +import { addPlatform, getEntryPath } from '../../src/helpers/platform'; + +import { getValue } from '../../src/helpers/config'; + +describe('getEntryPath', () => { + it('uses platform getEntryPath if the platform specifies it', () => { + env.platform = 'testPlatform'; + addPlatform('testPlatform', { + getEntryPath() { + return 'custom-entry-path'; + }, + }); + + const res = getEntryPath(); + expect(res).toEqual('custom-entry-path'); + + // cleanup env + delete env.platform; + }); + + it('uses main from nativescript.config.ts if set', () => { + env.ios = true; + + // mock getValue + const getValueMock = getValue as jest.Mock; + const getValueMockImpl = getValueMock.getMockImplementation(); + + getValueMock.mockImplementation((key) => { + if (key === 'main') { + return 'main-from-config'; + } + }); + + const res = getEntryPath(); + expect(res).toEqual('__jest__/main-from-config'); + + // reset mock implementation + getValueMock.mockImplementation(getValueMockImpl); + }); + + it('uses main from package.json', () => { + env.ios = true; + + const res = getEntryPath(); + // set in jest.setup.ts mock for package.json... + expect(res).toEqual('__jest__/src/app.js'); + }); +}); diff --git a/packages/webpack5/scripts/jest.setup.ts b/packages/webpack5/scripts/jest.setup.ts index 65a90cfae..3f2ba5d44 100644 --- a/packages/webpack5/scripts/jest.setup.ts +++ b/packages/webpack5/scripts/jest.setup.ts @@ -13,10 +13,10 @@ jest.mock('cosmiconfig', () => ({ }, })); +const getValueMock = jest.fn(); +getValueMock.mockImplementation((key, defaultValue) => defaultValue); jest.mock('../src/helpers/config.ts', () => ({ - getValue(key, defaultValue) { - return defaultValue; - }, + getValue: getValueMock, })); jest.mock('os', () => { diff --git a/packages/webpack5/src/helpers/config.ts b/packages/webpack5/src/helpers/config.ts index ae649ead9..221d1feee 100644 --- a/packages/webpack5/src/helpers/config.ts +++ b/packages/webpack5/src/helpers/config.ts @@ -1,5 +1,5 @@ +import { warnOnce } from './log'; import { env } from '../index'; -import { error, warnOnce } from './log'; function getCLILib() { if (!env.nativescriptLibPath) { @@ -28,7 +28,9 @@ export function getValue(key: string, defaultValue?: any): T { return defaultValue; } - return (lib.projectConfigService as { - getValue(key: string, defaultValue?: any): T; - }).getValue(key, defaultValue); + return ( + lib.projectConfigService as { + getValue(key: string, defaultValue?: any): T; + } + ).getValue(key, defaultValue); } diff --git a/packages/webpack5/src/helpers/platform.ts b/packages/webpack5/src/helpers/platform.ts index 1e07b62b3..f077416d7 100644 --- a/packages/webpack5/src/helpers/platform.ts +++ b/packages/webpack5/src/helpers/platform.ts @@ -2,6 +2,7 @@ import { dirname, resolve } from 'path'; import { getPackageJson, getProjectRootPath } from './project'; import { error, info, warnOnce } from './log'; +import { getValue } from './config'; import { env } from '../'; import AndroidPlatform from '../platforms/android'; @@ -99,6 +100,13 @@ export function getEntryPath() { return platform.getEntryPath(); } + // try main from nativescript.config.ts + const main = getValue('main'); + + if (main) { + return resolve(getProjectRootPath(), main); + } + // fallback to main field in package.json const packageJson = getPackageJson();