Add special .node.cjs variant for CJS support

This commit is contained in:
Vanilagy
2026-04-28 15:33:51 +02:00
parent a1aaa8e36a
commit e1dd95086a
5 changed files with 24 additions and 10 deletions
+2 -2
View File
@@ -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",
+17 -2
View File
@@ -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(
+1 -2
View File
@@ -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';
+2 -2
View File
@@ -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;
+2 -2
View File
@@ -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<StreamTargetChunk>({
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);