diff --git a/scripts/bundle.ts b/scripts/bundle.ts index 7dcc1ae..e5f9063 100644 --- a/scripts/bundle.ts +++ b/scripts/bundle.ts @@ -30,6 +30,7 @@ const createVariants = async ( */`, }, legalComments: 'none', + platform: 'node', }; const umdConfig: esbuild.BuildOptions = { diff --git a/src/node.ts b/src/node.ts index 3ed1aa3..4718ebc 100644 --- a/src/node.ts +++ b/src/node.ts @@ -8,4 +8,5 @@ // 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'); diff --git a/src/source.ts b/src/source.ts index 7e2d298..553c8e6 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.fs) { + if (!node.getFs) { 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 node.fs.open(filePath, 'r'); + this._fileHandle = await (await node.getFs()).open(filePath, 'r'); const stats = await this._fileHandle.stat(); return stats.size; diff --git a/src/target.ts b/src/target.ts index 20a6aeb..ec44613 100644 --- a/src/target.ts +++ b/src/target.ts @@ -692,12 +692,18 @@ export class FilePathTarget extends Target { 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(); // Let's back this target with a StreamTarget, makes the implementation very simple const writable = new WritableStream({ start: async () => { - this._fileHandle = await node.fs.open(filePath, 'w'); + this._fileHandle = await (await node.getFs()).open(filePath, 'w'); }, write: async (chunk) => { assert(this._fileHandle);