Skip to content

fix: detect memfs used by webpack-dev-server 4+#712

Open
Br1an67 wants to merge 1 commit intowebpack:mainfrom
Br1an67:fix/issue-471-dev-server-asset-handling
Open

fix: detect memfs used by webpack-dev-server 4+#712
Br1an67 wants to merge 1 commit intowebpack:mainfrom
Br1an67:fix/issue-471-dev-server-asset-handling

Conversation

@Br1an67
Copy link

@Br1an67 Br1an67 commented Mar 9, 2026

Fixes #471

Summary

When using webpack-dev-server 4+, the bundle analyzer was throwing "no such file" errors because it tried to read bundle files from disk, but webpack-dev-server 4+ uses memfs to store files in memory instead of writing them to disk.

This fix detects when memfs is being used by checking for the __vol property (which is specific to memfs's filesystem objects) and returns null for the bundle directory. This tells the analyzer to skip reading bundle files from disk and instead rely on the stats data.

Changes

  • Modified getBundleDirFromCompiler() in src/BundleAnalyzerPlugin.js to detect memfs by checking for the __vol property
  • When memfs is detected, returns null for bundleDir to prevent disk read attempts

Testing

  • The existing test/dev-server.js test verifies that the report file is saved correctly when using webpack-dev-server
  • Manual testing confirms that webpack-dev-server 4+ uses memfs with the __vol property

Diff Stats

 src/BundleAnalyzerPlugin.js | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Mar 9, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: Br1an67 / name: Br1an (8360d05)

@valscion
Copy link
Member

valscion commented Mar 9, 2026

Please sign the CLA. Using __vol sounds brittle and likely something that is a private API and no guarantees for backwards compatibility. @alexander-akait any insight from your part on this topic?

@alexander-akait
Copy link
Member

@valscion we should just use compiler.outputFileSystem here to get content, I think it is AI generated code, they even don't use PR template

@Br1an67 Br1an67 force-pushed the fix/issue-471-dev-server-asset-handling branch from 9671d6e to ec825e0 Compare March 10, 2026 12:55
@Br1an67
Copy link
Author

Br1an67 commented Mar 10, 2026

Thanks for the review! I’ve updated the implementation to avoid the __vol check. The analyzer now reads bundle assets via compiler.outputFileSystem when it isn’t the native fs, so it works with memfs/MemoryFileSystem without relying on private fields. I also rewrote the commit authors to my GitHub noreply email so EasyCLA should pass now. Please re-run EasyCLA when convenient.

@valscion
Copy link
Member

Yeah I'm not against AI generated code but the repository PR template does need to be followed and code changes should be minimal and understood well.


if (!outputFs || outputFs === fs) {
return null;
}
Copy link
Member

@alexander-akait alexander-akait Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is useless, remove it


if (typeof outputFs.readFileSync !== "function") {
return null;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is useless, remove it

src/analyzer.js Outdated
* @property {Logger} logger logger
* @property {CompressionAlgorithm} compressionAlgorithm compression algorithm
* @property {ExcludeAssets} excludeAssets exclude assets
* @property {{ readFileSync: (path: string, encoding: string) => string } | null} bundleDirFs filesystem for reading bundle files
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use type from webpack

Instead of detecting memfs by constructor name, use the compiler's
outputFileSystem directly to read bundle files when it supports
readFileSync (e.g. memfs from webpack-dev-server 4+). Falls back
to native fs for webpack 4's NodeOutputFileSystem which lacks
readFileSync.

Fixes webpack#471

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Br1an67 Br1an67 force-pushed the fix/issue-471-dev-server-asset-handling branch from ec825e0 to 8360d05 Compare March 18, 2026 00:22
@valscion
Copy link
Member

@Br1an67 I need to know the steps you've taken to verify this code works. Reply with the details, don't change code. Be explicit, list the steps out in numbered order. Assume the steps follow red-green TDD passes so that we can first verify the original issue existed. Then we apply your change. Then we verify the original issue has been fixed.

Ideally these steps would be encoded as a unit test directly in this repository. But it might be difficult so we do the reproduction steps first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Errors when using with the webpack dev server 4

3 participants