Skip to content

Commit

Permalink
fix(player): mark side effect chunks created by esbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
mihar-22 committed Aug 6, 2024
1 parent 472c030 commit fd271de
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion packages/vidstack/.scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function getNPMBundles(): BuildOptions[] {
{
...getNPMConfig({ dev: false }),
entryPoints: getBrowserInputs(),
plugins,
plugins: [...plugins, sideEffects()],
},
// server
{
Expand Down Expand Up @@ -324,6 +324,50 @@ function copyAssets(): Plugin {
};
}

function sideEffects(): Plugin {
return {
name: 'side-effects',
async setup(build) {
build.onEnd(async () => {
const { outdir } = build.initialOptions;
if (!outdir) return;

const dir = outdir.replace(/^.*?\//, ''),
files = await fs.readdir(`${outdir}/define`),
sideEffects = new Set<string>();

await Promise.all(
files.map(async (file) => {
const filePath = `${outdir}/define/${file}`,
isDirectory = (await fs.stat(filePath)).isDirectory();

if (isDirectory) return;

const code = await fs.readFile(filePath, 'utf-8');

// If there is a side effect chunk created by esbuild, it'll generally be the first import.
const end = code.indexOf(';'),
chunk = code.slice('import"../chunks/'.length, end - 1);

if (chunk.startsWith('vidstack') && chunk.endsWith('.js')) {
sideEffects.add(`./${dir}/chunks/${chunk}`);
}
}),
);

const pkgPath = 'dist-npm/package.json',
pkg = JSON.parse(await fs.readFile(pkgPath, 'utf-8'));

for (const chunk of sideEffects) {
pkg.sideEffects.push(chunk);
}

await fs.writeFile(pkgPath, JSON.stringify(pkg, null, 2), 'utf-8');
});
},
};
}

// This plugin rewrites chunk paths so our URL rewrites to jsDelivr work.
function rewriteCDNChunks(): Plugin {
return {
Expand Down

0 comments on commit fd271de

Please sign in to comment.