/
posthtml.js
55 lines (48 loc) 路 1.35 KB
/
posthtml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const loadPlugins = require('../utils/loadPlugins');
const posthtml = require('posthtml');
const posthtmlParse = require('posthtml-parser');
async function parse(code, asset) {
var config = await getConfig(asset);
if (!config) {
config = {};
}
config = Object.assign({lowerCaseAttributeNames: true}, config);
return posthtmlParse(code, config);
}
async function transform(asset) {
let config = await getConfig(asset);
if (!config) {
return;
}
await asset.parseIfNeeded();
let res = await posthtml(config.plugins).process(asset.ast, config);
asset.ast = res.tree;
asset.isAstDirty = true;
}
async function getConfig(asset) {
let config =
(await asset.getPackage()).posthtml ||
(await asset.getConfig([
'.posthtmlrc',
'.posthtmlrc.js',
'posthtml.config.js'
]));
if (!config && !asset.options.minify) {
return;
}
config = Object.assign({}, config);
const plugins = config.plugins;
if (typeof plugins === 'object') {
const depConfig = {
addDependencyTo: {
addDependency: name =>
asset.addDependency(name, {includedInParent: true})
}
};
Object.keys(plugins).forEach(p => Object.assign(plugins[p], depConfig));
}
config.plugins = await loadPlugins(plugins, asset.name);
config.skipParse = true;
return config;
}
module.exports = {parse, transform};