1
0
mirror of https://github.com/videojs/video.js.git synced 2024-11-28 08:58:46 +02:00

refactor: require videojs-vtt.js via require rather than concat (#3919)

Using aliasify and require, we can generate the dist/video.js and dist/alt/video.novtt.js files each with and without vttjs while having the require-able version of video.js available to require vttjs and have it work with bundlers like browserify and webpack.
This commit is contained in:
Brandon Casey 2017-01-11 14:56:07 -05:00 committed by Gary Katsevman
parent f35de1c98d
commit d290db1765
3 changed files with 47 additions and 49 deletions

View File

@ -137,10 +137,6 @@ module.exports = function(grunt) {
], ],
tasks: ['copy:dist'] tasks: ['copy:dist']
}, },
novtt: {
files: ['build/temp/video.js'],
tasks: ['concat:novtt']
},
minify: { minify: {
files: ['build/temp/video.js'], files: ['build/temp/video.js'],
tasks: ['uglify'] tasks: ['uglify']
@ -149,10 +145,6 @@ module.exports = function(grunt) {
files: ['src/css/**/*'], files: ['src/css/**/*'],
tasks: ['skin'] tasks: ['skin']
}, },
babel: {
files: ['src/js/**/*.js'],
tasks: ['babel:es5']
}
}, },
connect: { connect: {
dev: { dev: {
@ -326,32 +318,39 @@ module.exports = function(grunt) {
} }
}, },
browserify: { browserify: {
options: browserifyGruntOptions(),
build: { build: {
options: browserifyGruntOptions(),
files: { files: {
'build/temp/video.js': ['es5/video.js'] 'build/temp/video.js': ['es5/video.js']
} }
}, },
dist: { buildnovtt: {
options: browserifyGruntOptions({transform: [
['aliasify', {aliases: {'videojs-vtt.js': false}}]
]}),
files: {
'build/temp/alt/video.novtt.js': ['es5/video.js']
}
},
watch: {
options: browserifyGruntOptions({ options: browserifyGruntOptions({
transform: [ watch: true,
['browserify-versionify', { keepAlive: true,
placeholder: '../node_modules/videojs-vtt.js/dist/vtt.js',
version: 'https://cdn.rawgit.com/gkatsev/vtt.js/vjs-v0.12.1/dist/vtt.min.js'
}],
]
}), }),
files: { files: {
'build/temp/video.js': ['es5/video.js'] 'build/temp/video.js': ['es5/video.js']
} }
}, },
watch: { watchnovtt: {
options: { options: browserifyGruntOptions({
transform: [
['aliasify', {aliases: {'videojs-vtt.js': false}}]
],
watch: true, watch: true,
keepAlive: true keepAlive: true,
}, }),
files: { files: {
'build/temp/video.js': ['es5/video.js'] 'build/temp/alt/video.novtt.js': ['es5/video.js']
} }
}, },
tests: { tests: {
@ -390,14 +389,6 @@ module.exports = function(grunt) {
options: { options: {
separator: '\n' separator: '\n'
}, },
novtt: {
src: ['build/temp/video.js'],
dest: 'build/temp/alt/video.novtt.js'
},
vtt: {
src: ['build/temp/video.js', 'node_modules/videojs-vtt.js/dist/vtt.js'],
dest: 'build/temp/video.js'
},
ie8_addition: { ie8_addition: {
src: ['build/temp/video-js.css', 'src/css/ie8.css'], src: ['build/temp/video-js.css', 'src/css/ie8.css'],
dest: 'build/temp/video-js.css' dest: 'build/temp/video-js.css'
@ -408,22 +399,23 @@ module.exports = function(grunt) {
logConcurrentOutput: true logConcurrentOutput: true
}, },
tests: [ tests: [
'watch:babel', 'shell:babel',
'browserify:tests' 'browserify:tests'
], ],
dev: [ dev: [
'shell:babel',
'browserify:watch', 'browserify:watch',
'browserify:watchnovtt',
'browserify:tests', 'browserify:tests',
'watch:novtt',
'watch:skin', 'watch:skin',
'watch:dist', 'watch:dist'
'shell:babel'
], ],
// Run multiple watch tasks in parallel // Run multiple watch tasks in parallel
// Needed so watchify can cache intelligently // Needed so watchify can cache intelligently
watchAll: [ watchAll: [
'watch', 'watch',
'browserify:watch', 'browserify:watch',
'browserify:watchnovtt',
'browserify:tests', 'browserify:tests',
'karma:watch' 'karma:watch'
], ],
@ -508,14 +500,13 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('chg'); grunt.loadNpmTasks('chg');
grunt.loadNpmTasks('grunt-accessibility'); grunt.loadNpmTasks('grunt-accessibility');
const buildDependents = [ grunt.registerTask('build', [
'shell:lint', 'shell:lint',
'clean:build', 'clean:build',
'babel:es5', 'babel:es5',
'browserify:build', 'browserify:build',
'concat:novtt', 'browserify:buildnovtt',
'concat:vtt',
'usebanner:novtt', 'usebanner:novtt',
'usebanner:vtt', 'usebanner:vtt',
'uglify', 'uglify',
@ -528,18 +519,11 @@ module.exports = function(grunt) {
'copy:swf', 'copy:swf',
'copy:ie8', 'copy:ie8',
'vjslanguages' 'vjslanguages'
]; ]);
grunt.registerTask('build', buildDependents);
grunt.registerTask(
'build:dist',
buildDependents.map(task => task === 'browserify:build' ? 'browserify:dist' : task)
);
grunt.registerTask('dist', [ grunt.registerTask('dist', [
'clean:dist', 'clean:dist',
'build:dist', 'build',
'copy:dist', 'copy:dist',
'copy:examples', 'copy:examples',
'zip:dist' 'zip:dist'
@ -575,9 +559,7 @@ module.exports = function(grunt) {
// Run while developing // Run while developing
grunt.registerTask('dev', ['connect:dev', 'concurrent:dev']); grunt.registerTask('dev', ['connect:dev', 'concurrent:dev']);
grunt.registerTask('watchAll', ['build', 'connect:dev', 'concurrent:watchAll']); grunt.registerTask('watchAll', ['build', 'connect:dev', 'concurrent:watchAll']);
grunt.registerTask('test-a11y', ['copy:a11y', 'accessibility']); grunt.registerTask('test-a11y', ['copy:a11y', 'accessibility']);
// Pick your testing, or run both in different terminals // Pick your testing, or run both in different terminals

View File

@ -46,6 +46,7 @@
"xhr": "2.2.2" "xhr": "2.2.2"
}, },
"devDependencies": { "devDependencies": {
"aliasify": "^2.1.0",
"babel-cli": "^6.11.4", "babel-cli": "^6.11.4",
"babel-plugin-inline-json": "^1.1.1", "babel-plugin-inline-json": "^1.1.1",
"babel-plugin-transform-es3-member-expression-literals": "^6.8.0", "babel-plugin-transform-es3-member-expression-literals": "^6.8.0",

View File

@ -17,6 +17,7 @@ import { bufferedPercent } from '../utils/buffer.js';
import MediaError from '../media-error.js'; import MediaError from '../media-error.js';
import window from 'global/window'; import window from 'global/window';
import document from 'global/document'; import document from 'global/document';
import {isPlain} from '../utils/obj';
/** /**
* An Object containing a structure like: `{src: 'url', type: 'mimetype'}` or string * An Object containing a structure like: `{src: 'url', type: 'mimetype'}` or string
@ -522,13 +523,27 @@ class Tech extends Component {
* *
* @fires Tech#vttjsloaded * @fires Tech#vttjsloaded
* @fires Tech#vttjserror * @fires Tech#vttjserror
* @fires Tech#texttrackchange
*/ */
addWebVttScript_() { addWebVttScript_() {
if (!window.WebVTT && this.el().parentNode !== null && this.el().parentNode !== undefined) { if (!window.WebVTT && this.el().parentNode !== null && this.el().parentNode !== undefined) {
const vtt = require('videojs-vtt.js');
// load via require if available and vtt.js script location was not passed in
// as an option. novtt builds will turn the above require call into an empty object
// which will cause this if check to always fail.
if (!this.options_['vtt.js'] && isPlain(vtt) && Object.keys(vtt).length > 0) {
Object.keys(vtt).forEach(function(k) {
window[k] = vtt[k];
});
this.trigger('vttjsloaded');
return;
}
// load vtt.js via the script location option or the cdn of no location was
// passed in
const script = document.createElement('script'); const script = document.createElement('script');
script.src = this.options_['vtt.js'] || '../node_modules/videojs-vtt.js/dist/vtt.js'; script.src = this.options_['vtt.js'] || 'https://cdn.rawgit.com/gkatsev/vtt.js/vjs-v0.12.1/dist/vtt.min.js';
script.onload = () => { script.onload = () => {
/** /**
* Fired when vtt.js is loaded. * Fired when vtt.js is loaded.