Skip to content

Commit 1eb4dd8

Browse files
committed
fix(react-native): preserve config fallbacks
1 parent 0cbc2a6 commit 1eb4dd8

3 files changed

Lines changed: 100 additions & 4 deletions

File tree

packages/core/src/checks/react-native/utils/read-android-release-shrinking.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,15 @@ export const readAndroidReleaseShrinking = (
165165
try {
166166
contents = stripGradleComments(fs.readFileSync(absoluteFilePath, "utf-8"));
167167
} catch {
168-
return null;
168+
continue;
169169
}
170170

171171
const blockDetectionContents = maskGradleStrings(
172172
contents.replace(/(["'])release\1/g, RELEASE_STRING_MARKER),
173173
);
174174
const releaseBlocks = getReleaseBlocks(blockDetectionContents);
175175
const [releaseBlock] = releaseBlocks;
176-
if (releaseBlock === undefined || releaseBlocks.length !== 1) return null;
176+
if (releaseBlock === undefined || releaseBlocks.length !== 1) continue;
177177
const hasDisabledMinification =
178178
getFinalStaticBoolean(releaseBlock, MINIFY_ASSIGNMENT_PATTERN) === false;
179179
const hasDisabledResourceShrinking =

packages/core/src/checks/react-native/utils/read-static-babel-plugin-names.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,17 @@ export const readStaticBabelPluginNames = (
156156
const absoluteFilePath = path.join(rootDirectory, fileName);
157157
if (!isFile(absoluteFilePath)) continue;
158158
try {
159+
const pluginNames = parseConfigPluginNames(
160+
fileName,
161+
fs.readFileSync(absoluteFilePath, "utf-8"),
162+
);
163+
if (pluginNames === null) continue;
159164
return {
160165
filePath: fileName,
161-
pluginNames: parseConfigPluginNames(fileName, fs.readFileSync(absoluteFilePath, "utf-8")),
166+
pluginNames,
162167
};
163168
} catch {
164-
return { filePath: fileName, pluginNames: null };
169+
continue;
165170
}
166171
}
167172

packages/core/tests/check-react-native-project.test.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,52 @@ describe("checkReactNativeProject — Babel plugin order", () => {
687687
).toContain("rn-react-compiler-plugin-first");
688688
});
689689

690+
it("falls back to a lower-priority static Babel config", () => {
691+
const projectDirectory = makeProjectDirectory();
692+
writePackageJson(projectDirectory, {
693+
name: "rn-app",
694+
dependencies: { "react-native": "0.82.0" },
695+
});
696+
writeFile(projectDirectory, "babel.config.js", `module.exports = { plugins: [`);
697+
writeFile(
698+
projectDirectory,
699+
"babel.config.cjs",
700+
`module.exports = { plugins: ['other-plugin', 'babel-plugin-react-compiler'] };`,
701+
);
702+
703+
expect(
704+
rulesOf(checkReactNativeProject(projectDirectory, buildRnProject(projectDirectory))),
705+
).toContain("rn-react-compiler-plugin-first");
706+
});
707+
708+
it("falls back to a static package.json Babel config", () => {
709+
const projectDirectory = makeProjectDirectory();
710+
writePackageJson(projectDirectory, {
711+
name: "rn-app",
712+
dependencies: { "react-native": "0.82.0" },
713+
babel: { plugins: ["other-plugin", "babel-plugin-react-compiler"] },
714+
});
715+
writeFile(projectDirectory, "babel.config.js", `module.exports = getConfig();`);
716+
717+
expect(
718+
rulesOf(checkReactNativeProject(projectDirectory, buildRnProject(projectDirectory))),
719+
).toContain("rn-react-compiler-plugin-first");
720+
});
721+
722+
it("keeps the first statically readable Babel config authoritative", () => {
723+
const projectDirectory = makeProjectDirectory();
724+
writePackageJson(projectDirectory, {
725+
name: "rn-app",
726+
dependencies: { "react-native": "0.82.0" },
727+
babel: { plugins: ["other-plugin", "babel-plugin-react-compiler"] },
728+
});
729+
writeFile(projectDirectory, "babel.config.js", `module.exports = { plugins: [] };`);
730+
731+
expect(
732+
rulesOf(checkReactNativeProject(projectDirectory, buildRnProject(projectDirectory))),
733+
).not.toContain("rn-react-compiler-plugin-first");
734+
});
735+
690736
it("does NOT infer plugin order from a dynamic plugin array", () => {
691737
const projectDirectory = makeProjectDirectory();
692738
writePackageJson(projectDirectory, {
@@ -901,4 +947,49 @@ describe("checkReactNativeProject — Android release shrinking", () => {
901947
).not.toContain("rn-android-release-shrinking-disabled");
902948
});
903949

950+
it.each([
951+
["missing", `android { buildTypes { debug { minifyEnabled false } } }`],
952+
[
953+
"ambiguous",
954+
`android { buildTypes { release { minifyEnabled true } release { minifyEnabled false } } }`,
955+
],
956+
])("falls back after a %s release block", (_caseName, groovyBuildContents) => {
957+
const projectDirectory = makeProjectDirectory();
958+
writePackageJson(projectDirectory, {
959+
name: "rn-app",
960+
dependencies: { "react-native": "0.82.0" },
961+
});
962+
writeFile(projectDirectory, "android/app/build.gradle", groovyBuildContents);
963+
writeFile(
964+
projectDirectory,
965+
"android/app/build.gradle.kts",
966+
`android { buildTypes { getByName("release") { isMinifyEnabled = false } } }`,
967+
);
968+
969+
expect(
970+
rulesOf(checkReactNativeProject(projectDirectory, buildRnProject(projectDirectory))),
971+
).toContain("rn-android-release-shrinking-disabled");
972+
});
973+
974+
it("keeps the first statically readable Gradle file authoritative", () => {
975+
const projectDirectory = makeProjectDirectory();
976+
writePackageJson(projectDirectory, {
977+
name: "rn-app",
978+
dependencies: { "react-native": "0.82.0" },
979+
});
980+
writeFile(
981+
projectDirectory,
982+
"android/app/build.gradle",
983+
`android { buildTypes { release { minifyEnabled true } } }`,
984+
);
985+
writeFile(
986+
projectDirectory,
987+
"android/app/build.gradle.kts",
988+
`android { buildTypes { getByName("release") { isMinifyEnabled = false } } }`,
989+
);
990+
991+
expect(
992+
rulesOf(checkReactNativeProject(projectDirectory, buildRnProject(projectDirectory))),
993+
).not.toContain("rn-android-release-shrinking-disabled");
994+
});
904995
});

0 commit comments

Comments
 (0)