Skip to content

Commit

Permalink
feat(plugin): enable new architecture by default
Browse files Browse the repository at this point in the history
release-npm

BREAKING CHANGE: New React Native architecture enabled by default. Will require small migration in some cases. Use the "oldArchitecture": true option to keep using the old architecture.
  • Loading branch information
tobua committed Aug 30, 2024
1 parent 6314a37 commit 3eaba59
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 20 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ Adding a `numic` property allows to configure script and plugin behaviour. This
"androidVersion": 45,
"androidVersion": [8, "2.31"],
// Customize the Bundle ID for iOS and Android.
"bundleId": "com.tobua.numic"
"bundleId": "com.tobua.numic",
// Switch back to use the old React Native architecture.
"oldArchitecture": true
},
// Will be merged with tsconfig, that's by default gitignored.
"tsconfig": {
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
}
},
"dependencies": {
"@react-native-community/cli": "^14.0.0",
"@react-native/babel-preset": "^0.75.1",
"@react-native/eslint-config": "^0.75.1",
"@react-native/metro-config": "^0.75.1",
"@react-native/typescript-config": "^0.75.1",
"@react-native-community/cli": "^14.0.1",
"@react-native/babel-preset": "^0.75.2",
"@react-native/eslint-config": "^0.75.2",
"@react-native/metro-config": "^0.75.2",
"@react-native/typescript-config": "^0.75.2",
"arg": "^5.0.2",
"command-exists": "^1.2.9",
"deepmerge": "^4.3.1",
Expand All @@ -51,13 +51,13 @@
"skip-local-postinstall": "^2.0.4"
},
"devDependencies": {
"@types/bun": "^1.1.6",
"@types/bun": "^1.1.8",
"@types/command-exists": "^1.2.3",
"@types/prompts": "^2.4.9",
"@types/semver": "^7.5.8",
"jest-fixture": "^4.1.0",
"padua": "^4.0.1",
"react-native": "^0.75.1",
"react-native": "^0.75.2",
"vitest": "^2.0.5"
},
"peerDependencies": {
Expand Down
43 changes: 43 additions & 0 deletions plugin/new-architecture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path'

interface Options {
oldArchitecture?: boolean
}

interface PluginInput {
// Root project path.
projectPath?: string
// Location of /android or /ios folders, either root or inside /.numic.
nativePath?: string
log?: (message: string, type?: 'error' | 'warning') => void
options: Options
// Currently installed React Native version.
version?: string
}

export default async ({
nativePath = process.cwd(),
log = console.log,
options = {},
}: PluginInput) => {
const { oldArchitecture } = options
const gradlePropertiesFilePath = join(nativePath, 'android/gradle.properties')
let gradlePropertiesContents = readFileSync(gradlePropertiesFilePath, 'utf-8')

// Android requires gradle flag to be set, while iOS requires flag to be set during "pod install".
if (oldArchitecture) {
log('Using old architecture')
gradlePropertiesContents = gradlePropertiesContents.replaceAll(
'newArchEnabled=true',
'newArchEnabled=false',
)
} else {
log('Using new architecture')
gradlePropertiesContents = gradlePropertiesContents.replaceAll(
'newArchEnabled=false',
'newArchEnabled=true',
)
}
writeFileSync(gradlePropertiesFilePath, gradlePropertiesContents)
}
7 changes: 6 additions & 1 deletion script/ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
isOnline,
checkCommandVersion,
hasRejectedHunks,
options,
} from '../helper'
import { native } from './native'
import { patch } from './patch'
Expand Down Expand Up @@ -53,7 +54,11 @@ export const ios = async (inputs: RunInputs) => {
log('Updating iOS Pods')
if (await isOnline()) {
try {
execSync('pod update', { cwd: join(basePath(), 'ios'), encoding: 'utf8', stdio: 'pipe' })
execSync(`${options().oldArchitecture ? '' : 'RCT_NEW_ARCH_ENABLED=1 '}pod update`, {
cwd: join(basePath(), 'ios'),
encoding: 'utf8',
stdio: 'pipe',
})
} catch (error) {
log('Failed to run "pod update" in /ios', 'warning')
console.log(error.stdout)
Expand Down
5 changes: 3 additions & 2 deletions script/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { log, getFolders, basePath, options } from '../helper'
import type { PluginInput } from '../types'
import androidVersion from '../plugin/android-version'
import bundleId from '../plugin/bundle-id'
import newArchitecture from '../plugin/new-architecture'

const builtInPlugins = [androidVersion, bundleId]
const builtInPlugins = [androidVersion, bundleId, newArchitecture]

type PluginFunction = (options?: PluginInput) => void
type Plugin = string | PluginFunction
Expand All @@ -33,7 +34,7 @@ const runPluginsIn = async (plugins: Plugin[], location: string, silent = false)
projectPath: basePath(),
nativePath: location,
log: silent ? () => {} : log,
options: typeof plugin === 'function' ? options() : options()[basename(plugin)] ?? {},
options: typeof plugin === 'function' ? options() : (options()[basename(plugin)] ?? {}),
version: options().reactNativeVersion,
})
})
Expand Down
10 changes: 5 additions & 5 deletions template/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
"mobx": "^6.13.1",
"mobx-react-lite": "^4.0.7",
"react": "^18.3.1",
"react-native": "^0.75.1",
"react-native": "^0.75.2",
"reactigation": "^4.0.3",
"responsive-react-native": "^1.0.6"
},
"devDependencies": {
"@testing-library/jest-native": "^5.4.3",
"@testing-library/react-native": "^12.6.0",
"@testing-library/react-native": "^12.6.1",
"@types/jest": "^29.5.12",
"@types/react": "^18.3.3",
"@types/react": "^18.3.5",
"@types/react-native": "^0.73.0",
"android-sdk-numic-plugin": "^1.0.5",
"android-sdk-numic-plugin": "^1.0.6",
"babel-jest": "^29.7.0",
"icon-numic-plugin": "^1.4.4",
"jest": "^29.7.0",
"numic": "^2.4.1",
"numic": "^3.0.0",
"react-test-renderer": "^18.3.1",
"typescript": "^5.5.4"
},
Expand Down
8 changes: 4 additions & 4 deletions template/default/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
},
"dependencies": {
"react": "^18.3.1",
"react-native": "^0.75.1"
"react-native": "^0.75.2"
},
"type": "module",
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/react": "^18.3.3",
"@types/react": "^18.3.5",
"@types/react-native": "^0.73.0",
"@types/react-test-renderer": "^18.3.0",
"android-sdk-numic-plugin": "^1.0.5",
"android-sdk-numic-plugin": "^1.0.6",
"babel-jest": "^29.7.0",
"icon-numic-plugin": "^1.4.4",
"jest": "^29.7.0",
"numic": "^2.4.1",
"numic": "^3.0.0",
"react-test-renderer": "18.2.0",
"typescript": "^5.5.4"
},
Expand Down
16 changes: 16 additions & 0 deletions test/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,20 @@ test('Bundle ID will be adapted when configured.', async () => {

expect(iosProject).toContain('PRODUCT_BUNDLE_IDENTIFIER = com.tobua.numic;')
expect(iosProject).not.toContain('org.reactjs.native.example.$(PRODUCT_NAME:rfc1034identifier)')

const gradlePropertiesContents = readFile('android/gradle.properties')
// New architecture enabled by default.
expect(gradlePropertiesContents).toContain('newArchEnabled=true')
})

test('New architecture can optionally be disabled.', async () => {
prepare([
packageJson('plugin-new-architecture', { numic: { oldArchitecture: true } }),
reactNativePkg,
])

await native()

const gradlePropertiesContents = readFile('android/gradle.properties')
expect(gradlePropertiesContents).toContain('newArchEnabled=false')
})
1 change: 1 addition & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface Options {
nativeGitignore?: string[] | string
reactNativeVersion?: string
typescript: boolean
oldArchitecture?: boolean
}

export type NativeOptions = {
Expand Down

0 comments on commit 3eaba59

Please sign in to comment.