Skip to content

Commit

Permalink
Proposal: Stop relying on webpack for legacy bundles generation
Browse files Browse the repository at this point in the history
As a follow-up effort to #1420 (on which this work is based on), I
attempt here to stop relying on webpack for the generation of our
"legacy bundles", which are rarely used RxPlayer bundles that are
communicated through release notes and which exposes the
`RxPlayer` class through the global scope (`window`).

The idea, the same than for #1420, was to simplify our bundling process
by relying only on esbuild for this (instead of both webpack and esbuild
depending on the situation). I chose esbuild over webpack here mainly
because I better understand its role, behavior and most-of-all its API
than webpack's - though its (much) faster bundling speed is a also a
very good argument for this.
Like for #1420, the last remaining issue was our usage of the babeljs
transpiler to transpile to ES5 (our legacy bundles are ES5), but that's
been fixed by switching to swc for that part.

Our legacy bundle was purely configured by the `webpack.config.js` file
at the root of our project that I here removed.
To replace it, I re-purposed our `scripts/bundle_worker.mjs` script into
a more generic `scripts/run_bundler.mjs` script for which we're now
supposed to indicate the wanted input and output files.

--

Still, I encountered an important issue while doing that in that I did
not understand how we're supposed to indicate to esbuild that our entry
file's export has to be exported through the global scope (all my
introduction about the esbuild API being simpler to understand now loses
all credibility!). I tried playing with its
[`globalName`](https://esbuild.github.io/api/#global-name) and
[`format`](https://esbuild.github.io/api/#format) configuration options
which seem linked to that but couldn't make it work.

For now, I gave up, adding a `__GLOBAL_SCOPE__` compile-time constant
(boolean) to `src/index.ts` which has to be set to `true` before
bundling if you want to export the RxPlayer through the global scope.
  • Loading branch information
peaBerberian committed Jun 17, 2024
1 parent 061588a commit bb0e7f5
Show file tree
Hide file tree
Showing 11 changed files with 351 additions and 347 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@
"build:dev": "./scripts/generate_build.mjs --dev-mode",
"build:wasm:debug": "mkdir -p dist && cd ./src/parsers/manifest/dash/wasm-parser && cargo build --target wasm32-unknown-unknown && cp target/wasm32-unknown-unknown/debug/mpd_node_parser.wasm ../../../../../dist/mpd-parser.wasm",
"build:wasm:release": "./scripts/build_wasm_release.sh",
"bundle": "webpack --progress --config webpack.config.mjs --env production",
"bundle:min": "webpack --progress --config webpack.config.mjs --env minify --env production",
"bundle:min:watch": "webpack --progress --config webpack.config.mjs -w --env production --env minify",
"bundle:watch": "webpack --progress --config webpack.config.mjs -w --env production",
"bundle": "./scripts/run_bundler.mjs src/index.ts --production-mode --es5 dist/rx-player.js",
"bundle:min": "./scripts/run_bundler.mjs src/index.ts --production-mode --es5 dist/rx-player.js --minify",
"bundle:min:watch": "./scripts/run_bundler.mjs src/index.ts --production-mode --es5 dist/rx-player.js --minify --watch",
"bundle:watch": "./scripts/run_bundler.mjs src/index.ts --production-mode --es5 dist/rx-player.js -- watch",
"certificate": "./scripts/generate_certificate",
"check": "npm run check:types && npm run lint && npm run check:types:unit_tests",
"check:all": "npm run check:types && npm run lint && npm run lint:demo && npm run lint:tests && npm run test:unit && npm run test:integration && npm run test:memory && node -r esm ./scripts/check_nodejs_import_compatibility.js",
Expand Down
6 changes: 4 additions & 2 deletions scripts/build_demo.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import { pathToFileURL } from "url";
import esbuild from "esbuild";
import rootDirectory from "./utils/project_root_directory.mjs";
import getHumanReadableHours from "./utils/get_human_readable_hours.mjs";
import buildWorker from "./bundle_worker.mjs";
import runBundler from "./run_bundler.mjs";

const WORKER_IN_FILE = join(rootDirectory, "src/worker_entry_point.ts");
const DEMO_OUT_FILE = join(rootDirectory, "demo/full/bundle.js");
const WORKER_OUT_FILE = join(rootDirectory, "demo/full/worker.js");
const WASM_FILE_DEPENDENCY = join(rootDirectory, "dist/mpd-parser.wasm");
Expand Down Expand Up @@ -73,7 +74,7 @@ export default function buildDemo(options) {
}
});

buildWorker({
runBundler(WORKER_IN_FILE, {
watch,
minify,
production: !isDevMode,
Expand Down Expand Up @@ -128,6 +129,7 @@ export default function buildDemo(options) {
__LOGGER_LEVEL__: JSON.stringify({
CURRENT_LEVEL: "INFO",
}),
__GLOBAL_SCOPE__: JSON.stringify(true),
},
})
.then((context) => {
Expand Down
247 changes: 0 additions & 247 deletions scripts/bundle_worker.mjs

This file was deleted.

11 changes: 8 additions & 3 deletions scripts/generate_build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import * as path from "path";
import { fileURLToPath, pathToFileURL } from "url";
import { rimraf } from "rimraf";
import generateEmbeds from "./generate_embeds.mjs";
import buildWorker from "./bundle_worker.mjs";
import runBundler from "./run_bundler.mjs";

const currentDirectory = path.dirname(fileURLToPath(import.meta.url));

Expand All @@ -35,6 +35,10 @@ const BUILD_ARTEFACTS_TO_REMOVE = [
"src/__GENERATED_CODE",
];

const WORKER_IN_FILE = path.join(ROOT_DIR, "src/worker_entry_point.ts");
const WORKER_OUT_FILE = path.join(ROOT_DIR, "dist/worker.js");
const ES5_WORKER_OUT_FILE = path.join(ROOT_DIR, "dist/worker.es5.js");

// If true, this script is called directly
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
const { argv } = process;
Expand Down Expand Up @@ -79,12 +83,13 @@ async function generateBuild(options = {}) {

console.log(" 👷 Bundling worker files...");
await Promise.all([
buildWorker({
runBundler(WORKER_IN_FILE, {
watch: false,
minify: !devMode,
outfile: WORKER_OUT_FILE,
es5Outfile: ES5_WORKER_OUT_FILE,
production: !devMode,
silent: true,
es5: true,
}),
]);

Expand Down
Loading

0 comments on commit bb0e7f5

Please sign in to comment.