Skip to content

Commit

Permalink
fix(player/react): media-captions should be safe to use on server
Browse files Browse the repository at this point in the history
  • Loading branch information
mihar-22 committed Oct 30, 2023
1 parent 18789bc commit b986bf8
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"react": "^18.0.0"
},
"dependencies": {
"media-captions": "^1.0.0"
"media-captions": "^1.0.1"
},
"devDependencies": {
"@maverick-js/cli": "0.40.9",
Expand Down
5 changes: 3 additions & 2 deletions packages/react/src/components/ui/sliders/time-slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import {
useStateContext,
type ReactElementProps,
} from 'maverick.js/react';
import { VTTCue } from 'media-captions';
import type { VTTCue } from 'media-captions';
import { mediaState } from 'vidstack';

import { createVTTCue } from '../../../utils';
import {
SliderChaptersInstance,
SliderThumbnailInstance,
Expand Down Expand Up @@ -141,7 +142,7 @@ function ChapterTracks({ instance, children }: ChapterTracksProps) {
{ $chapters } = React.useContext(TimeSliderContext);

if (!emptyCue.current) {
emptyCue.current = new VTTCue(0, 0, '');
emptyCue.current = createVTTCue();
}

React.useEffect(() => {
Expand Down
17 changes: 17 additions & 0 deletions packages/react/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { noop } from 'maverick.js/std';
import type { VTTCue } from 'media-captions';

export function createVTTCue(startTime = 0, endTime = 0, text = ''): VTTCue {
if (__SERVER__) {
return {
startTime,
endTime,
text,
addEventListener: noop,
removeEventListener: noop,
dispatchEvent: noop,
} as VTTCue;
}

return new window.VTTCue(startTime, endTime, text);
}
2 changes: 1 addition & 1 deletion packages/vidstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"watch:types": "pnpm run build:types -w"
},
"dependencies": {
"media-captions": "^1.0.0"
"media-captions": "^1.0.1"
},
"devDependencies": {
"@floating-ui/dom": "^1.4.4",
Expand Down
9 changes: 5 additions & 4 deletions packages/vidstack/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const MODE_WATCH = process.argv.includes('-w'),
MODE_TYPES = process.argv.includes('--config-types'),
MODE_CDN = process.argv.includes('--config-cdn');

const EXTERNAL_PACKAGES = ['hls.js', 'media-captions', 'media-icons'],
const NPM_EXTERNAL_PACKAGES = ['hls.js', 'media-captions', 'media-icons'],
CDN_EXTERNAL_PACKAGES = ['media-captions', 'media-icons'],
NPM_BUNDLES = [define({ type: 'server' }), define({ type: 'dev' }), define({ type: 'prod' })],
CDN_BUNDLES = [defineCDN({ dev: true }), defineCDN(), defineCDN({ layouts: true })],
TYPES_BUNDLES = [defineTypes()];
Expand Down Expand Up @@ -64,7 +65,7 @@ function defineTypes() {
}
},
},
external: EXTERNAL_PACKAGES,
external: NPM_EXTERNAL_PACKAGES,
plugins: [dts({ respectExternal: true })],
};
}
Expand Down Expand Up @@ -117,7 +118,7 @@ function define({ target, type, minify }) {
maxParallelFileOps: shouldMangle ? 1 : 20,
treeshake: true,
preserveEntrySignatures: 'strict',
external: EXTERNAL_PACKAGES,
external: NPM_EXTERNAL_PACKAGES,
output: {
format: 'esm',
dir: `dist/${type.replace('local-', '')}`,
Expand Down Expand Up @@ -220,7 +221,7 @@ function defineCDN({ dev = false, layouts = false } = {}) {
return null;
},
},
external: ['media-icons', 'media-captions'],
external: CDN_EXTERNAL_PACKAGES,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
type Scope,
} from 'maverick.js';
import { animationFrameThrottle } from 'maverick.js/std';
import { VTTCue } from 'media-captions';
import type { VTTCue } from 'media-captions';

import { observeActiveTextTrack } from '../../../../core';
import { useMediaContext, type MediaContext } from '../../../../core/api/media-context';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import throttle from 'just-throttle';
import { Component, effect, peek, provideContext, signal, useContext } from 'maverick.js';
import { isNull, setAttribute } from 'maverick.js/std';
import { setAttribute } from 'maverick.js/std';

import { useMediaContext, type MediaContext } from '../../../../core/api/media-context';
import type { TextTrack } from '../../../../core/tracks/text/text-track';
Expand Down
12 changes: 6 additions & 6 deletions packages/vidstack/src/core/tracks/text/text-track.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { DOMEvent, EventsTarget, isNumber } from 'maverick.js/std';
import {
type CaptionsFileFormat,
type CaptionsParserFactory,
type VTTCue,
type VTTHeaderMetadata,
type VTTRegion,
import type {
CaptionsFileFormat,
CaptionsParserFactory,
VTTCue,
VTTHeaderMetadata,
VTTRegion,
} from 'media-captions';

import { getRequestCredentials } from '../../../utils/network';
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b986bf8

Please sign in to comment.