Skip to content

Commit 1013935

Browse files
SnaveSutitgitbutler-client
authored andcommitted
🐛 Fix libraries requesting node:fs early in the plugin loading process
1 parent 7d8dcc5 commit 1013935

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

.scripts/esbuild.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ const COMMON_CONFIG: esbuild.BuildOptions = {
233233
svelte: 'svelte',
234234
module: './.scripts/fakeModule.js',
235235
'node:module': './.scripts/fakeModule.js',
236+
'node:fs': './src/constants.ts',
236237
},
237238
format: 'iife',
238239
define: DEFINES,

src/constants.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,42 @@
11
import PACKAGEJSON from '../package.json'
2-
import { localize as translate } from './util/lang'
2+
import { localize } from './util/lang'
33
export const PACKAGE: typeof PACKAGEJSON = PACKAGEJSON
44

55
let cachedFsModule: ScopedFS | null = null
66
export function getFsModule() {
7+
debugger
78
cachedFsModule ??= requireNativeModule('fs', {
8-
message: translate('require.fs'),
9+
message: localize('require.fs'),
910
optional: false,
1011
})!
1112
return cachedFsModule
1213
}
14+
15+
// Super lazy-loading of properties, so that we don't load the fs or fs.promises module until it's actually used by a dependency.
16+
export default new Proxy(
17+
{},
18+
{
19+
get(target, prop) {
20+
if (prop === 'promises') {
21+
return new Proxy(
22+
{},
23+
{
24+
get(target, prop) {
25+
return (...args: any[]) => {
26+
// @ts-expect-error
27+
return getFsModule().promises[prop as keyof ScopedFS['promises']](
28+
...args
29+
)
30+
}
31+
},
32+
}
33+
)
34+
}
35+
36+
return (...args: any[]) => {
37+
// @ts-expect-error
38+
return getFsModule()[prop as keyof ScopedFS](...args)
39+
}
40+
},
41+
}
42+
)

0 commit comments

Comments
 (0)