UNPKG

2.43 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), createDiagnostics) {
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 const resolvedFrom = resolvePath(projectRoot, from);
29 return stylable_processor_1.process(parser_1.safeParse(content, { from: resolvedFrom }), createDiagnostics === null || createDiagnostics === void 0 ? void 0 : createDiagnostics(resolvedFrom), resolveNamespace);
30 }, {
31 readFileSync(resolvedPath) {
32 return fileSystem.readFileSync(resolvedPath, 'utf8');
33 },
34 statSync(resolvedPath) {
35 const stat = fileSystem.statSync(resolvedPath);
36 if (!stat.mtime) {
37 return {
38 mtime: new Date(0),
39 };
40 }
41 return stat;
42 },
43 readlinkSync() {
44 throw new Error(`not implemented`);
45 },
46 }, (path, context) => resolvePath(context || projectRoot, path));
47 if (onProcess) {
48 fileProcessor.postProcessors.push(onProcess);
49 }
50 return {
51 resolvePath,
52 fileProcessor,
53 };
54}
55exports.createInfrastructure = createInfrastructure;
56//# sourceMappingURL=create-infra-structure.js.map
\No newline at end of file