-
-
Notifications
You must be signed in to change notification settings - Fork 376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use memfs instead of memory-fs #366
Conversation
Codecov Report
@@ Coverage Diff @@
## master #366 +/- ##
==========================================
+ Coverage 96.85% 96.87% +0.02%
==========================================
Files 7 7
Lines 254 256 +2
==========================================
+ Hits 246 248 +2
Misses 8 8
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #366 +/- ##
==========================================
+ Coverage 96.85% 96.89% +0.04%
==========================================
Files 7 7
Lines 254 258 +4
==========================================
+ Hits 246 250 +4
Misses 8 8
Continue to review full report at Codecov.
|
lib/fs.js
Outdated
const { fs: memfs } = require('memfs'); | ||
// borrow join and normalize from memory-fs | ||
memfs.join = require('memory-fs/lib/join'); | ||
memfs.normalize = require('memory-fs/lib/normalize'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not good solution, we don't need two memory-fs package
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those functions appear to be unrelated to the file system itself and could live somewhere else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know, but it is bad have two memory fs package, we should move these function to separate repo or move to memfs
Also can you provide example where
|
Original comment moved here: https://gist.github.com/heygrady/ae6ec0d725c6c68974802ba5d8328617
This is what I meant:
// importFromMemoryFs.js
const { promisify } = require('util')
const requireFromString = require('require-from-string')
let cache = {}
const importFromMemoryFs = async (stats, pathname) => {
const { compilation, hash } = stats
const { outputFileSystem } = compilation.compiler
if (cache[hash] && cache[hash][pathname]) {
return cache[hash][pathname]
} else if (!cache[hash]) {
cache = { [hash]: {} }
}
const readFileAsync = promisify(outputFileSystem.readFile.bind(outputFileSystem))
try {
const buffer = await readFileAsync(pathname, 'utf8')
const source = buffer.toString()
const module = requireFromString(source, pathname)
cache[hash][pathname] = module
return module
} catch (error) {
console.error(error)
}
}
module.exports = importFromMemoryFs In my project I'm using webpack-dev-server with server-side-rendering. I was able to get it all working roughly as described here. // ssr-dev-middleware.js
const path = require('path')
const { wrap } = require('async-middleware')
const importFromMemoryFs = './importFromMemoryFs'
const pathToMain = path.join(__dirname, '..', 'dist', 'server', 'main.js')
module.exports = wrap(async (req, res, next) => {
const stats = res.locals.webpackStats.stats[1]
const middleware = await importFromMemoryFs(stats, pathToMain)
middleware(req, res, () => undefined)
}) |
I'm exploring easy ways to make https://github.com/heygrady/memory-fs/tree/memfs UpdateI was able to get every test passing except for the tests with win32 paths starting with Some differences:
Switching to memfs would probably be blocked by the difference in win32 support. I might've overlooked something obvious but the |
This is nice PR but I got far better solution I believe. #370 |
@laynef It's better to open a separate issue on that. It would be also nice to elaborate it with details. So far everything it worked smoothly for me. |
You can configure fs using options, maybe we revisit default memory fs in future, no need right now |
Anyway thank you for the PR |
Uses
memfs
instead ofmemory-fs
for the in-memoryoutputFileSystem
.memfs
appears to be actively maintained, whilememory-fs
hasn't had any meaningful updates in a few years.This allows people to use out-of-the-box webpack-dev-server for novel use-cases where having a fully API-compliant filesystem would be beneficial. Related: webpack/memory-fs#43
I ran into issues adding my own server-side-rendering middleware into webpack-dev-server that needed to
require
files from theoutputFileSystem
. Doing this with memory-fs was difficult, time consuming and seemed not to work very reliably. My alternative was to write to disk, which means 30 second re-builds.Interestingly,
memfs
is part of a suite of tools that enables useful stuff like patchingrequire
to import modules from an in-memory file system.This was a simple drop-in replacement.
join
andnormalize
methods from memory-fs to "a better place"