Skip to content

Commit

Permalink
scripts: add start:fast script, allowing very fast demo building
Browse files Browse the repository at this point in the history
  • Loading branch information
peaBerberian committed Sep 15, 2021
1 parent 0310640 commit e60c533
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 14 deletions.
2 changes: 1 addition & 1 deletion demo/full/scripts/controllers/Main.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import RxPlayer from "rx-player";
import RxPlayer from "../../../../src/index.ts";
import React from "react";
import Player from "./Player.jsx";

Expand Down
2 changes: 1 addition & 1 deletion demo/full/scripts/modules/player/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* application.
*/

import RxPlayer from "rx-player";
import RxPlayer from "../../../../../src/index.ts";
import { linkPlayerEventsToState } from "./events.js";
import { Subject } from "rxjs";
import { takeUntil } from "rxjs/operators";
Expand Down
19 changes: 18 additions & 1 deletion package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"prepublishOnly": "npm run build:modular",
"standalone": "node ./scripts/run_standalone_demo.js",
"start": "node ./scripts/start_demo_web_server.js",
"start:fast": "node ./scripts/start_demo_web_server.js --fast",
"wasm-strip": "node scripts/wasm-strip.js dist/mpd-parser.wasm",
"test:appveyor": "npm run test:unit && npm run test:memory",
"test:integration": "node tests/integration/run.js --bchromehl --bfirefoxhl",
Expand Down Expand Up @@ -98,7 +99,8 @@
"babel-loader": "8.2.2",
"chai": "4.3.4",
"cheerio": "1.0.0-rc.10",
"core-js": "3.17.3",
"core-js": "3.17.1",
"esbuild": "0.12.25",
"eslint": "7.32.0",
"eslint-plugin-import": "2.24.2",
"eslint-plugin-jsdoc": "36.1.0",
Expand Down Expand Up @@ -139,6 +141,7 @@
"scripts-list": {
"Build a demo page (e.g. to test a code change)": {
"start": "Build the \"full\" demo (with a UI) with the non-minified RxPlayer and serve it on a local server. Re-build on file updates.",
"start:fast": "Very fast version of `start` which does not perform type-checking. This script can be useful for very quick checks of modifications on a demo page",
"demo": "Build the demo in demo/bundle.js",
"demo:min": "Build the demo and minify it in demo/bundle.js",
"demo:watch": "Build the demo in demo/bundle.js each times the files update.",
Expand Down
153 changes: 153 additions & 0 deletions scripts/fast_demo_build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/**
* # fast_demo_build.js
*
* This file allows to perform a "fast" build of the RxPlayer's demo, by using
* esbuild.
*
* You can either run it directly as a script (run `node fast_demo_build.js -h`
* to see the different options) or by requiring it as a node module.
* If doing the latter you will obtain a function you will have to run with the
* right options.
*/

const path = require("path");
const esbuild = require("esbuild");
const getHumanReadableHours = require("./utils/get_human_readable_hours");

// If true, this script is called directly
if (require.main === module) {
const { argv } = process;
if (argv.includes("-h") || argv.includes("--help")) {
displayHelp();
process.exit(0);
}
const shouldWatch = argv.includes("-w") || argv.includes("--watch");
const shouldMinify = argv.includes("-m") || argv.includes("--minify");
const production = argv.includes("-p") || argv.includes("--production-mode");
fastDemoBuild({
watch: shouldWatch,
minify: shouldMinify,
production,
});
} else {
// This script is loaded as a module
module.exports = fastDemoBuild;
}

/**
* Build the demo with the given options.
* @param {Object} options
* @param {boolean} [options.minify] - If `true`, the output will be minified.
* @param {boolean} [options.production] - If `false`, the code will be compiled
* in "development" mode, which has supplementary assertions.
* @param {boolean} [options.watch] - If `true`, the RxPlayer's files involve
* will be watched and the code re-built each time one of them changes.
*/
function fastDemoBuild(options) {
const minify = !!options.minify;
const watch = !!options.watch;
const isDevMode = !options.production;
let beforeTime = performance.now();

esbuild.build({
entryPoints: [path.join(__dirname, "../demo/full/scripts/index.jsx")],
bundle: true,
minify,
outfile: path.join(__dirname, "../demo/full/bundle.js"),
watch: !watch ? undefined : {
onRebuild(error, result) {
if (error) {
console.error(`\x1b[31m[${getHumanReadableHours()}]\x1b[0m Demo re-build failed:`,
err);
} else {
if (result.errors > 0 || result.warnings > 0) {
const { errors, warnings } = result;
console.log(`\x1b[33m[${getHumanReadableHours()}]\x1b[0m ` +
`Demo re-built with ${errors.length} error(s) and ` +
` ${warnings.length} warning(s) ` +
`(in ${stats.endTime - stats.startTime} ms).`);
}
console.log(`\x1b[32m[${getHumanReadableHours()}]\x1b[0m ` +
"Demo re-built!");
}
},
},
define: {
"__DEV__": isDevMode,
"__LOGGER_LEVEL__": "\"INFO\"",
"process.env.NODE_ENV": JSON.stringify(isDevMode ? "development" : "production"),
"__FEATURES__.BIF_PARSER": true,
"__FEATURES__.DASH": true,
"__FEATURES__.DIRECTFILE": true,
"__FEATURES__.EME": true,
"__FEATURES__.HTML_SAMI": true,
"__FEATURES__.HTML_SRT": true,
"__FEATURES__.HTML_TTML": true,
"__FEATURES__.HTML_VTT": true,
"__FEATURES__.LOCAL_MANIFEST": true,
"__FEATURES__.METAPLAYLIST": true,
"__FEATURES__.NATIVE_SAMI": true,
"__FEATURES__.NATIVE_SRT": true,
"__FEATURES__.NATIVE_TTML": true,
"__FEATURES__.NATIVE_VTT": true,
"__FEATURES__.SMOOTH": true,

// Path relative to src/features where optional features are implemented
"__RELATIVE_PATH__.BIF_PARSER": JSON.stringify("../parsers/images/bif.ts"),
"__RELATIVE_PATH__.DASH": JSON.stringify("../transports/dash/index.ts"),
"__RELATIVE_PATH__.DASH_JS_PARSER": JSON.stringify("../parsers/manifest/dash/js-parser/index.ts"),
"__RELATIVE_PATH__.DIRECTFILE": JSON.stringify("../core/init/initialize_directfile.ts"),
"__RELATIVE_PATH__.EME_MANAGER": JSON.stringify("../core/eme/index.ts"),
"__RELATIVE_PATH__.HTML_SAMI": JSON.stringify("../parsers/texttracks/sami/html.ts"),
"__RELATIVE_PATH__.HTML_SRT": JSON.stringify("../parsers/texttracks/srt/html.ts"),
"__RELATIVE_PATH__.HTML_TEXT_BUFFER": JSON.stringify("../core/segment_buffers/implementations/text/html/index.ts"),
"__RELATIVE_PATH__.HTML_TTML": JSON.stringify("../parsers/texttracks/ttml/html/index.ts"),
"__RELATIVE_PATH__.HTML_VTT": JSON.stringify("../parsers/texttracks/webvtt/html/index.ts"),
"__RELATIVE_PATH__.IMAGE_BUFFER": JSON.stringify("../core/segment_buffers/implementations/image/index.ts"),
"__RELATIVE_PATH__.LOCAL_MANIFEST": JSON.stringify("../transports/local/index.ts"),
"__RELATIVE_PATH__.MEDIA_ELEMENT_TRACK_CHOICE_MANAGER": JSON.stringify("../core/api/media_element_track_choice_manager.ts"),
"__RELATIVE_PATH__.METAPLAYLIST": JSON.stringify("../transports/metaplaylist/index.ts"),
"__RELATIVE_PATH__.NATIVE_SAMI": JSON.stringify("../parsers/texttracks/sami/native.ts"),
"__RELATIVE_PATH__.NATIVE_SRT": JSON.stringify("../parsers/texttracks/srt/native.ts"),
"__RELATIVE_PATH__.NATIVE_TEXT_BUFFER": JSON.stringify("../core/segment_buffers/implementations/text/native/index.ts"),
"__RELATIVE_PATH__.NATIVE_TTML": JSON.stringify("../parsers/texttracks/ttml/native/index.ts"),
"__RELATIVE_PATH__.NATIVE_VTT": JSON.stringify("../parsers/texttracks/webvtt/native/index.ts"),
"__RELATIVE_PATH__.SMOOTH": JSON.stringify("../transports/smooth/index.ts"),
}
}).then(
(result) => {
if (result.errors > 0 || result.warnings > 0) {
const { errors, warnings } = result;
console.log(`\x1b[33m[${getHumanReadableHours()}]\x1b[0m ` +
`Demo built with ${errors.length} error(s) and ` +
` ${warnings.length} warning(s) ` +
`(in ${stats.endTime - stats.startTime} ms).`);
}
console.log(`\x1b[32m[${getHumanReadableHours()}]\x1b[0m ` +
`Build done in ${(performance.now() - beforeTime).toFixed(2)}ms`);
},
(err) => {
console.error(`\x1b[31m[${getHumanReadableHours()}]\x1b[0m Demo build failed:`,
err);
process.exit(1);
});
}

/**
* Display through `console.log` an helping message relative to how to run this
* script.
*/
function displayHelp() {
/* eslint-disable no-console */
console.log(
/* eslint-disable indent */
`Usage: node generate_full_demo.js [options]
Options:
-h, --help Display this help
-m, --minify Minify the built demo
-p, --production-mode Build all files in production mode (less runtime checks, mostly).
-w, --watch Re-build each time either the demo or library files change`,
/* eslint-enable indent */
);
/* eslint-enable no-console */
}
7 changes: 1 addition & 6 deletions scripts/generate_full_demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Build the full demo
* ===================
*
* This script allows to build the full demo locally.
* This script allows to build the full demo locally, by using Webpack's bundler.
*
* You can either run it directly as a script (run `node
* generate_full_demo.js -h` to see the different options) or by requiring it as
Expand Down Expand Up @@ -58,11 +58,6 @@ function generateFullDemo(options) {
entry: path.join(__dirname, "../demo/full/scripts/index.jsx"),
resolve: {
extensions: [".ts", ".tsx", ".js", ".jsx", ".json"],
alias: {
["rx-player$"]: path.resolve(__dirname, "../src/index.ts"),
["rx-player/tools$"]: path.resolve(__dirname, "../src/tools/index.ts"),
["rx-player/experimental/tools$"]: path.resolve(__dirname, "../src/experimental/tools/index.ts"),
},
},
output: {
path: path.join(__dirname, "../demo/full"),
Expand Down
18 changes: 14 additions & 4 deletions scripts/start_demo_web_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@
*/

const path = require("path");
const generateFullDemo = require("./generate_full_demo");
const fastBuild = require("./fast_demo_build");
const slowBuild = require("./generate_full_demo");
const launchStaticServer = require("./launch_static_server");

generateFullDemo({ watch: true,
minify: false,
production: false });
const shouldRunFastVersion = process.argv.includes("--fast") ||
process.argv.includes("-f");

if (shouldRunFastVersion) {
fastBuild({ watch: true,
minify: false,
production: false });
} else {
slowBuild({ watch: true,
minify: false,
production: false });
}

launchStaticServer(path.join(__dirname, "../demo/full/"),
{ certificatePath: path.join(__dirname, "../localhost.crt"),
Expand Down

0 comments on commit e60c533

Please sign in to comment.