mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-04 08:00:01 +08:00
The recent memfs updates (#39559) no longer work with our asset pipeline, because the package started requiring some standard node packages (stream, buffer, path, events) which are not available in our env (this is why we needed the memfs in the first place!)
51 lines
1.1 KiB
JavaScript
51 lines
1.1 KiB
JavaScript
// Minimal in-memory fs for `@rollup/browser`. Only implements what
|
|
// it actually needs for bundling.
|
|
|
|
const FILE_STATS = {
|
|
isFile: () => true,
|
|
isSymbolicLink: () => false,
|
|
};
|
|
|
|
export default function createVirtualFs(modules, basePath) {
|
|
const files = new Map();
|
|
const dirs = new Map();
|
|
|
|
for (const [key, content] of Object.entries(modules)) {
|
|
const path = basePath + key;
|
|
files.set(path, content);
|
|
|
|
const slash = path.lastIndexOf("/");
|
|
const parent = path.slice(0, slash);
|
|
let children = dirs.get(parent);
|
|
if (!children) {
|
|
children = [];
|
|
dirs.set(parent, children);
|
|
}
|
|
children.push(path.slice(slash + 1));
|
|
}
|
|
|
|
return {
|
|
async readFile(path) {
|
|
const content = files.get(path);
|
|
if (content === undefined) {
|
|
throw new Error(`ENOENT: ${path}`);
|
|
}
|
|
return content;
|
|
},
|
|
|
|
async readdir(path) {
|
|
const children = dirs.get(path);
|
|
if (!children) {
|
|
throw new Error(`ENOENT: ${path}`);
|
|
}
|
|
return children;
|
|
},
|
|
|
|
async lstat(path) {
|
|
if (files.has(path)) {
|
|
return FILE_STATS;
|
|
}
|
|
throw new Error(`ENOENT: ${path}`);
|
|
},
|
|
};
|
|
}
|