microsoft / react-native-test-app

react-native-test-app provides an app for all supported platforms as a package
MIT License
586 stars 87 forks source link

feat(macos): add macOS support for config plugins #2160

Closed tido64 closed 1 month ago

tido64 commented 1 month ago

Description

I had this branch lying around for about a year or so. Rebased, and brushed up a bit.

Platforms affected

Test plan

  1. Enable config plugins:
    diff --git a/example/app.json b/example/app.json
    index 3b8992ea..8be080e1 100644
    --- a/example/app.json
    +++ b/example/app.json
    @@ -13,6 +13,9 @@
           "presentationStyle": "modal"
         }
       ],
    +  "plugins": [
    +    "./test-plugin.js"
    +  ],
       "resources": {
         "android": [
           "dist/res",
    diff --git a/example/package.json b/example/package.json
    index 35447f2e..b02cc291 100644
    --- a/example/package.json
    +++ b/example/package.json
    @@ -29,6 +29,7 @@
       "devDependencies": {
         "@babel/core": "^7.1.6",
         "@babel/preset-env": "^7.1.6",
    +    "@expo/config-plugins": "^8.0.0",
         "@react-native/babel-preset": "^0.73.19",
         "@react-native/metro-config": "^0.73.3",
         "@rnx-kit/metro-config": "^1.3.15",
  2. Create example/test-plugin.js:

    const { createRunOncePlugin, } = require("@expo/config-plugins");
    const { mergeContents } = require("@expo/config-plugins/build/utils/generateCode");
    const { macos } = require("react-native-test-app/plugins/index");
    
    function withCustomAppDelegate(config) {
      return macos.withAppDelegate(config, (config) => {
        config.modResults.contents = mergeContents({
          tag: "test-plugin.js",
          src: config.modResults.contents,
          newSrc: "// added by test-plugin.js",
          anchor: /import Cocoa/,
          offset: 1,
          comment: "//",
        }).contents;
        return config;
      });
    }
    
    module.exports = createRunOncePlugin(
      withCustomAppDelegate,
      "test-plugin.js",
      "UNVERSIONED"
    );
  3. Under example folder, run pod install --project-directory=macos
  4. Verify that AppDelegate.swift was written to:

    diff --git a/macos/ReactTestApp/AppDelegate.swift b/macos/ReactTestApp/AppDelegate.swift
    index 73a7288e..95f936ae 100644
    --- a/macos/ReactTestApp/AppDelegate.swift
    +++ b/macos/ReactTestApp/AppDelegate.swift
    @@ -1,4 +1,7 @@
     import Cocoa
    +// @generated begin test-plugin.js - expo prebuild (DO NOT MODIFY) sync-bf0d29fe87baaf78ed9ffca2f64bacb7c675534f
    +// added by test-plugin.js
    +// @generated end test-plugin.js
     import ReactTestApp_DevSupport
    
     @main