From e1dd95086a71f391b3d6ca2eabb71197716fb757 Mon Sep 17 00:00:00 2001 From: Vanilagy <1696106+Vanilagy@users.noreply.github.com> Date: Tue, 28 Apr 2026 15:33:51 +0200 Subject: [PATCH] Add special .node.cjs variant for CJS support --- package.json | 4 ++-- scripts/bundle.ts | 19 +++++++++++++++++-- src/node.ts | 3 +-- src/source.ts | 4 ++-- src/target.ts | 4 ++-- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index b601c5c..30ee8a5 100644 --- a/package.json +++ b/package.json @@ -7,13 +7,13 @@ "workspaces": [ "packages/*" ], - "main": "./dist/modules/src/index.js", + "main": "./dist/bundles/mediabunny.node.cjs", "module": "./dist/modules/src/index.js", "types": "./dist/modules/src/index.d.ts", "exports": { "types": "./dist/modules/src/index.d.ts", "import": "./dist/modules/src/index.js", - "require": "./dist/modules/src/index.js" + "require": "./dist/bundles/mediabunny.node.cjs" }, "files": [ "README.md", diff --git a/scripts/bundle.ts b/scripts/bundle.ts index e5f9063..6a22de0 100644 --- a/scripts/bundle.ts +++ b/scripts/bundle.ts @@ -11,6 +11,7 @@ const createVariants = async ( umdExtension: string, specificUmdConfig: esbuild.BuildOptions = {}, specificEsmConfig: esbuild.BuildOptions = {}, + nodeUmdVariant = false, ) => { const baseConfig: esbuild.BuildOptions = { entryPoints: [entryPoint], @@ -30,7 +31,6 @@ const createVariants = async ( */`, }, legalComments: 'none', - platform: 'node', }; const umdConfig: esbuild.BuildOptions = { @@ -74,7 +74,19 @@ const createVariants = async ( minify: true, }); - return [umdVariant, esmVariant, umdMinifiedVariant, esmMinifiedVariant]; + const variants = [umdVariant, esmVariant, umdMinifiedVariant, esmMinifiedVariant]; + + if (nodeUmdVariant) { + const nodeVariant = await esbuild.context({ + ...umdConfig, + ...specificUmdConfig, + outfile: `${outfileBase}.node.${umdExtension}`, + platform: 'node', // This is different + }); + variants.push(nodeVariant); + } + + return variants; }; const mediabunnyVariants = await createVariants( @@ -82,6 +94,9 @@ const mediabunnyVariants = await createVariants( 'Mediabunny', 'dist/bundles/mediabunny', 'cjs', + undefined, + undefined, + true, ); const mp3EncoderVariants = await createVariants( diff --git a/src/node.ts b/src/node.ts index 4718ebc..3ed1aa3 100644 --- a/src/node.ts +++ b/src/node.ts @@ -8,5 +8,4 @@ // This file contains Node.js-specific code that does not run in a browser. -// Dynamic import so it can be included in the bundles and still work properly in the browser -export const getFs = () => import('node:fs/promises'); +export * as fs from 'node:fs/promises'; diff --git a/src/source.ts b/src/source.ts index 59cb8db..ec8336a 100644 --- a/src/source.ts +++ b/src/source.ts @@ -959,7 +959,7 @@ export class FilePathSource extends PathedSource { throw new TypeError('options.maxCacheSize, when provided, must be a non-negative number.'); } - if (!node.getFs) { + if (!node.fs) { throw new Error( 'FilePathSource is only available in server-side environments (Node.js, Bun, Deno).', ); @@ -970,7 +970,7 @@ export class FilePathSource extends PathedSource { // Let's back this source with a StreamSource, makes the implementation very simple this._streamSource = new StreamSource({ getSize: async () => { - this._fileHandle = await (await node.getFs()).open(filePath, 'r'); + this._fileHandle = await node.fs.open(filePath, 'r'); const stats = await this._fileHandle.stat(); return stats.size; diff --git a/src/target.ts b/src/target.ts index ec44613..df961a9 100644 --- a/src/target.ts +++ b/src/target.ts @@ -692,7 +692,7 @@ export class FilePathTarget extends Target { throw new TypeError('options must be an object.'); } - if (!node.getFs) { + if (!node.fs) { throw new Error( 'FilePathTarget is only available in server-side environments (Node.js, Bun, Deno).', ); @@ -703,7 +703,7 @@ export class FilePathTarget extends Target { // Let's back this target with a StreamTarget, makes the implementation very simple const writable = new WritableStream({ start: async () => { - this._fileHandle = await (await node.getFs()).open(filePath, 'w'); + this._fileHandle = await node.fs.open(filePath, 'w'); }, write: async (chunk) => { assert(this._fileHandle);