fix: detect core version before including inspector_modules on android (#10520)

This commit is contained in:
Igor Randjelovic
2024-04-16 14:20:14 +02:00
committed by GitHub
parent e234ca6052
commit ccee8e8166
12 changed files with 70 additions and 58 deletions

View File

@ -1,9 +1,10 @@
import { extname, resolve } from 'path';
import Config from 'webpack-chain';
import { satisfies } from 'semver';
import { existsSync } from 'fs';
import { getTypescript, readTsConfig } from '../helpers/typescript';
import { getDependencyPath } from '../helpers/dependencies';
import { getDependencyVersion } from '../helpers/dependencies';
import { getProjectTSConfigPath } from '../helpers/project';
import { env as _env, IWebpackEnv } from '../index';
import { warnOnce } from '../helpers/log';
@ -187,13 +188,16 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
.loader('angular-hot-loader');
});
const buildAngularMajorVersion = getBuildAngularMajorVersion();
if (buildAngularMajorVersion) {
const buildAngularVersion = getDependencyVersion(
'@angular-devkit/build-angular'
);
if (buildAngularVersion) {
const buildAngularOptions: any = {
aot: !disableAOT,
};
if (buildAngularMajorVersion < 15) {
if (satisfies(buildAngularVersion, '<15.0.0')) {
const tsConfig = readTsConfig(tsConfigPath);
const { ScriptTarget } = getTypescript();
buildAngularOptions.scriptTarget =
@ -315,27 +319,6 @@ function getAngularWebpackPlugin(): any {
return AngularWebpackPlugin;
}
const MAJOR_VER_RE = /^(\d+)\./;
function getBuildAngularMajorVersion() {
const buildAngularPath = getDependencyPath('@angular-devkit/build-angular');
if (!buildAngularPath) {
return null;
}
try {
const buildAngularVersion =
require(`${buildAngularPath}/package.json`).version;
const [_, majorStr] = buildAngularVersion.match(MAJOR_VER_RE);
return Number(majorStr);
} catch (err) {
// ignore
}
return null;
}
function tryRequireResolve(path: string) {
try {
return require.resolve(path);
@ -348,10 +331,10 @@ function getWebpackLoaderPath() {
return (
tryRequireResolve(
'@angular-devkit/build-angular/src/babel/webpack-loader'
) ||
) ??
tryRequireResolve(
'@angular-devkit/build-angular/src/tools/babel/webpack-loader'
) ||
) ??
// fallback to angular 16.1+
'@angular-devkit/build-angular/src/tools/babel/webpack-loader'
);

View File

@ -5,6 +5,7 @@ import {
HotModuleReplacementPlugin,
} from 'webpack';
import Config from 'webpack-chain';
import { satisfies } from 'semver';
import { existsSync } from 'fs';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
@ -12,11 +13,11 @@ import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import TerserPlugin from 'terser-webpack-plugin';
import { getProjectFilePath, getProjectTSConfigPath } from '../helpers/project';
import { getDependencyVersion, hasDependency } from '../helpers/dependencies';
import { PlatformSuffixPlugin } from '../plugins/PlatformSuffixPlugin';
import { applyFileReplacements } from '../helpers/fileReplacements';
import { addCopyRule, applyCopyRules } from '../helpers/copyRules';
import { WatchStatePlugin } from '../plugins/WatchStatePlugin';
import { hasDependency } from '../helpers/dependencies';
import { applyDotEnvPlugin } from '../helpers/dotEnv';
import { env as _env, IWebpackEnv } from '../index';
import { getValue } from '../helpers/config';
@ -86,7 +87,7 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
const sourceMapAbsolutePath = getProjectFilePath(
`./${
env.buildPath ?? 'platforms'
}/${platform}-sourceMaps/[file].map[query]`,
}/${platform}-sourceMaps/[file].map[query]`
);
const sourceMapRelativePath = relative(outputPath, sourceMapAbsolutePath);
config.output.sourceMapFilename(sourceMapRelativePath);
@ -272,7 +273,7 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
const configFile = tsConfigPath
? {
configFile: tsConfigPath,
}
}
: undefined;
// set up ts support
@ -451,7 +452,7 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
* +-----------------------------------------------------------------------------------------+
*/
/System.import\(\) is deprecated/,
]),
])
);
// todo: refine defaults
@ -516,7 +517,11 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
function shouldIncludeInspectorModules(): boolean {
const platform = getPlatformName();
// todo: check if core modules are external
// todo: check if we are testing
return platform === 'ios' || platform === 'android';
const coreVersion = getDependencyVersion('@nativescript/core');
if (coreVersion && satisfies(coreVersion, '>=8.7.0')) {
return platform === 'ios' || platform === 'android';
}
return platform === 'ios';
}

View File

@ -47,3 +47,23 @@ export function getDependencyPath(dependencyName: string): string | null {
return null;
}
}
/**
* Utility to get the version of a dependency.
*
* @param dependencyName
* @returns string | null - version of the dependency or null if not found
*/
export function getDependencyVersion(dependencyName: string): string | null {
const dependencyPath = getDependencyPath(dependencyName);
if (!dependencyPath) {
return null;
}
try {
return require(`${dependencyPath}/package.json`).version;
} catch (err) {
// ignore
}
return null;
}