Skip to content
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

feat: move require (local files) to source.uri #3535

Merged
merged 8 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test-build-docs.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

name: test build docs
name: Test Docs build
on:
workflow_dispatch:
pull_request:
Expand All @@ -8,7 +8,7 @@ on:
- 'docs/**'

jobs:
deploy-docs:
build-docs:
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down
11 changes: 10 additions & 1 deletion docs/pages/component/props.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -622,12 +622,21 @@ The docs for this prop are incomplete and will be updated as each option is inve

Example:

Pass directly the asset to play (deprecated)

```javascript
const sintel = require('./sintel.mp4');
source={ sintel };
```

Or by using an uri (starting from 6.0.0-beta.6)

source = {sintel};
```javascript
const sintel = require('./sintel.mp4');
source={{ uri: sintel }}
```


#### URI string

A number of URI schemes are supported by passing an object with a `uri` attribute.
Expand Down
5 changes: 4 additions & 1 deletion examples/basic/src/VideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ class VideoPlayer extends Component {
seekerWidth = 0;

srcAllPlatformList = [
require('./broadchurch.mp4'),
{
description: 'local file',
uri: require('./broadchurch.mp4'),
},
{
description: '(hls|live) red bull tv',
uri: 'https://rbmn-live.akamaized.net/hls/live/590964/BoRB-AT/master_928.m3u8',
Expand Down
3 changes: 2 additions & 1 deletion examples/basic/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"extends": "@react-native/typescript-config/tsconfig.json",
"compilerOptions": {
"paths": {
"react-native-video": ["../../src/index"]
"react-native-video": ["../../src/index"],
"react": ["./node_modules/@types/react"]
}
},
"include": ["src", "**/*.js"],
Expand Down
4 changes: 3 additions & 1 deletion src/types/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export type ReactVideoSourceProperties = {
};

export type ReactVideoSource = Readonly<
ReactVideoSourceProperties | NodeRequire
Omit<ReactVideoSourceProperties, 'uri'> & {
uri?: string | NodeRequire;
}
>;

export type DebugConfig = Readonly<{
Expand Down
16 changes: 12 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import type {Component, RefObject, ComponentClass} from 'react';
import {Image, findNodeHandle} from 'react-native';
import type {ImageSourcePropType} from 'react-native';
import {Image, findNodeHandle, type ImageSourcePropType} from 'react-native';
import type {ReactVideoSource, ReactVideoSourceProperties} from './types/video';

type Source = ImageSourcePropType | ReactVideoSource;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function generateHeaderForNative(obj?: Record<string, any>) {
if (!obj) {
Expand All @@ -13,14 +10,25 @@ export function generateHeaderForNative(obj?: Record<string, any>) {
return Object.entries(obj).map(([key, value]) => ({key, value}));
}

type Source = ImageSourcePropType | ReactVideoSource;

export function resolveAssetSourceForVideo(
source: Source,
): ReactVideoSourceProperties {
// This is deprecated, but we need to support it for backward compatibility
if (typeof source === 'number') {
return {
uri: Image.resolveAssetSource(source).uri,
};
}

if ('uri' in source && typeof source.uri === 'number') {
return {
...source,
uri: Image.resolveAssetSource(source.uri).uri,
};
}

return source as ReactVideoSourceProperties;
}

Expand Down
Loading