@@ -8,54 +8,64 @@ const workerpool = require('workerpool');
88
99const pool = workerpool . pool ( ) ;
1010
11- function minify ( contents , outputFilePath ) {
11+ function minifyJS ( contents ) {
1212 const UglifyJS = require ( 'uglify-js' ) ;
13- const jsonminify = require ( 'jsonminify' ) ;
13+ const result = UglifyJS . minify ( contents ) ;
14+ if ( result . error ) console . error ( '[MinifyPlugin]' , result . error ) ;
15+ if ( result . warnings ) console . warn ( '[MinifyPlugin]' , result . warnings ) ;
16+ return result . code ;
17+ }
18+ function minifyWXML ( contents ) {
1419 const htmlmin = require ( 'html-minifier' ) ;
15- if ( / \. j s $ / . test ( outputFilePath ) ) {
16- const result = UglifyJS . minify ( contents ) ;
17- if ( result . error ) console . error ( '[MinifyPlugin]' , result . error ) ;
18- if ( result . warnings ) console . warn ( '[MinifyPlugin]' , result . warnings ) ;
19- return result . code ;
20- }
21- if ( / \. j s o n $ / . test ( outputFilePath ) ) {
22- return jsonminify ( contents ) . toString ( ) ;
23- }
24- if ( / \. w x m l $ / . test ( outputFilePath ) ) {
25- return htmlmin . minify ( contents , {
26- removeComments : true ,
27- keepClosingSlash : true ,
28- collapseWhitespace : true ,
29- caseSensitive : true
30- } ) ;
31- }
20+ return htmlmin . minify ( contents , {
21+ removeComments : true ,
22+ keepClosingSlash : true ,
23+ collapseWhitespace : true ,
24+ caseSensitive : true
25+ } ) ;
26+ }
27+ function minifyJSON ( contents ) {
28+ const jsonminify = require ( 'jsonminify' ) ;
29+ return jsonminify ( contents ) . toString ( ) ;
3230}
3331
3432module . exports = class MinifyPlugin {
33+ constructor ( ) {
34+ this . js = true ;
35+ this . wxml = true ;
36+ this . json = true ;
37+ }
38+
3539 apply ( mpb ) {
3640 if ( mpb . optimization . minimize ) {
41+ if ( typeof mpb . optimization . minimize === 'object' ) {
42+ this . js = mpb . optimization . minimize . js ;
43+ this . wxml = mpb . optimization . minimize . wxml ;
44+ this . json = mpb . optimization . minimize . json ;
45+ } else if ( mpb . optimization . minimize === false ) {
46+ this . js = false ;
47+ this . wxml = false ;
48+ this . json = false ;
49+ }
3750 mpb . hooks . beforeEmitFile . tapPromise ( 'MinifyPlugin' , async ( asset ) => {
3851 if ( asset . contents ) {
39- // if (/\.js$/.test(asset.outputFilePath)) {
40- // const result = UglifyJS.minify(asset.contents);
41- // if (result.error) console.error('[MinifyPlugin]', result.error);
42- // if (result.warnings) console.warn('[MinifyPlugin]', result.warnings);
43- // asset.contents = result.code;
44- // } else if (/\.json$/.test(asset.outputFilePath)) {
45- // asset.contents = jsonminify(asset.contents).toString();
46- // } else if (/\.wxml$/.test(asset.outputFilePath)) {
47- // asset.contents = asset.contents = htmlmin.minify(asset.contents, {
48- // removeComments: true,
49- // keepClosingSlash: true,
50- // collapseWhitespace: true,
51- // caseSensitive: true
52- // });
53- // }
54- if ( / \. ( j s | j s o n | w x m l ) $ / . test ( asset . outputFilePath ) ) {
55- asset . contents = await pool . exec ( minify , [
56- asset . contents ,
57- asset . outputFilePath
58- ] ) ;
52+ if ( / \. j s $ / . test ( asset . outputFilePath ) && this . js ) {
53+ // const result = UglifyJS.minify(asset.contents);
54+ // if (result.error) console.error('[MinifyPlugin]', result.error);
55+ // if (result.warnings) console.warn('[MinifyPlugin]', result.warnings);
56+ // asset.contents = result.code;
57+ asset . contents = await pool . exec ( minifyJS , [ asset . contents ] ) ;
58+ } else if ( / \. j s o n $ / . test ( asset . outputFilePath ) && this . json ) {
59+ // asset.contents = jsonminify(asset.contents).toString();
60+ asset . contents = await pool . exec ( minifyJSON , [ asset . contents ] ) ;
61+ } else if ( / \. w x m l $ / . test ( asset . outputFilePath ) && this . wxml ) {
62+ // asset.contents = asset.contents = htmlmin.minify(asset.contents, {
63+ // removeComments: true,
64+ // keepClosingSlash: true,
65+ // collapseWhitespace: true,
66+ // caseSensitive: true
67+ // });
68+ asset . contents = await pool . exec ( minifyWXML , [ asset . contents ] ) ;
5969 }
6070 }
6171 return Promise . resolve ( ) ;
0 commit comments