8000 feat(webpack): read nativescript.config.ts main if set before fallbac… · NativeScript/NativeScript@61ff7e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 61ff7e4

Browse files
authored
feat(webpack): read nativescript.config.ts main if set before fallback to package.json (#9769)
implements #9658 BREAKING CHANGES: Possibly breaking if a project has a main field set in the nativescript.config.ts - since after this lands, this value will be used instead of package.json main. The impact is likely very small, the steps to migrate: (Option A) remove main from nativescript.config.ts if set (Option B) update main to the correct path in nativescript.config.ts if set incorrectly
1 parent cb7bd2a commit 61ff7e4

File tree

5 files changed

+67
-8
lines changed

5 files changed

+67
-8
lines changed

packages/core/config/config.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export interface NativeScriptConfig {
147147
*/
148148
id?: string;
149149
/**
150-
* App's main entry file (currently ignored - set it in package.json main field)
150+
* App's main entry file - this setting overrides the value set in package.json
151151
*/
152152
main?: string;
153153
/**
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { env } from '../../src/';
2+
import { addPlatform, getEntryPath } from '../../src/helpers/platform';
3+
4+
import { getValue } from '../../src/helpers/config';
5+
6+
describe('getEntryPath', () => {
7+
it('uses platform getEntryPath if the platform specifies it', () => {
8+
env.platform = 'testPlatform';
9+
addPlatform('testPlatform', {
10+
getEntryPath() {
11+
return 'custom-entry-path';
12+
},
13+
});
14+
15+
const res = getEntryPath();
16+
expect(res).toEqual('custom-entry-path');
17+
18+
// cleanup env
19+
delete env.platform;
20+
});
21+
22+
it('uses main from nativescript.config.ts if set', () => {
23+
env.ios = true;
24+
25+
// mock getValue
26+
const getValueMock = getValue as jest.Mock;
27+
const getValueMockImpl = getValueMock.getMockImplementation();
28+
29+
getValueMock.mockImplementation((key) => {
30+
if (key === 'main') {
31+
return 'main-from-config';
32+
}
33+
});
34+
35+
const res = getEntryPath();
36+
expect(res).toEqual('__jest__/main-from-config');
37+
38+
// reset mock implementation
39+
getValueMock.mockImplementation(getValueMockImpl);
40+
});
41+
42+
it('uses main from package.json', () => {
43+
env.ios = true;
44+
45+
const res = getEntryPath();
46+
// set in jest.setup.ts mock for package.json...
47+
expect(res).toEqual('__jest__/src/app.js');
48+
});
49+
});

packages/webpack5/scripts/jest.setup.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ jest.mock('cosmiconfig', () => ({
1313
},
1414
}));
1515

16+
const getValueMock = jest.fn();
17+
getValueMock.mockImplementation((key, defaultValue) => defaultValue);
1618
jest.mock('../src/helpers/config.ts', () => ({
17-
getValue(key, defaultValue) {
18-
return defaultValue;
19-
},
19+
getValue: getValueMock,
2020
}));
2121

2222
jest.mock('os', () => {

packages/webpack5/src/helpers/config.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { warnOnce } from './log';
12
import { env } from '../index';
2-
import { error, warnOnce } from './log';
33

44
function getCLILib() {
55
if (!env.nativescriptLibPath) {
@@ -28,7 +28,9 @@ export function getValue<T = any>(key: string, defaultValue?: any): T {
2828
return defaultValue;
2929
}
3030

31-
return (lib.projectConfigService as {
32-
getValue(key: string, defaultValue?: any): T;
33-
}).getValue(key, defaultValue);
31+
return (
32+
lib.projectConfigService as {
33+
getValue(key: string, defaultValue?: any): T;
34+
}
35+
).getValue(key, defaultValue);
3436
}

packages/webpack5/src/helpers/platform.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { dirname, resolve } from 'path';
22

33
import { getPackageJson, getProjectRootPath } from './project';
44
import { error, info, warnOnce } from './log';
5+
import { getValue } from './config';
56
import { env } from '../';
67

78
import AndroidPlatform from '../platforms/android';
@@ -99,6 +100,13 @@ export function getEntryPath() {
99100
return platform.getEntryPath();
100101
}
101102

103+
// try main from nativescript.config.ts
104+
const main = getValue('main');
105+
106+
if (main) {
107+
return resolve(getProjectRootPath(), main);
108+
}
109+
102110
// fallback to main field in package.json
103111
const packageJson = getPackageJson();
104112

0 commit comments

Comments
 (0)
0