通过 memory-fs
设定webpack的outputFileSystem
自定义文件系统,想把打包后的文件写到内存中,
`
const MemoryFS = require("memory-fs");
const webpack = require("webpack");
const fs = new MemoryFS();
const compiler = webpack(config, (err, stats) => {
if (err || stats.hasErrors()) {
// 在这里处理错误
} else {
console.log('成功!');
}
// 处理完成
});
compiler.outputFileSystem = fs;
compiler.run((err, stats) => {
// 之后读取输出:
const content = fs.readFileSync(path.join(__dirname,"./dist/search.js"),'utf-8');
console.log(content);
});
`
会报
`
MemoryFileSystemError: ENOENT: no such file or directory, readFile 'E:\study\dist\search.js'
at MemoryFileSystem.readFileSync (E:\study\node_modules\?[4m.0.5.0@memory-fs?[24m\lib\MemoryFileSys
at E:\study\webpackTest.js:102:24
`
这样的错误 ,dist目录下的search.js是没有设置自定义文件系统,写到磁盘中的文件名,现在想从内存中读取最后打包文件内容,该如何写读取的路径呀,好像怎么写都会报没有这个文件或者目录 。
换了一个环境 ,这样指定路径
就能获取到相应的内容了