Skip to content

Commit ccee8e8

Browse files
authored
fix: detect core version before including inspector_modules on android (#10520)
1 parent e234ca6 commit ccee8e8

File tree

12 files changed

+70
-58
lines changed

12 files changed

+70
-58
lines changed

packages/webpack5/__tests__/configuration/__snapshots__/angular.spec.ts.snap

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,6 @@ exports[`angular configuration for android 1`] = `
435435
'__jest__/src/app.js',
436436
'@nativescript/core/ui/frame',
437437
'@nativescript/core/ui/frame/activity'
438-
],
439-
'tns_modules/inspector_modules': [
440-
'@nativescript/core/inspector_modules'
441438
]
442439
}
443440
}"

packages/webpack5/__tests__/configuration/__snapshots__/base.spec.ts.snap

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,6 @@ exports[`base configuration for android 1`] = `
323323
'__jest__/src/app.js',
324324
'@nativescript/core/ui/frame',
325325
'@nativescript/core/ui/frame/activity'
326-
],
327-
'tns_modules/inspector_modules': [
328-
'@nativescript/core/inspector_modules'
329326
]
330327
}
331328
}"

packages/webpack5/__tests__/configuration/__snapshots__/javascript.spec.ts.snap

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,6 @@ exports[`javascript configuration for android 1`] = `
322322
'__jest__/src/app.js',
323323
'@nativescript/core/ui/frame',
324324
'@nativescript/core/ui/frame/activity'
325-
],
326-
'tns_modules/inspector_modules': [
327-
'@nativescript/core/inspector_modules'
328325
]
329326
}
330327
}"

packages/webpack5/__tests__/configuration/__snapshots__/react.spec.ts.snap

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,6 @@ exports[`react configuration > android > adds ReactRefreshWebpackPlugin when HMR
352352
'__jest__/src/app.js',
353353
'@nativescript/core/ui/frame',
354354
'@nativescript/core/ui/frame/activity'
355-
],
356-
'tns_modules/inspector_modules': [
357-
'@nativescript/core/inspector_modules'
358355
]
359356
}
360357
}"
@@ -683,9 +680,6 @@ exports[`react configuration > android > base config 1`] = `
683680
'__jest__/src/app.js',
684681
'@nativescript/core/ui/frame',
685682
'@nativescript/core/ui/frame/activity'
686-
],
687-
'tns_modules/inspector_modules': [
688-
'@nativescript/core/inspector_modules'
689683
]
690684
}
691685
}"

packages/webpack5/__tests__/configuration/__snapshots__/svelte.spec.ts.snap

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,6 @@ exports[`svelte configuration for android 1`] = `
344344
'__jest__/src/app.js',
345345
'@nativescript/core/ui/frame',
346346
'@nativescript/core/ui/frame/activity'
347-
],
348-
'tns_modules/inspector_modules': [
349-
'@nativescript/core/inspector_modules'
350347
]
351348
}
352349
}"

packages/webpack5/__tests__/configuration/__snapshots__/typescript.spec.ts.snap

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,6 @@ exports[`typescript configuration for android 1`] = `
322322
'__jest__/src/app.js',
323323
'@nativescript/core/ui/frame',
324324
'@nativescript/core/ui/frame/activity'
325-
],
326-
'tns_modules/inspector_modules': [
327-
'@nativescript/core/inspector_modules'
328325
]
329326
}
330327
}"

packages/webpack5/__tests__/configuration/__snapshots__/vue.spec.ts.snap

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,6 @@ exports[`vue configuration for android 1`] = `
357357
'__jest__/src/app.js',
358358
'@nativescript/core/ui/frame',
359359
'@nativescript/core/ui/frame/activity'
360-
],
361-
'tns_modules/inspector_modules': [
362-
'@nativescript/core/inspector_modules'
363360
]
364361
}
365362
}"

