@stylable/core
Version:
CSS for Components
69 lines • 3.45 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Stylable = void 0;
const create_infra_structure_1 = require("./create-infra-structure");
const diagnostics_1 = require("./diagnostics");
const parser_1 = require("./parser");
const stylable_processor_1 = require("./stylable-processor");
const stylable_resolver_1 = require("./stylable-resolver");
const stylable_transformer_1 = require("./stylable-transformer");
class Stylable {
constructor(projectRoot, fileSystem, requireModule, delimiter = '__', onProcess, diagnostics = new diagnostics_1.Diagnostics(), hooks = {}, resolveOptions = {}, optimizer, mode = 'production', resolveNamespace, timedCacheOptions = {
timeout: 1,
useTimer: true,
}, resolveModule) {
this.projectRoot = projectRoot;
this.fileSystem = fileSystem;
this.requireModule = requireModule;
this.delimiter = delimiter;
this.onProcess = onProcess;
this.diagnostics = diagnostics;
this.hooks = hooks;
this.resolveOptions = resolveOptions;
this.optimizer = optimizer;
this.mode = mode;
this.resolveNamespace = resolveNamespace;
this.timedCacheOptions = timedCacheOptions;
this.resolveModule = resolveModule;
const { fileProcessor, resolvePath } = create_infra_structure_1.createInfrastructure(projectRoot, fileSystem, onProcess, resolveOptions, this.resolveNamespace, timedCacheOptions, resolveModule);
this.resolvePath = resolvePath;
this.fileProcessor = fileProcessor;
this.resolver = new stylable_resolver_1.StylableResolver(this.fileProcessor, this.requireModule);
}
static create(config) {
return new this(config.projectRoot, config.fileSystem, (id) => {
if (config.requireModule) {
return config.requireModule(id);
}
throw new Error('Javascript files are not supported without requireModule options');
}, config.delimiter, config.onProcess, config.diagnostics, config.hooks, config.resolveOptions, config.optimizer, config.mode, config.resolveNamespace, config.timedCacheOptions, config.resolveModule);
}
createTransformer(options = {}) {
return new stylable_transformer_1.StylableTransformer({
delimiter: this.delimiter,
diagnostics: new diagnostics_1.Diagnostics(),
fileProcessor: this.fileProcessor,
requireModule: this.requireModule,
postProcessor: this.hooks.postProcessor,
replaceValueHook: this.hooks.replaceValueHook,
mode: this.mode,
...options,
});
}
transform(meta, resourcePath, options = {}) {
if (typeof meta === 'string') {
// TODO: refactor to use fileProcessor
// meta = this.fileProcessor.processContent(meta, resourcePath + '');
const root = parser_1.safeParse(meta, { from: resourcePath });
meta = new stylable_processor_1.StylableProcessor(undefined, this.resolveNamespace).process(root);
}
const transformer = this.createTransformer(options);
this.fileProcessor.add(meta.source, meta);
return transformer.transform(meta);
}
process(fullpath, context, ignoreCache) {
return this.fileProcessor.process(fullpath, ignoreCache, context);
}
}
exports.Stylable = Stylable;
//# sourceMappingURL=stylable.js.map