1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | const parser_1 = __importDefault(require("./parser"));
|
6 | const image_size_1 = require("image-size");
|
7 | const cssTemplate_1 = require("./template/cssTemplate");
|
8 | const path_1 = __importDefault(require("path"));
|
9 | const defaultCSSConfig = {};
|
10 | function createClassName(fileName) {
|
11 | return path_1.default.basename(fileName, path_1.default.extname(fileName));
|
12 | }
|
13 | async 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 | }
|
26 | module.exports = DataURICSSParser;
|