Fix FilePathSource/FilePathTarget not working in server-side environments using CJS (closes #360)

This commit is contained in:
Vanilagy
2026-04-27 18:29:52 +02:00
parent 15beb69137
commit c89e7429f7
4 changed files with 12 additions and 4 deletions
+1
View File
@@ -30,6 +30,7 @@ const createVariants = async (
*/`, */`,
}, },
legalComments: 'none', legalComments: 'none',
platform: 'node',
}; };
const umdConfig: esbuild.BuildOptions = { const umdConfig: esbuild.BuildOptions = {
+2 -1
View File
@@ -8,4 +8,5 @@
// This file contains Node.js-specific code that does not run in a browser. // This file contains Node.js-specific code that does not run in a browser.
export * as fs from 'node:fs/promises'; // Dynamic import so it can be included in the bundles and still work properly in the browser
export const getFs = () => import('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.'); throw new TypeError('options.maxCacheSize, when provided, must be a non-negative number.');
} }
if (!node.fs) { if (!node.getFs) {
throw new Error( throw new Error(
'FilePathSource is only available in server-side environments (Node.js, Bun, Deno).', '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 // Let's back this source with a StreamSource, makes the implementation very simple
this._streamSource = new StreamSource({ this._streamSource = new StreamSource({
getSize: async () => { getSize: async () => {
this._fileHandle = await node.fs.open(filePath, 'r'); this._fileHandle = await (await node.getFs()).open(filePath, 'r');
const stats = await this._fileHandle.stat(); const stats = await this._fileHandle.stat();
return stats.size; return stats.size;
+7 -1
View File
@@ -692,12 +692,18 @@ export class FilePathTarget extends Target {
throw new TypeError('options must be an object.'); throw new TypeError('options must be an object.');
} }
if (!node.getFs) {
throw new Error(
'FilePathTarget is only available in server-side environments (Node.js, Bun, Deno).',
);
}
super(); super();
// Let's back this target with a StreamTarget, makes the implementation very simple // Let's back this target with a StreamTarget, makes the implementation very simple
const writable = new WritableStream<StreamTargetChunk>({ const writable = new WritableStream<StreamTargetChunk>({
start: async () => { start: async () => {
this._fileHandle = await node.fs.open(filePath, 'w'); this._fileHandle = await (await node.getFs()).open(filePath, 'w');
}, },
write: async (chunk) => { write: async (chunk) => {
assert(this._fileHandle); assert(this._fileHandle);