packages/webpack5/__tests__/configuration/base.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import fs from 'fs';
44
import base from '../../src/configuration/base';
55
import { init } from '../../src';
66
import { applyFileReplacements } from '../../src/helpers/fileReplacements';
7+
import * as dependenciesHelpers from '../../src/helpers/dependencies';
78
import { additionalCopyRules } from '../../src/helpers/copyRules';
89

910
describe('base configuration', () => {
@@ -221,4 +222,30 @@ describe('base configuration', () => {
221222
expect(config.output.get('sourceMapFilename')).toMatchSnapshot();
222223
expect(config.get('devtool')).toBe('hidden-source-map');
223224
});
225+
226+
it('includes inspector_modules on android when @nativescript/core version is >= 8.7.0', () => {
227+
const getDependencyVersionSpy = jest.spyOn(
228+
dependenciesHelpers,
229+
'getDependencyVersion'
230+
);
231+
getDependencyVersionSpy.withImplementation(
232+
(name) => {
233+
if (name === '@nativescript/core') {
234+
return '8.7.0';
235+
}
236+
return null;
237+
},
238+
() => {
239+
init({
240+
android: true,
241+
});
242+
243+
const config = base(new Config());
244+
const entry = config.entryPoints.get('tns_modules/inspector_modules');
245+
246+
expect(entry).toBeDefined();
247+
expect(entry.values().length).toBe(1);
248+
}
249+
);
250+
});
224251
});

packages/webpack5/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"sass": "^1.0.0",
4242
"sass-loader": "^13.0.0",
4343
"sax": "^1.0.0",
44+
"semver": "^7.0.0 || ^6.0.0",
4445
"source-map": "^0.7.0",
4546
"terser-webpack-plugin": "^5.0.0",
4647
"ts-dedent": "^2.0.0",

packages/webpack5/src/configuration/angular.ts

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { extname, resolve } from 'path';
22
import Config from 'webpack-chain';
3+
import { satisfies } from 'semver';
34
import { existsSync } from 'fs';
45

56
import { getTypescript, readTsConfig } from '../helpers/typescript';
6-
import { getDependencyPath } from '../helpers/dependencies';
7+
import { getDependencyVersion } from '../helpers/dependencies';
78
import { getProjectTSConfigPath } from '../helpers/project';
89
import { env as _env, IWebpackEnv } from '../index';
910
import { warnOnce } from '../helpers/log';
@@ -187,13 +188,16 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
187188
.loader('angular-hot-loader');
188189
});
189190

