UNPKG

@stylable/core

Version:

CSS for Components

90 lines 2.93 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createMinimalFS = void 0; const path_1 = require("path"); const deindent_1 = __importDefault(require("deindent")); function createMinimalFS({ files, trimWS }) { const creationDate = new Date(); const filePaths = new Map(Object.entries(files).map(([filePath, { content, mtime = creationDate }]) => [ filePath, { content, mtime }, ])); const directoryPaths = new Set(); for (const filePath of filePaths.keys()) { for (const directoryPath of getParentPaths(path_1.dirname(filePath))) { directoryPaths.add(directoryPath); } } const fs = { readFileSync(path) { if (!files[path]) { throw new Error('Cannot find file: ' + path); } if (trimWS) { return deindent_1.default(files[path].content).trim(); } return files[path].content; }, statSync(path) { const isDirectory = directoryPaths.has(path); const fileEntry = filePaths.get(path); if (!fileEntry && !isDirectory) { throw new Error(`ENOENT: no such file or directory, stat ${path}`); } return { isDirectory() { return isDirectory; }, isFile() { return !!fileEntry; }, mtime: fileEntry ? fileEntry.mtime : new Date(), }; }, readlinkSync() { throw new Error(`not implemented`); }, }; const requireModule = function require(id) { const _module = { id, exports: {}, }; try { if (!id.match(/\.js$/)) { id += '.js'; } // eslint-disable-next-line @typescript-eslint/no-implied-eval const fn = new Function('module', 'exports', 'require', files[id].content); fn(_module, _module.exports, requireModule); } catch (e) { throw new Error('Cannot require file: ' + id); } return _module.exports; }; function resolvePath(_ctx, path) { return path; } return { fs, requireModule, resolvePath, }; } exports.createMinimalFS = createMinimalFS; function getParentPaths(initialDirectoryPath) { const parentPaths = []; let currentPath = initialDirectoryPath; let lastPath; while (currentPath !== lastPath) { parentPaths.push(currentPath); lastPath = currentPath; currentPath = path_1.dirname(currentPath); } return parentPaths; } //# sourceMappingURL=memory-minimal-fs.js.map