Skip to content

Commit

Permalink
Fix light ESM build (#6307)
Browse files Browse the repository at this point in the history
ESM build needs a special empty file that exports an object to prevent
property access of "undefined"

(cherry picked from commit 474a10b)
  • Loading branch information
Chocobozzz authored and robwalch committed Jun 1, 2024
1 parent e1c2904 commit b3d1746
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
34 changes: 18 additions & 16 deletions build-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,59 +202,61 @@ const basePlugins = [
commonjs({ transformMixedEsModules: true }),
];

function getAliasesForLightDist() {
function getAliasesForLightDist(format) {
const emptyFile = format === 'esm' ? 'empty-es.js' : 'empty.js';

let aliases = {};

if (!addEMESupport) {
aliases = {
...aliases,
'./controller/eme-controller': './empty.js',
'./utils/mediakeys-helper': './empty.js',
'../utils/mediakeys-helper': '../empty.js',
'./controller/eme-controller': `./${emptyFile}`,
'./utils/mediakeys-helper': `./${emptyFile}`,
'../utils/mediakeys-helper': `../${emptyFile}`,
};
}

if (!addCMCDSupport) {
aliases = { ...aliases, './controller/cmcd-controller': './empty.js' };
aliases = { ...aliases, './controller/cmcd-controller': `./${emptyFile}` };
}

if (!addSubtitleSupport) {
aliases = {
...aliases,
'./utils/cues': './empty.js',
'./controller/timeline-controller': './empty.js',
'./controller/subtitle-track-controller': './empty.js',
'./controller/subtitle-stream-controller': './empty.js',
'./utils/cues': `./${emptyFile}`,
'./controller/timeline-controller': `./${emptyFile}`,
'./controller/subtitle-track-controller': `./${emptyFile}`,
'./controller/subtitle-stream-controller': `./${emptyFile}`,
};
}

if (!addAltAudioSupport) {
aliases = {
...aliases,
'./controller/audio-track-controller': './empty.js',
'./controller/audio-stream-controller': './empty.js',
'./controller/audio-track-controller': `./${emptyFile}`,
'./controller/audio-stream-controller': `./${emptyFile}`,
};
}

if (!addVariableSubstitutionSupport) {
aliases = {
...aliases,
'./utils/variable-substitution': './empty.js',
'../utils/variable-substitution': '../empty.js',
'./utils/variable-substitution': `./${emptyFile}`,
'../utils/variable-substitution': `../${emptyFile}`,
};
}

if (!addM2TSAdvancedCodecSupport) {
aliases = {
...aliases,
'./ac3-demuxer': '../empty.js',
'./ac3-demuxer': `../${emptyFile}`,
};
}

if (!addMediaCapabilitiesSupport) {
aliases = {
...aliases,
'../utils/mediacapabilities-helper': '../empty.js',
'../utils/mediacapabilities-helper': `../${emptyFile}`,
};
}

Expand Down Expand Up @@ -299,7 +301,7 @@ const buildRollupConfig = ({
? [alias({ entries: { './transmuxer-worker': '../empty.js' } })]
: []),
...(type === BUILD_TYPE.light
? [alias({ entries: getAliasesForLightDist() })]
? [alias({ entries: getAliasesForLightDist(format) })]
: []),
...(format === 'esm'
? [buildBabelEsm({ stripConsole: true })]
Expand Down
5 changes: 5 additions & 0 deletions src/empty-es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// This file is inserted as a shim for modules which we do not want to include into the distro.
// This replacement is done in the "alias" plugin of the rollup config.
// Use a ES dedicated file as Rollup assigns an object in the output
// For example: "var KeySystemFormats = emptyEs.KeySystemFormats;"
module.exports = {};

0 comments on commit b3d1746

Please sign in to comment.