Newer Version Available
Preview Components in Custom Mobile Apps
The best way to learn about custom app previews is to study the examples at github.com/forcedotcom/LWC-Mobile-Samples. This repo contains sample native mobile apps built to host previews, and sample Lightning Web Components projects for previewing in those apps. Binding these artifacts together are the advanced parameters of the lwc:preview command.
HelloWorld: A Sample Lightning Web Component Project
- JSON configuration file: mobile-apps.json
- iOS bundle script file: configure_ios_test_app.ts
- Android bundle script file: configure_android_test_app.ts
Custom Native Sample Apps
In the apps folder of the LWC-Mobile-Samples repo, you can find the iOS and Android native apps that the HelloWorld component targets. These native apps are bare-bones projects that each add a container for Lightning web component previews to what’s essentially a template app. In iOS, the container is an instance of WKWebView. In Android, it’s a WebView object. These two classes—WKWebView and WebView—are implementation-dependent choices. For your own app, you can use whatever works best for hosting web content.
- Parse the configuration values and launch arguments from the JSON file.
- Use the launch arguments to create a preview URL. If the preview_server_enabled argument is true but the launch arguments don't include a web domain and port number, configure a URL based on a local private address and default port.
- Instantiate a web view object.
- To display the preview, send the URL with component name to the web view.
Where Do I Place the Configuration File?
To guarantee that the plug-in finds and uses your configuration file, follow these guidelines.
- Requirement (all cases): Name your configuration file mobile-apps.json. The plug-in doesn’t accept other configuration file names.
- Command-line usage
-
- Use the preview command’s optional -f and -d parameters
to specify a custom location:
- -f specifies the config file’s location. This path can be absolute or relative.
- -d specifies the project’s root directory.
- If you specify -f but not -d, the plug-in uses the value of -f to determine the file’s location.
- If you specify -f and -d, the plug-in can consider both values to determine the file’s location.
- If -f uses a relative path, the file location is calculated starting from the directory specified by -d. If -d isn’t specified, the path is calculated against the current working directory.
- If -f specifies an absolute path, the -d value is ignored in calculating the config file location.
- Use the preview command’s optional -f and -d parameters
to specify a custom location:
-
- Example 1
-
-f /../../mobile-apps.json -d /Users/jdoe/MyProject Result /Users/mobile-apps.json - Example 2
-
-f /Users/jdoe/OtherProject/mobile-apps.json -d Not specified Result /Users/jdoe/OtherProject/mobile-apps.json - Example 3
-
-f /Users/jdoe/OtherProject/mobile-apps.json -d /Users/jdoe/MyProject Result /Users/jdoe/OtherProject/mobile-apps.json - Example 4
-
-f mobile-apps.json -d Not specified Result <current_working_directory>/mobile-app.json
-
If the plug-in is unable to find the config file on the calculated path, it posts an error message showing the path that failed.
- VS Code usage
- In VS Code, you can’t specify a custom path for the config file. VS Code always looks for mobile-apps.json in the default root folder of the Lightning Web Components project.