UNPKG

2.29 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
6
7const loadPlugins = require('../utils/loadPlugins');
8
9const posthtml = require('posthtml');
10
11const posthtmlParse = require('posthtml-parser');
12
13function parse(_x, _x2) {
14 return _parse.apply(this, arguments);
15}
16
17function _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
30function transform(_x3) {
31 return _transform.apply(this, arguments);
32}
33
34function _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
50function getConfig(_x4) {
51 return _getConfig.apply(this, arguments);
52}
53
54function _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 // This is deprecated in favor of result messages but kept for compatibility
69 // See https://github.com/posthtml/posthtml-include/blob/e4f2a57c2e52ff721eed747b65eddf7d7a1451e3/index.js#L18-L26
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
87module.exports = {
88 parse,
89 transform
90};
\No newline at end of file