1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.doCompileStyle = exports.compileStyleAsync = exports.compileStyle = void 0;
|
7 | const postcss = require('postcss');
|
8 | const trim_1 = __importDefault(require("./stylePlugins/trim"));
|
9 | const scoped_1 = __importDefault(require("./stylePlugins/scoped"));
|
10 | const styleProcessors_1 = require("./styleProcessors");
|
11 | function compileStyle(options) {
|
12 | return doCompileStyle(Object.assign(Object.assign({}, options), { isAsync: false }));
|
13 | }
|
14 | exports.compileStyle = compileStyle;
|
15 | function compileStyleAsync(options) {
|
16 | return Promise.resolve(doCompileStyle(Object.assign(Object.assign({}, options), { isAsync: true })));
|
17 | }
|
18 | exports.compileStyleAsync = compileStyleAsync;
|
19 | function doCompileStyle(options) {
|
20 | const { filename, id, scoped = true, trim = true, preprocessLang, postcssOptions, postcssPlugins } = options;
|
21 | const preprocessor = preprocessLang && styleProcessors_1.processors[preprocessLang];
|
22 | const preProcessedSource = preprocessor && preprocess(options, preprocessor);
|
23 | const map = preProcessedSource ? preProcessedSource.map : options.map;
|
24 | const source = preProcessedSource ? preProcessedSource.code : options.source;
|
25 | const plugins = (postcssPlugins || []).slice();
|
26 | if (trim) {
|
27 | plugins.push(trim_1.default());
|
28 | }
|
29 | if (scoped) {
|
30 | plugins.push(scoped_1.default(id));
|
31 | }
|
32 | const postCSSOptions = Object.assign(Object.assign({}, postcssOptions), { to: filename, from: filename });
|
33 | if (map) {
|
34 | postCSSOptions.map = {
|
35 | inline: false,
|
36 | annotation: false,
|
37 | prev: map
|
38 | };
|
39 | }
|
40 | let result, code, outMap;
|
41 | const errors = [];
|
42 | if (preProcessedSource && preProcessedSource.errors.length) {
|
43 | errors.push(...preProcessedSource.errors);
|
44 | }
|
45 | try {
|
46 | result = postcss(plugins).process(source, postCSSOptions);
|
47 |
|
48 | if (options.isAsync) {
|
49 | return result
|
50 | .then((result) => ({
|
51 | code: result.css || '',
|
52 | map: result.map && result.map.toJSON(),
|
53 | errors,
|
54 | rawResult: result
|
55 | }))
|
56 | .catch((error) => ({
|
57 | code: '',
|
58 | map: undefined,
|
59 | errors: [...errors, error.message],
|
60 | rawResult: undefined
|
61 | }));
|
62 | }
|
63 |
|
64 | code = result.css;
|
65 | outMap = result.map;
|
66 | }
|
67 | catch (e) {
|
68 | errors.push(e);
|
69 | }
|
70 | return {
|
71 | code: code || ``,
|
72 | map: outMap && outMap.toJSON(),
|
73 | errors,
|
74 | rawResult: result
|
75 | };
|
76 | }
|
77 | exports.doCompileStyle = doCompileStyle;
|
78 | function preprocess(options, preprocessor) {
|
79 | return preprocessor.render(options.source, options.map, Object.assign({
|
80 | filename: options.filename
|
81 | }, options.preprocessOptions));
|
82 | }
|