UNPKG

1.23 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5const parser_1 = __importDefault(require("./parser"));
6const image_size_1 = require("image-size");
7const cssTemplate_1 = require("./template/cssTemplate");
8const path_1 = __importDefault(require("path"));
9const defaultCSSConfig = {};
10function createClassName(fileName) {
11 return path_1.default.basename(fileName, path_1.default.extname(fileName));
12}
13async function DataURICSSParser(filePath, config = defaultCSSConfig) {
14 var _a;
15 const parser = new parser_1.default();
16 const background = await parser.encode(filePath);
17 const ast = Object.assign(Object.assign({}, defaultCSSConfig), config);
18 if (!background || !parser.fileName || !parser.buffer) {
19 throw `Unabled to read file ${filePath}`;
20 }
21 if (ast.width || ast.height || ast.backgroundSize) {
22 ast.dimensions = image_size_1.imageSize(parser.buffer);
23 }
24 return cssTemplate_1.cssParser(Object.assign(Object.assign({}, ast), { className: (_a = ast.className) !== null && _a !== void 0 ? _a : createClassName(parser.fileName), background }));
25}
26module.exports = DataURICSSParser;