UNPKG

3.45 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Stylable = void 0;
4const create_infra_structure_1 = require("./create-infra-structure");
5const diagnostics_1 = require("./diagnostics");
6const parser_1 = require("./parser");
7const stylable_processor_1 = require("./stylable-processor");
8const stylable_resolver_1 = require("./stylable-resolver");
9const stylable_transformer_1 = require("./stylable-transformer");
10class Stylable {
11 constructor(projectRoot, fileSystem, requireModule, delimiter = '__', onProcess, diagnostics = new diagnostics_1.Diagnostics(), hooks = {}, resolveOptions = {}, optimizer, mode = 'production', resolveNamespace, timedCacheOptions = {
12 timeout: 1,
13 useTimer: true,
14 }, resolveModule) {
15 this.projectRoot = projectRoot;
16 this.fileSystem = fileSystem;
17 this.requireModule = requireModule;
18 this.delimiter = delimiter;
19 this.onProcess = onProcess;
20 this.diagnostics = diagnostics;
21 this.hooks = hooks;
22 this.resolveOptions = resolveOptions;
23 this.optimizer = optimizer;
24 this.mode = mode;
25 this.resolveNamespace = resolveNamespace;
26 this.timedCacheOptions = timedCacheOptions;
27 this.resolveModule = resolveModule;
28 const { fileProcessor, resolvePath } = create_infra_structure_1.createInfrastructure(projectRoot, fileSystem, onProcess, resolveOptions, this.resolveNamespace, timedCacheOptions, resolveModule);
29 this.resolvePath = resolvePath;
30 this.fileProcessor = fileProcessor;
31 this.resolver = new stylable_resolver_1.StylableResolver(this.fileProcessor, this.requireModule);
32 }
33 static create(config) {
34 return new this(config.projectRoot, config.fileSystem, (id) => {
35 if (config.requireModule) {
36 return config.requireModule(id);
37 }
38 throw new Error('Javascript files are not supported without requireModule options');
39 }, config.delimiter, config.onProcess, config.diagnostics, config.hooks, config.resolveOptions, config.optimizer, config.mode, config.resolveNamespace, config.timedCacheOptions, config.resolveModule);
40 }
41 createTransformer(options = {}) {
42 return new stylable_transformer_1.StylableTransformer({
43 delimiter: this.delimiter,
44 diagnostics: new diagnostics_1.Diagnostics(),
45 fileProcessor: this.fileProcessor,
46 requireModule: this.requireModule,
47 postProcessor: this.hooks.postProcessor,
48 replaceValueHook: this.hooks.replaceValueHook,
49 mode: this.mode,
50 ...options,
51 });
52 }
53 transform(meta, resourcePath, options = {}) {
54 if (typeof meta === 'string') {
55 // TODO: refactor to use fileProcessor
56 // meta = this.fileProcessor.processContent(meta, resourcePath + '');
57 const root = parser_1.safeParse(meta, { from: resourcePath });
58 meta = new stylable_processor_1.StylableProcessor(undefined, this.resolveNamespace).process(root);
59 }
60 const transformer = this.createTransformer(options);
61 this.fileProcessor.add(meta.source, meta);
62 return transformer.transform(meta);
63 }
64 process(fullpath, context, ignoreCache) {
65 return this.fileProcessor.process(fullpath, ignoreCache, context);
66 }
67}
68exports.Stylable = Stylable;
69//# sourceMappingURL=stylable.js.map
\No newline at end of file