UNPKG

devextreme-themebuilder

Version:
112 lines (111 loc) 5.33 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const sass = __importStar(require("sass-embedded")); const dx_theme_builder_metadata_1 = require("../data/metadata/dx-theme-builder-metadata"); const parse_value_1 = require("./parse-value"); const post_compiler_1 = require("./post-compiler"); class Compiler { constructor() { this.changedVariables = {}; this.importerCache = {}; this.meta = dx_theme_builder_metadata_1.metadata; this.userItems = {}; this.compile = (file, items, options) => __awaiter(this, void 0, void 0, function* () { return this.sassCompile(file, items, options, sass.compileAsync); }); this.compileString = (content, items, options) => __awaiter(this, void 0, void 0, function* () { return this.sassCompile(content, items, options, sass.compileStringAsync); }); this.sassCompile = (source, items, options, compile) => __awaiter(this, void 0, void 0, function* () { this.changedVariables = {}; if (items) { this.userItems = items.reduce((acc, item) => { acc[item.key] = item; return acc; }, {}); } let compilerOptions = { importers: [{ canonicalize: this.canonicalize, load: this.load, }], functions: { 'collector($map)': this.collector, 'getCustomVar($value)': this.getCustomVar, }, }; compilerOptions = Object.assign(Object.assign({}, compilerOptions), options); return new Promise((resolve, reject) => { compile(source, compilerOptions) .then((data) => { resolve({ result: Object.assign(Object.assign({}, data), { css: (0, post_compiler_1.optimizeCss)(data.css) }), changedVariables: this.changedVariables, }); }) .catch((error) => reject(error)); }); }); this.canonicalize = (url) => (url.includes('tb_index') ? new URL(`db:${url}`) : null); this.load = () => ({ contents: this.indexFileContent, syntax: 'scss', }); this.getCustomVar = (values) => { const customVariable = values[0].get(0); const nameVariable = customVariable.get(0); let result = sass.sassNull; const customerVariable = this.userItems[nameVariable.text]; if (customerVariable) { result = (0, parse_value_1.parseString)(customerVariable.value); } return result; }; this.collector = (maps) => { maps.forEach((map) => { map.asList.forEach((value) => { if (value.get(1) === sass.sassNull) { return; } const key = value.get(0); if (!(key instanceof sass.SassString)) { return; } const variableKey = key.text; const variableValue = (0, parse_value_1.parse)(value.get(1)); this.changedVariables[variableKey] = variableValue; }); }); return sass.sassNull; }; } } exports.default = Compiler;