Make InputFile edge-compatible with conditional fs imports#1297
Make InputFile edge-compatible with conditional fs imports#1297Copilot wants to merge 7 commits intofix-node-syncfrom
Conversation
|
Important Review skippedBot user detected. To trigger a single review, invoke the You can disable this status message by setting the Comment |
Co-authored-by: Meldiron <19310830+Meldiron@users.noreply.github.com>
Co-authored-by: Meldiron <19310830+Meldiron@users.noreply.github.com>
Co-authored-by: Meldiron <19310830+Meldiron@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
templates/node/src/inputFile.ts.twig
Outdated
| // Only import fs modules if not in edge runtime | ||
| if (!isEdgeRuntime) { | ||
| try { | ||
| basename = require("path").basename; | ||
| realpathSync = require("fs").realpathSync; | ||
| fs = require("fs").promises; | ||
| } catch (e) { | ||
| // If imports fail, fs will remain undefined | ||
| } | ||
| } |
There was a problem hiding this comment.
@copilot Do not use require, its not efficient. Lets switch to using import here
There was a problem hiding this comment.
Replaced require() with dynamic import() statements using top-level await. The modules are now loaded in parallel with Promise.all() for better efficiency. Changes in commit 2732380.
templates/node/src/inputFile.ts.twig
Outdated
| // Conditionally import fs modules to support edge runtimes | ||
| let basename: ((path: string) => string) | undefined; | ||
| let realpathSync: ((path: string) => string) | undefined; | ||
| let fs: typeof import('fs').promises | undefined; |
There was a problem hiding this comment.
@copilot I am a bit worried about type here. This type would fail on edge runtime, right? Because edge doesnt know fs. Do we need to change this, or is it okay thanks to build step running on non-edge machine?
There was a problem hiding this comment.
Good catch! Replaced the typeof import('fs').promises type with an explicit interface that defines only the methods we use. This avoids any potential edge runtime type resolution issues while maintaining type safety. The explicit interface approach is safer since it doesn't reference Node.js types directly. Changes in commit 2732380.
Co-authored-by: Meldiron <19310830+Meldiron@users.noreply.github.com>
Co-authored-by: Meldiron <19310830+Meldiron@users.noreply.github.com>
Co-authored-by: Meldiron <19310830+Meldiron@users.noreply.github.com>
The
fsmodule imports ininputFile.tscause the entire InputFile class to fail in edge runtimes (Cloudflare Workers, Vercel Edge), even for methods likefromBuffer()that don't require filesystem access.Changes
importstatements with dynamicimport()calls guarded by edge runtime detectionPromise.all()to loadpathandfsmodules in parallel for better efficiencyfromPath(),size(),slice(), andtoBuffer()to throw descriptive errors when fs operations are attempted in edge environmentstypeof (globalThis as any).EdgeRuntime !== 'undefined'pattern matchingclient.ts.twigtypeof import('fs').promisesto avoid edge runtime type resolution issuesResult
Buffer-based methods work in edge runtimes:
No breaking changes for Node.js environments where all methods continue to work. Dynamic imports load asynchronously at module initialization, with undefined checks providing clear errors if modules aren't available.
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.