Skip to content

Commit

Permalink
feat: 增加minify include exclude
Browse files Browse the repository at this point in the history
  • Loading branch information
liujin10 committed Dec 16, 2020
1 parent 1973774 commit 1bfdce0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mpbuild",
"version": "1.4.9",
"version": "1.4.10",
"description": "",
"main": "src/index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/loaderManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ module.exports = class LoaderManager {
if (test.test(asset.name)) {
let shouleRunLoder = true;
if (Array.isArray(exclude)) {
const shouldExclude = mm.any(asset.path, exclude);
if (shouldExclude && Array.isArray(include)) {
shouleRunLoder = mm.any(asset.path, exclude);
if (!shouldExclude && Array.isArray(include)) {
shouleRunLoder = mm.any(asset.path, include);
}
}
Expand Down
26 changes: 24 additions & 2 deletions src/plugin/minifyPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// const jsonminify = require('jsonminify');
// const htmlmin = require('html-minifier');
const workerpool = require('workerpool');
const mm = require('micromatch');

const pool = workerpool.pool();

Expand Down Expand Up @@ -51,6 +52,27 @@ function minifyJSON(contents) {
return jsonminify(contents).toString();
}

function sholdRunMiniFunc(asset, rule) {
if(!rule) {
return false;
}
if(typeof rule === "boolean" && rule) {
return true;
}
if(Object.prototype.toString.call(rule) === '[object Object]') {
const {include, exclude} = rule || {};
let sholdRunMini = true;
if (Array.isArray(exclude)) {
sholdRunMini = !mm.any(asset.path, exclude);
if (!sholdRunMini && Array.isArray(include)) {
sholdRunMini = mm.any(asset.path, include);
}
}
return sholdRunMini;
}
return false
}

module.exports = class MinifyPlugin {
constructor() {
this.js = true;
Expand Down Expand Up @@ -80,10 +102,10 @@ module.exports = class MinifyPlugin {
asset.contents,
this.js
]);
} else if (/\.json$/.test(asset.outputFilePath) && this.json) {
} else if (/\.json$/.test(asset.outputFilePath) && sholdRunMiniFunc(asset, this.json)) {
// asset.contents = jsonminify(asset.contents).toString();
asset.contents = await pool.exec(minifyJSON, [asset.contents]);
} else if (/\.wxml$/.test(asset.outputFilePath) && this.wxml) {
} else if (/\.wxml$/.test(asset.outputFilePath) && sholdRunMiniFunc(asset, this.wxml)) {
// asset.contents = asset.contents = htmlmin.minify(asset.contents, {
// removeComments: true,
// keepClosingSlash: true,
Expand Down

0 comments on commit 1bfdce0

Please sign in to comment.