1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
6 |
|
7 | const loadPlugins = require('../utils/loadPlugins');
|
8 |
|
9 | const posthtml = require('posthtml');
|
10 |
|
11 | const posthtmlParse = require('posthtml-parser');
|
12 |
|
13 | function parse(_x, _x2) {
|
14 | return _parse.apply(this, arguments);
|
15 | }
|
16 |
|
17 | function _parse() {
|
18 | _parse = (0, _asyncToGenerator2.default)(function* (code, asset) {
|
19 | var config = yield getConfig(asset);
|
20 |
|
21 | if (!config) {
|
22 | config = {};
|
23 | }
|
24 |
|
25 | return posthtmlParse(code, config);
|
26 | });
|
27 | return _parse.apply(this, arguments);
|
28 | }
|
29 |
|
30 | function transform(_x3) {
|
31 | return _transform.apply(this, arguments);
|
32 | }
|
33 |
|
34 | function _transform() {
|
35 | _transform = (0, _asyncToGenerator2.default)(function* (asset) {
|
36 | let config = yield getConfig(asset);
|
37 |
|
38 | if (!config) {
|
39 | return;
|
40 | }
|
41 |
|
42 | yield asset.parseIfNeeded();
|
43 | let res = yield posthtml(config.plugins).process(asset.ast, config);
|
44 | asset.ast = res.tree;
|
45 | asset.isAstDirty = true;
|
46 | });
|
47 | return _transform.apply(this, arguments);
|
48 | }
|
49 |
|
50 | function getConfig(_x4) {
|
51 | return _getConfig.apply(this, arguments);
|
52 | }
|
53 |
|
54 | function _getConfig() {
|
55 | _getConfig = (0, _asyncToGenerator2.default)(function* (asset) {
|
56 | let config = yield asset.getConfig(['.posthtmlrc', '.posthtmlrc.js', 'posthtml.config.js'], {
|
57 | packageKey: 'posthtml'
|
58 | });
|
59 |
|
60 | if (!config && !asset.options.minify) {
|
61 | return;
|
62 | }
|
63 |
|
64 | config = config || {};
|
65 | const plugins = config.plugins;
|
66 |
|
67 | if (typeof plugins === 'object') {
|
68 |
|
69 |
|
70 | const depConfig = {
|
71 | addDependencyTo: {
|
72 | addDependency: name => asset.addDependency(name, {
|
73 | includedInParent: true
|
74 | })
|
75 | }
|
76 | };
|
77 | Object.keys(plugins).forEach(p => Object.assign(plugins[p], depConfig));
|
78 | }
|
79 |
|
80 | config.plugins = yield loadPlugins(plugins, asset.name);
|
81 | config.skipParse = true;
|
82 | return config;
|
83 | });
|
84 | return _getConfig.apply(this, arguments);
|
85 | }
|
86 |
|
87 | module.exports = {
|
88 | parse,
|
89 | transform
|
90 | }; |
\ | No newline at end of file |