From c600d4471bf584045b3edc0a7b080584d11d8a97 Mon Sep 17 00:00:00 2001 From: luikore Date: Tue, 29 May 2018 01:15:27 +0800 Subject: [PATCH] Enable posthtml-parse options in posthtmlrc (#1316) --- src/assets/HTMLAsset.js | 7 +++---- src/transforms/posthtml.js | 27 +++++++++++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/assets/HTMLAsset.js b/src/assets/HTMLAsset.js index 83fd0b8c280..0645afe53a7 100644 --- a/src/assets/HTMLAsset.js +++ b/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'); @@ -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; @@ -151,7 +150,7 @@ class HTMLAsset extends Asset { } async pretransform() { - await posthtmlTransform(this); + await posthtmlTransform.transform(this); } async transform() { diff --git a/src/transforms/posthtml.js b/src/transforms/posthtml.js index 66b7015bac4..29f9f2d6b20 100644 --- a/src/transforms/posthtml.js +++ b/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; @@ -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; } @@ -38,3 +51,5 @@ async function getConfig(asset) { config.skipParse = true; return config; } + +module.exports = {parse, transform};