Skip to content

Commit

Permalink
feat(cli): allow stuff empty attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Genuifx committed Mar 6, 2020
1 parent f234ce2 commit e2998af
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
18 changes: 9 additions & 9 deletions packages/wxa-cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/wxa-cli/src/const/defaultWxaConfigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default class DefaultWxaConfigs {
splitDeps: {
maxDeps: -1,
},
allowEmptyAttributes: true,
},
};
}
Expand Down
11 changes: 11 additions & 0 deletions packages/wxa-cli/src/optimizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {AsyncSeriesHook} from 'tapable';
import ProgressBar from '../helpers/progressTextBar';
import SplitDeps from './splitDeps';
import types from '../const/types';
import {stuffEmptyAttributs} from './stuffEmptyAttributes';

let debug = debugPKG('WXA:Optimizer');

Expand Down Expand Up @@ -34,6 +35,7 @@ export default class Optimizer {

await Promise.all(optimizeTasks);


if (!this.cmdOptions.watch) this.splitDeps.run(indexedMap);
}

Expand All @@ -43,13 +45,22 @@ export default class Optimizer {
debugger;
return;
}

const text = path.relative(this.cwd, dep.src);
this.progress.draw(text, 'Optimizing', !this.cmdOptions.verbose);

if (~types.WECHAT.concat(types.TT).indexOf(this.wxaConfigs.target)) {
this.doWxaOptimize(dep, indexedMap);
}

// stuff xml empty Attributes
if (
this.wxaConfigs.optimization.allowEmptyAttributes &&
~['xml', 'wxml'].indexOf(dep.kind)
) {
stuffEmptyAttributs(dep);
}

await this.hooks.optimizeAssets.promise(dep);
}

Expand Down
27 changes: 27 additions & 0 deletions packages/wxa-cli/src/optimizer/stuffEmptyAttributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// import {f} from 'domutils';
import {parseXML, serializeXML} from '../compilers/xml.js';
import {Parser} from 'htmlparser2';
import path from 'path';

export function stuffEmptyAttributs(mdl) {
let dom = parseXML(mdl.code, path.parse(mdl.src));

walkDOM(dom);

mdl.code = serializeXML(dom);
}

function walkDOM(dom) {
dom.forEach((ele)=>{
if (ele.attribs) {
Object.keys(ele.attribs).forEach((attrName)=>{
// suff empty attribs
if (ele.attribs[attrName] === '') ele.attribs[attrName] = attrName;
});
}

if (Array.isArray(ele.children) && ele.children.length) {
walkDOM(ele.children);
}
});
}

0 comments on commit e2998af

Please sign in to comment.