UNPKG

2.28 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.createInfrastructure = void 0;
7const path_1 = __importDefault(require("path"));
8const cached_process_file_1 = require("./cached-process-file");
9const parser_1 = require("./parser");
10const stylable_processor_1 = require("./stylable-processor");
11const timed_cache_1 = require("./timed-cache");
12const module_resolver_1 = require("./module-resolver");
13function createInfrastructure(projectRoot, fileSystem, onProcess, resolveOptions = {}, resolveNamespace, timedCacheOptions, resolveModule = module_resolver_1.createDefaultResolver(fileSystem, resolveOptions)) {
14 let resolvePath = (context = projectRoot, moduleId) => {
15 if (!path_1.default.isAbsolute(moduleId) && !moduleId.startsWith('.')) {
16 moduleId = resolveModule(context, moduleId);
17 }
18 return moduleId;
19 };
20 if (timedCacheOptions) {
21 const cacheManager = timed_cache_1.timedCache(resolvePath, {
22 createKey: (args) => args.join(';'),
23 ...timedCacheOptions,
24 });
25 resolvePath = cacheManager.get;
26 }
27 const fileProcessor = cached_process_file_1.cachedProcessFile((from, content) => {
28 return stylable_processor_1.process(parser_1.safeParse(content, { from: resolvePath(projectRoot, from) }), undefined, resolveNamespace);
29 }, {
30 readFileSync(resolvedPath) {
31 return fileSystem.readFileSync(resolvedPath, 'utf8');
32 },
33 statSync(resolvedPath) {
34 const stat = fileSystem.statSync(resolvedPath);
35 if (!stat.mtime) {
36 return {
37 mtime: new Date(0),
38 };
39 }
40 return stat;
41 },
42 readlinkSync() {
43 throw new Error(`not implemented`);
44 },
45 }, (path, context) => resolvePath(context || projectRoot, path));
46 if (onProcess) {
47 fileProcessor.postProcessors.push(onProcess);
48 }
49 return {
50 resolvePath,
51 fileProcessor,
52 };
53}
54exports.createInfrastructure = createInfrastructure;
55//# sourceMappingURL=create-infra-structure.js.map
\No newline at end of file