Platform-Specific Code
Hosanna UI is a TV platform framework designed primarily for Roku, but also supports Android and Samsung TV platforms. There are several ways to handle platform-specific logic and code in your project.
1. Platform-Specific File Extensions
You can split platform-specific code into separate files using extensions like .roku.js
, .android.js
, or .samsung.js
. However, the extension itself is not special—additional configuration is required.
To make these files work, you must configure your tsconfig.json
and vite.config.ts
to use the @hs-platform
special path:
Example tsconfig.json
:
{
"compilerOptions": {
// ...existing code...
"paths": {
"@hs-platform/PlatformAppInitializer": [ "./src/hosanna-ui/targets/web/app/AppInitializer.web.ts" ],
"@hs-platform/PlatformTaskAppInitializer": [ "./src/hosanna-ui/targets/web/app/TaskAppInitializer.web.ts" ]
}
// ...existing code...
}
}
Example vite.config.ts
:
export default defineConfig({
// ...existing code...
resolve: {
preserveSymlinks: true,
alias: {
'@hs-platform/PlatformAppInitializer': path.resolve(__dirname, 'src/hosanna-ui/targets/web/app/AppInitializer.web.ts'),
'@hs-platform/PlatformTaskAppInitializer': path.resolve(__dirname, 'src/hosanna-ui/targets/web/app/TaskAppInitializer.web.ts'),
}
},
// ...existing code...
});
Importing platform-specific modules:
import { PlatformAppInitializer } from '@hs-platform/PlatformAppInitializer';
2. Inline Platform-Specific Code
You can use the hs_native_roku
function to inject Roku-specific BrightScript code directly:
hs_native_roku(`
' brs code here
print "Hello from Roku!"
`)
3. Excluding Code for Specific Platforms
You can exclude entire files or individual lines from a specific platform using special comments.
Exclude an entire file:
// hs:exclude-from-platform roku
// ...rest of file...
Exclude a single line:
// hs:exclude-from-platform roku
console.log('This line will not be included for Roku');
4. Platform Detection at Runtime
Hosanna UI provides a module to detect the platform in which the app is running. Use this for small parts of platform-specific logic.
import { Platform } from 'hosanna-ui';
if (Platform.OS === 'roku') {
console.log('Running on Roku');
} else if (Platform.OS === 'android') {
console.log('Running on Android');
} else if (Platform.OS === 'samsung') {
console.log('Running on Samsung TV');
}