From 555006b9c380439ec928144a1cc769e8f9d88256 Mon Sep 17 00:00:00 2001 From: Vanilagy <1696106+Vanilagy@users.noreply.github.com> Date: Tue, 16 Sep 2025 21:13:32 +0200 Subject: [PATCH] Add improved logic for loading Node.js module (fixes #118) --- package.json | 4 ++++ scripts/add-import-extensions.ts | 2 +- scripts/bundle.ts | 3 +++ src/node.ts | 11 +++++++++++ src/source.ts | 10 ++++++---- 5 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 src/node.ts diff --git a/package.json b/package.json index ce2e3fe..a1f1445 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,10 @@ "dist", "src" ], + "browser": { + "./dist/modules/src/node.js": false, + "./src/node.ts": false + }, "sideEffects": false, "scripts": { "build": "./build.sh", diff --git a/scripts/add-import-extensions.ts b/scripts/add-import-extensions.ts index ee07742..d5da4b1 100644 --- a/scripts/add-import-extensions.ts +++ b/scripts/add-import-extensions.ts @@ -24,7 +24,7 @@ const walkDir = (dir: string) => { const fixFile = (filePath: string) => { const content = fs.readFileSync(filePath, 'utf8'); const fixed = content.replace( - /(\s+from\s+['"])([^'"]*)(['"])/g, + /(\s+from\s+['"])(\.[^'"]*)(['"])/g, // This only matches relative imports '$1$2.js$3', ); diff --git a/scripts/bundle.ts b/scripts/bundle.ts index 02de9ac..fbe7a91 100644 --- a/scripts/bundle.ts +++ b/scripts/bundle.ts @@ -16,6 +16,9 @@ const createVariants = async ( entryPoints: [entryPoint], bundle: true, logLevel: 'info', + logOverride: { + 'import-is-undefined': 'silent', // Warning caused by the disabled "node.ts" import + }, banner: { js: `/*! * Copyright (c) 2025-present, Vanilagy and contributors diff --git a/src/node.ts b/src/node.ts new file mode 100644 index 0000000..81f2a0d --- /dev/null +++ b/src/node.ts @@ -0,0 +1,11 @@ +/*! + * Copyright (c) 2025-present, Vanilagy and contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +// This file contains Node.js-specific code that does not run in a browser. + +export * as fs from 'node:fs/promises'; diff --git a/src/source.ts b/src/source.ts index 54aedae..6e548ce 100644 --- a/src/source.ts +++ b/src/source.ts @@ -18,6 +18,9 @@ import { toDataView, toUint8Array, } from './misc'; +import * as nodeAlias from './node'; + +const node = nodeAlias; // Aliasing it prevents some bundler warnings export type ReadResult = { bytes: Uint8Array; @@ -548,9 +551,7 @@ export class FilePathSource extends Source { // Let's back this source with a StreamSource, makes the implementation very simple this._streamSource = new StreamSource({ getSize: async () => { - const FS_MODULE_NAME = 'node:fs/promises'; - const fs = await import(/* @vite-ignore */ FS_MODULE_NAME) as typeof import('node:fs/promises'); - fileHandle = await fs.open(filePath, 'r'); + fileHandle = await node.fs.open(filePath, 'r'); const stats = await fileHandle.stat(); return stats.size; @@ -558,8 +559,9 @@ export class FilePathSource extends Source { read: async (start, end) => { assert(fileHandle); - const buffer = Buffer.alloc(end - start); + const buffer = new Uint8Array(end - start); await fileHandle.read(buffer, 0, end - start, start); + return buffer; }, maxCacheSize: options.maxCacheSize,