Skip to content

Commit

Permalink
feat: 支持optimization 参数
Browse files Browse the repository at this point in the history
  • Loading branch information
ximing committed Mar 18, 2019
1 parent b61472c commit b63a27a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 33 deletions.
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const HandleJSONComponentDep = require('./plugin/handleJSONComponentDep');
const HandleWXSSDep = require('./plugin/handleWXSSDep');
const HandleWXMLDep = require('./plugin/handleWXMLDep');
const NpmRewrite = require('./plugin/npmRewrite');
const MinifyPlugin = require('./plugin/minifyPlugin');
const AppJSONPick = require('./plugin/appJSONPick');
const CopyImagePlugin = require('./plugin/copyImagePlugin');
const CopyPlugin = require('./plugin/copyPlugin');
Expand All @@ -44,6 +45,12 @@ class Mpbuilder {
beforeEmitFile: new AsyncSeriesWaterfallHook(['asset']),
watchRun: new AsyncSeriesHook(['compiler'])
};
this.optimization = Object.assign(
{
minimize: true
},
config.optimization
);
this.watching = new Watching(this, async () => {
await this.hooks.afterCompile.promise(this);
});
Expand All @@ -66,7 +73,8 @@ class Mpbuilder {
new HandleJSONComponentDep(),
new HandleWXMLDep(),
new HandleWXSSDep(),
new NpmRewrite()
new NpmRewrite(),
new MinifyPlugin()
],
this.config.plugins
);
Expand Down
9 changes: 0 additions & 9 deletions src/loader/html-minifier-loader.js

This file was deleted.

11 changes: 0 additions & 11 deletions src/loader/json-loader.js

This file was deleted.

6 changes: 0 additions & 6 deletions src/loader/wxml-loader.js

This file was deleted.

6 changes: 0 additions & 6 deletions src/loaderManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,17 @@ const mm = require('micromatch');

const babelLoader = require('./loader/babel-loader');
const fileLoader = require('./loader/file-loader');
const jsonLoader = require('./loader/json-loader');
const replaceLoader = require('./loader/replace-loader');
const taroJSLoader = require('./loader/taro-js-loader');
const tsLoader = require('./loader/ts-loader');
const wxmlLoader = require('./loader/wxml-loader');
const postcssLoader = require('./loader/postcss-loader');
const htmlMinifierLoader = require('./loader/html-minifier-loader');

const map = {
'babel-loader': babelLoader,
'file-loader': fileLoader,
'json-loader': jsonLoader,
'replace-loader': replaceLoader,
'taro-js-loader': taroJSLoader,
'ts-loader': tsLoader,
'wxml-loader': wxmlLoader,
'html-minifier-loader': htmlMinifierLoader,
'postcss-loader': postcssLoader
};

Expand Down
33 changes: 33 additions & 0 deletions src/plugin/minifyPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Created by ximing on 2019-03-18.
*/
const UglifyJS = require('uglify-js');
const jsonminify = require('jsonminify');
const htmlmin = require('html-minifier');

module.exports = class MinifyPlugin {
apply(mpb) {
if (mpb.optimization.minimize) {
mpb.hooks.beforeEmitFile.tapPromise('MinifyPlugin', async (asset) => {
if (asset.contents) {
if (/\.js$/.test(asset.outputFilePath)) {
const result = UglifyJS.minify(asset.contents);
if (result.error) console.error('[MinifyPlugin]', result.error);
if (result.warnings) console.warn('[MinifyPlugin]', result.warnings);
asset.contents = result.code;
} else if (/\.json$/.test(asset.outputFilePath)) {
asset.contents = jsonminify(asset.contents).toString();
} else if (/\.wxml$/.test(asset.outputFilePath)) {
asset.contents = asset.contents = htmlmin.minify(asset.contents, {
removeComments: true,
keepClosingSlash: true,
collapseWhitespace: true,
caseSensitive: true
});
}
}
return Promise.resolve();
});
}
}
};

0 comments on commit b63a27a

Please sign in to comment.