Skip to content

Commit 0fe3f2f

Browse files
authored
feat(dev): add this.fs support (#20301)
1 parent 45f6443 commit 0fe3f2f

File tree

5 files changed

+140
-106
lines changed

5 files changed

+140
-106
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"picocolors": "^1.1.1",
6666
"playwright-chromium": "^1.54.1",
6767
"prettier": "3.6.2",
68-
"rollup": "^4.40.0",
68+
"rollup": "^4.43.0",
6969
"simple-git-hooks": "^2.13.0",
7070
"tsx": "^4.20.3",
7171
"typescript": "~5.7.2",

packages/vite/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"fdir": "^6.4.6",
8686
"picomatch": "^4.0.3",
8787
"postcss": "^8.5.6",
88-
"rollup": "^4.40.0",
88+
"rollup": "^4.43.0",
8989
"tinyglobby": "^0.2.14"
9090
},
9191
"optionalDependencies": {

packages/vite/src/node/__tests__/plugins/hooks.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,18 @@ describe('supports plugin context', () => {
330330
},
331331
})
332332
})
333+
334+
test('this.fs is supported in dev', async () => {
335+
expect.hasAssertions()
336+
337+
const server = await createServerWithPlugin({
338+
name: 'test',
339+
resolveId(id) {
340+
if (id !== ENTRY_ID) return
341+
expect(this.fs.readFile).toBeTypeOf('function')
342+
},
343+
})
344+
await server.transformRequest(ENTRY_ID)
345+
await server.close()
346+
})
333347
})

packages/vite/src/node/server/pluginContainer.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ SOFTWARE.
3030
*/
3131

3232
import fs from 'node:fs'
33+
import fsp from 'node:fs/promises'
3334
import { join } from 'node:path'
3435
import { performance } from 'node:perf_hooks'
3536
import { parseAst as rollupParseAst } from 'rollup/parseAst'
@@ -50,6 +51,7 @@ import type {
5051
PluginContextMeta,
5152
ResolvedId,
5253
RollupError,
54+
RollupFsModule,
5355
RollupLog,
5456
MinimalPluginContext as RollupMinimalPluginContext,
5557
PluginContext as RollupPluginContext,
@@ -617,6 +619,22 @@ class MinimalPluginContext<T extends Environment = Environment>
617619
}
618620
}
619621

622+
const fsModule: RollupFsModule = {
623+
appendFile: fsp.appendFile,
624+
copyFile: fsp.copyFile,
625+
mkdir: fsp.mkdir as RollupFsModule['mkdir'],
626+
mkdtemp: fsp.mkdtemp,
627+
readdir: fsp.readdir,
628+
readFile: fsp.readFile as RollupFsModule['readFile'],
629+
realpath: fsp.realpath,
630+
rename: fsp.rename,
631+
rmdir: fsp.rmdir,
632+
stat: fsp.stat,
633+
lstat: fsp.lstat,
634+
unlink: fsp.unlink,
635+
writeFile: fsp.writeFile,
636+
}
637+
620638
class PluginContext
621639
extends MinimalPluginContext
622640
implements Omit<RollupPluginContext, 'cache'>
@@ -635,6 +653,8 @@ class PluginContext
635653
super(_container.minimalContext.meta, _container.environment)
636654
}
637655

656+
fs = fsModule
657+
638658
parse(code: string, opts: any) {
639659
return rollupParseAst(code, opts)
640660
}

0 commit comments

Comments
 (0)