-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add setup-module-windows command for automated Windows module scaffolding with comprehensive TurboModule stub generation #15118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
17864b1
Initial plan
Copilot c9e1eea
Add setup-module-windows command with cpp-app template support
Copilot 645a1f7
Add comprehensive documentation for setup-module-windows command
Copilot 7685aaa
Export setupModuleWindowsInternal from CLI index.ts
Copilot 3f6616a
Remove .xaml files from cpp-app template documentation
Copilot fda336c
Fix yarn and yarn build issues
Copilot 347f68a
Refactor setupModuleWindows.ts by extracting functions into separate …
Copilot 328517a
Revert vnext/codegen changes and restore beachball-config engines req…
Copilot aa06852
Fix TypeScript compilation errors in setupModuleWindows command
Copilot 3673b03
Add logic to parse nativemodulespec.g.h files and fix path reporting …
Copilot ca76aee
Reorganize utils into subdirectory and add hello world method to gene…
Copilot 9d67ee3
Remove all changes in vnext/codegen directory
Copilot 8c0753e
Fix init-windows multiple execution issue by handling problematic exa…
Copilot 02639e3
Set overwrite flag to true for init-windows in setup-module-windows c…
Copilot 45c45b9
Fix fs.exists import pattern in setup-module-windows to use ES6 imports
Copilot 56fcf71
Complete import consistency fixes in setup-module-windows utils
Copilot 08eb305
Fix TypeScript compilation errors in setupModuleWindows test mocks
Copilot 32ee87a
Improve stub function generation with proper callback signatures and …
Copilot 60b0cc3
Remove all changes in vnext/codegen directory as requested
Copilot c1469af
Remove cleaning and installing dependencies step from setup-module-wi…
Copilot dd13b19
Fix codegen directory detection and module name display in setup-modu…
Copilot e214378
Change files
anupriya13 473a6d5
Remove unused skipDeps and skipBuild parameters and fix formatting
Copilot 6dfe853
lint fixes
anupriya13 fb908eb
Update documentation to remove skip-deps and skip-build options and r…
Copilot d321494
Fix multiple init-windows logging by preventing spinner conflicts and…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@react-native-windows-cli-add-setup-module-windows-command.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "prerelease", | ||
| "comment": "Add setup-module-windows command with cpp-app template support", | ||
| "packageName": "@react-native-windows/cli", | ||
| "email": "[email protected]", | ||
| "dependentChangeType": "patch" | ||
| } |
247 changes: 247 additions & 0 deletions
247
packages/@react-native-windows/cli/src/commands/setupModuleWindows/EXAMPLE.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,247 @@ | ||
| # Complete Example: Setting up Windows support for react-native-webview | ||
|
|
||
| This example demonstrates how to use the `setup-module-windows` command to add Windows support to the popular `react-native-webview` community module. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| You must have a TurboModule spec file before running the setup command. The command will fail if no spec file exists. | ||
|
|
||
| ## Step 1: Create TurboModule Spec File | ||
|
|
||
| First, create the spec file (`NativeReactNativeWebview.ts`): | ||
|
|
||
| ```typescript | ||
| /** | ||
| * Copyright (c) Microsoft Corporation. | ||
| * Licensed under the MIT License. | ||
| * @format | ||
| */ | ||
|
|
||
| import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport'; | ||
| import {TurboModuleRegistry} from 'react-native'; | ||
|
|
||
| export interface Spec extends TurboModule { | ||
| goBack(): void; | ||
| goForward(): void; | ||
| reload(): void; | ||
| stopLoading(): void; | ||
| loadUrl(url: string): void; | ||
| evaluateJavaScript(script: string): Promise<string>; | ||
| } | ||
|
|
||
| export default TurboModuleRegistry.getEnforcing<Spec>('ReactNativeWebview'); | ||
| ``` | ||
|
|
||
| ## Step 2: Run the Setup Command | ||
|
|
||
| ### For Library Module (default) | ||
| ```bash | ||
| cd react-native-webview | ||
| yarn react-native setup-module-windows --logging | ||
| ``` | ||
|
|
||
| ### For App Module | ||
| ```bash | ||
| cd react-native-webview | ||
| yarn react-native setup-module-windows --template cpp-app --logging | ||
| ``` | ||
|
|
||
| ## Step 3: Command Output | ||
|
|
||
| ``` | ||
| ✔ Setting up Windows support for React Native module... | ||
| [SetupModuleWindows] Validating environment... | ||
| [SetupModuleWindows] Project name: react-native-webview | ||
| [SetupModuleWindows] Yarn found | ||
| [SetupModuleWindows] Checking for TurboModule spec file... | ||
| [SetupModuleWindows] Found valid spec file(s): NativeReactNativeWebview.ts | ||
| [SetupModuleWindows] Extracted actual module name: ReactNativeWebview | ||
| [SetupModuleWindows] Added codegenConfig to package.json with module name: ReactNativeWebview | ||
| [SetupModuleWindows] Installing dependencies... | ||
| [SetupModuleWindows] Dependencies installed | ||
| [SetupModuleWindows] Running init-windows with cpp-lib template... | ||
| [SetupModuleWindows] init-windows completed successfully | ||
| [SetupModuleWindows] Running codegen-windows... | ||
| [SetupModuleWindows] codegen-windows completed successfully | ||
| [SetupModuleWindows] Generating C++ stub files... | ||
| [SetupModuleWindows] Generated header stub: /path/to/windows/ReactNativeWebview/ReactNativeWebview.h | ||
| [SetupModuleWindows] Generated cpp stub: /path/to/windows/ReactNativeWebview/ReactNativeWebview.cpp | ||
|
|
||
| 🎉 Your React Native module now supports Windows! | ||
|
|
||
| Files created/updated: | ||
| 📄 package.json - Added codegen configuration | ||
| 💻 windows/ReactNativeWebview/ReactNativeWebview.h - C++ header file (implement your methods here) | ||
| ⚙️ windows/ReactNativeWebview/ReactNativeWebview.cpp - C++ implementation file (add your logic here) | ||
|
|
||
| Next steps: | ||
| 1. 📝 Update the generated spec file with your module's interface | ||
| 2. 🔧 Implement the methods in the generated C++ stub files | ||
| 3. 🏗️ Build your project to verify everything works | ||
| 4. 📚 See the documentation for more details on TurboModule development | ||
|
|
||
| For help, visit: https://microsoft.github.io/react-native-windows/ | ||
| ``` | ||
|
|
||
| ## Step 4: Final Project Structure | ||
|
|
||
| After running the command, your project will have this structure: | ||
|
|
||
| ### For cpp-lib template (default): | ||
| ``` | ||
| react-native-webview/ | ||
| ├── package.json (updated with codegenConfig) | ||
| ├── NativeReactNativeWebview.ts (your spec file) | ||
| ├── windows/ | ||
| │ ├── ReactNativeWebview/ | ||
| │ │ ├── ReactNativeWebview.h (generated) | ||
| │ │ └── ReactNativeWebview.cpp (generated) | ||
| │ └── ReactNativeWebview.sln (from init-windows) | ||
| ├── codegen/ | ||
| │ └── ReactNativeWebviewSpec.g.h (from codegen-windows) | ||
| └── README.md | ||
| ``` | ||
|
|
||
| ### For cpp-app template: | ||
| ``` | ||
| react-native-webview/ | ||
| ├── package.json (updated with codegenConfig) | ||
| ├── NativeReactNativeWebview.ts (your spec file) | ||
| ├── windows/ | ||
| │ ├── App.xaml | ||
| │ ├── MainWindow.xaml | ||
| │ ├── ReactNativeWebview/ | ||
| │ │ ├── ReactNativeWebview.h (generated) | ||
| │ │ └── ReactNativeWebview.cpp (generated) | ||
| │ └── ReactNativeWebview.sln (from init-windows) | ||
| ├── codegen/ | ||
| │ └── ReactNativeWebviewSpec.g.h (from codegen-windows) | ||
| └── README.md | ||
| ``` | ||
|
|
||
| ## Step 5: Generated Files Content | ||
|
|
||
| ### Header File (ReactNativeWebview.h) | ||
|
|
||
| ```cpp | ||
| #pragma once | ||
|
|
||
| #include "pch.h" | ||
| #include "resource.h" | ||
|
|
||
| #include "codegen/NativeReactNativeWebviewSpec.g.h" | ||
| #include "NativeModules.h" | ||
|
|
||
| namespace winrt::ReactNativeWebviewSpecs | ||
| { | ||
|
|
||
| REACT_MODULE(ReactNativeWebview) | ||
| struct ReactNativeWebview | ||
| { | ||
| using ModuleSpec = ReactNativeWebviewCodegen::ReactNativeWebviewSpec; | ||
|
|
||
| REACT_INIT(Initialize) | ||
| void Initialize(React::ReactContext const &reactContext) noexcept; | ||
|
|
||
| // Reference function for demonstration (from cpp-lib template) | ||
| // double multiply(double a, double b) noexcept { return a * b; } | ||
|
|
||
| // Hello World example to verify module functionality | ||
| REACT_METHOD(sayHello) | ||
| void sayHello(std::string name, std::function<void(std::string)> const & callback) noexcept; | ||
|
|
||
| // TODO: Add your method implementations here | ||
|
|
||
| private: | ||
| React::ReactContext m_context; | ||
| }; | ||
|
|
||
| } // namespace winrt::ReactNativeWebviewSpecs | ||
| ``` | ||
|
|
||
| ### Implementation File (ReactNativeWebview.cpp) | ||
|
|
||
| ```cpp | ||
| #include "ReactNativeWebview.h" | ||
|
|
||
| namespace winrt::ReactNativeWebviewSpecs { | ||
|
|
||
| void ReactNativeWebview::Initialize(React::ReactContext const &reactContext) noexcept { | ||
| m_context = reactContext; | ||
| } | ||
|
|
||
| void ReactNativeWebview::sayHello(std::string name, std::function<void(std::string)> const & callback) noexcept { | ||
| std::string result = "Hello " + name + "! Module is working."; | ||
| callback(result); | ||
| } | ||
|
|
||
| // TODO: Implement your methods here | ||
|
|
||
| } // namespace winrt::ReactNativeWebviewSpecs | ||
| ``` | ||
|
|
||
| ## Step 6: Build and Test | ||
|
|
||
| ```bash | ||
| # Build the Windows project | ||
| cd windows | ||
| msbuild ReactNativeWebview.sln | ||
|
|
||
| # Or use Visual Studio to open and build the solution | ||
| ``` | ||
|
|
||
| ## Template Differences | ||
|
|
||
| ### cpp-lib Template (Default) | ||
| - **Use case**: Native modules that provide functionality to React Native apps | ||
| - **Structure**: Library project with module files in subdirectory | ||
| - **Best for**: Community modules, utility libraries, platform bridges | ||
|
|
||
| ### cpp-app Template | ||
| - **Use case**: Standalone Windows applications with React Native UI | ||
| - **Structure**: Full application project with XAML UI files | ||
| - **Best for**: Complete Windows apps, enterprise applications | ||
|
|
||
| ## Command Options | ||
|
|
||
| ### Template Selection | ||
| ```bash | ||
| # Use cpp-lib template (default) | ||
| yarn react-native setup-module-windows | ||
|
|
||
| # Use cpp-app template | ||
| yarn react-native setup-module-windows --template cpp-app | ||
|
|
||
| # Use cpp-lib template explicitly | ||
| yarn react-native setup-module-windows --template cpp-lib | ||
| ``` | ||
|
|
||
| ### Other Options | ||
| ```bash | ||
| # Verbose logging | ||
| yarn react-native setup-module-windows --logging | ||
|
|
||
| # Skip dependency upgrades | ||
| yarn react-native setup-module-windows --skip-deps | ||
|
|
||
| # Skip build verification | ||
| yarn react-native setup-module-windows --skip-build | ||
|
|
||
| # Combine options | ||
| yarn react-native setup-module-windows --template cpp-app --logging --skip-deps | ||
| ``` | ||
|
|
||
| ## Error Handling | ||
|
|
||
| If you get the error: **"Create Spec File - TurboModule spec file not found"** | ||
|
|
||
| This means you need to create a TurboModule spec file first. The command will not automatically create spec files - you must create them manually before running the setup. | ||
|
|
||
| ## Benefits of Using setup-module-windows | ||
|
|
||
| 1. **Template Flexibility**: Choose between library and application project structures | ||
| 2. **Validation**: Ensures TurboModule spec exists before proceeding | ||
| 3. **Best Practices**: Generated files follow RNW coding standards and patterns | ||
| 4. **Completeness**: Creates the entire Windows integration structure in one command | ||
| 5. **Consistency**: Ensures consistent naming and structure across all Windows modules | ||
| 6. **Reference Code**: Includes multiply function reference and working Hello World example | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the doc with latest changes and remove skipdeps etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated EXAMPLE.md with latest changes - removed references to skip-deps, skip-build options and dependency installation step from command output. Changes committed in fb908eb.