Skip to content

Commit

Permalink
Enable posthtml-parse options in posthtmlrc (#1316)
Browse files Browse the repository at this point in the history
  • Loading branch information
luikore authored and Jasper De Moor committed May 28, 2018
1 parent 0d63879 commit c600d44
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
7 changes: 3 additions & 4 deletions src/assets/HTMLAsset.js
@@ -1,5 +1,4 @@
const Asset = require('../Asset');
const parse = require('posthtml-parser');
const api = require('posthtml/lib/api');
const urlJoin = require('../utils/urlJoin');
const render = require('posthtml-render');
Expand Down Expand Up @@ -79,8 +78,8 @@ class HTMLAsset extends Asset {
this.isAstDirty = false;
}

parse(code) {
let res = parse(code, {lowerCaseAttributeNames: true});
async parse(code) {
let res = await posthtmlTransform.parse(code, this);
res.walk = api.walk;
res.match = api.match;
return res;
Expand Down Expand Up @@ -151,7 +150,7 @@ class HTMLAsset extends Asset {
}

async pretransform() {
await posthtmlTransform(this);
await posthtmlTransform.transform(this);
}

async transform() {
Expand Down
27 changes: 21 additions & 6 deletions src/transforms/posthtml.js
@@ -1,7 +1,17 @@
const loadPlugins = require('../utils/loadPlugins');
const posthtml = require('posthtml');
const posthtmlParse = require('posthtml-parser');

module.exports = async function(asset) {
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;
Expand All @@ -12,13 +22,16 @@ module.exports = async function(asset) {

asset.ast = res.tree;
asset.isAstDirty = true;
};
}

async function getConfig(asset) {
let config = await asset.getConfig(
['.posthtmlrc', '.posthtmlrc.js', 'posthtml.config.js'],
{packageKey: 'posthtml'}
);
let config =
(await asset.getPackage()).posthtml ||
(await asset.getConfig([
'.posthtmlrc',
'.posthtmlrc.js',
'posthtml.config.js'
]));
if (!config && !asset.options.minify) {
return;
}
Expand All @@ -38,3 +51,5 @@ async function getConfig(asset) {
config.skipParse = true;
return config;
}

module.exports = {parse, transform};

0 comments on commit c600d44

Please sign in to comment.