Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"devDependencies": {
"@jridgewell/remapping": "catalog:",
"@jridgewell/trace-mapping": "catalog:",
"@jsonjoy.com/fs-core": "catalog:",
"@rstackjs/load-config": "catalog:",
"@types/cors": "catalog:",
"@types/node": "catalog:",
Expand Down
47 changes: 47 additions & 0 deletions packages/core/src/server/assets-middleware/memoryFileSystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Node, Superblock } from '@jsonjoy.com/fs-core';
import { createFsFromVolume, type IFs, Volume } from 'memfs';

/**
* Rspack usually writes each asset as one complete Buffer. Rebuilds overwrite
* files with writeFile, which truncates them and releases their old buffers,
* so spare capacity from the previous build cannot be reused.
*
* memfs's power-of-two growth helps incremental appends, but wastes memory for
* these whole-file writes (e.g. a 5 MiB asset reserves 8 MiB). Copying into an
* exact-sized buffer avoids that unused capacity on both initial builds and
* rebuilds. Other writes retain memfs's normal growth behavior.
*/
function write(
this: Node,
buffer: Buffer,
offset = 0,
length = buffer.length,
position = 0,
): number {
if (
this.getSize() === 0 &&
position === 0 &&
offset === 0 &&
length === buffer.length &&
length > 0
) {
// Copy the input and keep the inode's metadata/events so links, descriptors
// and watchers agree.
this.setBuffer(buffer);
return length;
}

return Node.prototype.write.call(this, buffer, offset, length, position);
}

class OutputSuperblock extends Superblock {
override createNode(mode: number): Node {
const node = super.createNode(mode);
node.write = write;
return node;
}
}

export function createMemoryFileSystem(): IFs {
return createFsFromVolume(new Volume(new OutputSuperblock()));
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ export async function setupOutputFileSystem(
compilers: Compiler[],
): Promise<OutputFileSystem> {
if (writeToDisk !== true) {
const { createFsFromVolume, Volume } = await import(
/* rspackChunkName: "memfs" */ 'memfs'
const { createMemoryFileSystem } = await import(
/* rspackChunkName: "memfs" */ './memoryFileSystem'
);
const outputFileSystem = createFsFromVolume(
new Volume(),
) as OutputFileSystem;
const outputFileSystem = createMemoryFileSystem() as OutputFileSystem;

for (const compiler of compilers) {
compiler.outputFileSystem = outputFileSystem;
Expand Down
28 changes: 28 additions & 0 deletions packages/core/tests/memoryFileSystem.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { IFsWithVolume } from 'memfs';
import { createMemoryFileSystem } from '../src/server/assets-middleware/memoryFileSystem';

test('should copy whole-file writes into exact-sized buffers, including overwrites', () => {
const fs = createMemoryFileSystem() as IFsWithVolume;
// Stay above the Buffer pool threshold when checking the backing allocation.
for (const size of [Buffer.poolSize * 2 + 1, Buffer.poolSize + 1]) {
const content = Buffer.alloc(size, 'x');
fs.writeFileSync('/asset.js', content);
content.fill(0);

const stat = fs.statSync('/asset.js');
const node = fs.__vol._core.inodes[Number(stat.ino)];
expect(stat.size).toBe(size);
expect(node.buf.buffer.byteLength).toBe(size);
expect(fs.readFileSync('/asset.js')).toEqual(Buffer.alloc(size, 'x'));
}
});

test('should preserve partial writes, existing contents and appends', () => {
const fs = createMemoryFileSystem();
const fd = fs.openSync('/asset.js', 'w+');
fs.writeSync(fd, Buffer.from('abcd'), 1, 2, 2);
fs.writeSync(fd, Buffer.from('X'), 0, 1, 0);
fs.closeSync(fd);
fs.appendFileSync('/asset.js', '!');
expect(fs.readFileSync('/asset.js', 'utf8')).toBe('X\0bc!');
});
8 changes: 7 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ catalog:
'@hono/node-server': '^2.1.1'
'@jridgewell/remapping': '^2.3.5'
'@jridgewell/trace-mapping': '^0.3.31'
'@jsonjoy.com/fs-core': '4.78.0'
'@module-federation/enhanced': '2.9.0'
'@module-federation/rsbuild-plugin': '2.9.0'
'@module-federation/runtime-tools': '2.9.0'
Expand Down Expand Up @@ -94,7 +95,8 @@ catalog:
'less-loader': '^12.3.3'
'lit': '^3.3.3'
'loader-utils': '^3.3.1'
'memfs': '^4.78.0'
# Keep memfs and its storage extension on matching versions.
'memfs': '4.78.0'
'mrmime': '^2.0.1'
'on-finished': '2.4.1'
'open': '^11.0.3'
Expand Down
1 change: 1 addition & 0 deletions scripts/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ jiti
jridgewell
jscpuprofile
jsesc
jsonjoy
kaios
koppers
lightningcss
Expand Down