190-
const buildAngularMajorVersion = getBuildAngularMajorVersion();
191-
if (buildAngularMajorVersion) {
191+
const buildAngularVersion = getDependencyVersion(
192+
'@angular-devkit/build-angular'
193+
);
194+
195+
if (buildAngularVersion) {
192196
const buildAngularOptions: any = {
193197
aot: !disableAOT,
194198
};
195199

196-
if (buildAngularMajorVersion < 15) {
200+
if (satisfies(buildAngularVersion, '<15.0.0')) {
197201
const tsConfig = readTsConfig(tsConfigPath);
198202
const { ScriptTarget } = getTypescript();
199203
buildAngularOptions.scriptTarget =
@@ -315,27 +319,6 @@ function getAngularWebpackPlugin(): any {
315319
return AngularWebpackPlugin;
316320
}
317321

318-
const MAJOR_VER_RE = /^(\d+)\./;
319-
function getBuildAngularMajorVersion() {
320-
const buildAngularPath = getDependencyPath('@angular-devkit/build-angular');
321-
if (!buildAngularPath) {
322-
return null;
323-
}
324-
325-
try {
326-
const buildAngularVersion =
327-
require(`${buildAngularPath}/package.json`).version;
328-
329-
const [_, majorStr] = buildAngularVersion.match(MAJOR_VER_RE);
330-
331-
return Number(majorStr);
332-
} catch (err) {
333-
// ignore
334-
}
335-
336-
return null;
337-
}
338-
339322
function tryRequireResolve(path: string) {
340323
try {
341324
return require.resolve(path);
@@ -348,10 +331,10 @@ function getWebpackLoaderPath() {
348331
return (
349332
tryRequireResolve(
350333
'@angular-devkit/build-angular/src/babel/webpack-loader'
351-
) ||
334+
) ??
352335
tryRequireResolve(
353336
'@angular-devkit/build-angular/src/tools/babel/webpack-loader'
354-
) ||
337+
) ??
355338
// fallback to angular 16.1+
356339
'@angular-devkit/build-angular/src/tools/babel/webpack-loader'
357340
);

packages/webpack5/src/configuration/base.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ import {
55
HotModuleReplacementPlugin,
66
} from 'webpack';
77
import Config from 'webpack-chain';
8+
import { satisfies } from 'semver';
89
import { existsSync } from 'fs';
910

1011
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
1112
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
1213
import TerserPlugin from 'terser-webpack-plugin';
1314

1415
import { getProjectFilePath, getProjectTSConfigPath } from '../helpers/project';
16+
import { getDependencyVersion, hasDependency } from '../helpers/dependencies';
1517
import { PlatformSuffixPlugin } from '../plugins/PlatformSuffixPlugin';
1618
import { applyFileReplacements } from '../helpers/fileReplacements';
1719
import { addCopyRule, applyCopyRules } from '../helpers/copyRules';
1820
import { WatchStatePlugin } from '../plugins/WatchStatePlugin';
19-
import { hasDependency } from '../helpers/dependencies';
2021
import { applyDotEnvPlugin } from '../helpers/dotEnv';
2122
import { env as _env, IWebpackEnv } from '../index';
2223
import { getValue } from '../helpers/config';
@@ -86,7 +87,7 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
8687
const sourceMapAbsolutePath = getProjectFilePath(
8788
`./${
8889
env.buildPath ?? 'platforms'
89-
}/${platform}-sourceMaps/[file].map[query]`,
90+
}/${platform}-sourceMaps/[file].map[query]`
9091
);
9192
const sourceMapRelativePath = relative(outputPath, sourceMapAbsolutePath);
9293
config.output.sourceMapFilename(sourceMapRelativePath);
@@ -272,7 +273,7 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
272273
const configFile = tsConfigPath
273274
? {
274275
configFile: tsConfigPath,
275-
}
276+
}
276277
: undefined;
277278

278279
// set up ts support
@@ -451,7 +452,7 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
451452
* +-----------------------------------------------------------------------------------------+
452453
*/
453454
/System.import\(\) is deprecated/,
454-
]),
455+
])
455456
);
456457

457458
// todo: refine defaults
@@ -516,7 +517,11 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
516517

517518
function shouldIncludeInspectorModules(): boolean {
518519
const platform = getPlatformName();
519-
// todo: check if core modules are external
520-
// todo: check if we are testing
521-
return platform === 'ios' || platform === 'android';
520+
const coreVersion = getDependencyVersion('@nativescript/core');
521+
522+
if (coreVersion && satisfies(coreVersion, '>=8.7.0')) {
523+
return platform === 'ios' || platform === 'android';
524+
}
525+
526+
return platform === 'ios';
522527
}

packages/webpack5/src/helpers/dependencies.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,23 @@ export function getDependencyPath(dependencyName: string): string | null {
4747
return null;
4848
}
4949
}
50+
51+
/**
52+
* Utility to get the version of a dependency.
53+
*
54+
* @param dependencyName
55+
* @returns string | null - version of the dependency or null if not found
56+
*/
57+
export function getDependencyVersion(dependencyName: string): string | null {
58+
const dependencyPath = getDependencyPath(dependencyName);
59+
if (!dependencyPath) {
60+
return null;
61+
}
62+
63+
try {
64+
return require(`${dependencyPath}/package.json`).version;
65+
} catch (err) {
66+
// ignore
67+
}
68+
return null;
69+
}

0 commit comments

Comments
 (0)