1
- var ejs = require ( 'ejs' ) ,
2
- UglifyJS = require ( 'uglify-js' ) ,
3
- utils = require ( 'loader-utils' ) ,
4
- path = require ( 'path' ) ,
5
- htmlmin = require ( 'html-minifier' ) ,
6
- merge = require ( 'merge' ) ;
7
-
1
+ var ejs = require ( 'ejs' ) ;
2
+ var path = require ( 'path' ) ;
3
+ var merge = require ( 'merge' ) ;
4
+ var utils = require ( 'loader-utils' ) ;
5
+ var terser = require ( 'terser' ) ;
6
+ var htmlmin = require ( 'html-minifier' ) ;
8
7
9
8
module . exports = function ( source ) {
10
9
this . cacheable && this . cacheable ( ) ;
11
10
12
- var query = typeof this . query === 'object' ? this . query : utils . parseQuery ( this . query ) ;
13
- var opts = merge ( this . options [ 'ejs-compiled-loader' ] || { } , query ) ;
14
- opts . client = true ;
15
-
16
- // Skip compile debug for production when running with
17
- // webpack --optimize-minimize
18
- if ( this . minimize && opts . compileDebug === undefined ) {
19
- opts . compileDebug = false ;
20
- }
21
-
22
- // Use filenames relative to working dir, which should be project root
23
- opts . filename = path . relative ( process . cwd ( ) , this . resourcePath ) ;
24
-
25
- if ( opts . htmlmin ) {
26
- source = htmlmin . minify ( source , opts [ 'htmlminOptions' ] || { } ) ;
27
- }
28
-
29
- var template = ejs . compile ( source , opts ) ;
30
-
31
- // Beautify javascript code
32
- if ( ! this . minimize && opts . beautify !== false ) {
33
- var ast = UglifyJS . parse ( template . toString ( ) ) ;
34
- ast . figure_out_scope ( ) ;
35
- template = ast . print_to_string ( { beautify : true } ) ;
36
- }
11
+ // wepkack3: options
12
+ var options = ( this . hasOwnProperty ( "options" ) && ( typeof this . options [ 'ejs-compiled-loader' ] === 'object' ) ) ? this . options [ 'ejs-compiled-loader' ] : { } ;
13
+
14
+ // webpack4: query
15
+ var query = ( this . hasOwnProperty ( "query" ) ) ? ( typeof this . query === 'object' ) ? this . query : utils . parseQuery ( this . query ) : { } ;
16
+
17
+ // merge opts from defaults,opts and query
18
+ var opts = merge ( {
19
+ client : true ,
20
+ compileDebug : ! ! this . minimize ,
21
+ minimize : ( typeof this . minimize === 'boolean' ) ? this . minimize : false ,
22
+ beautify : false ,
23
+ htmlmin : ( typeof this . htmlmin === 'boolean' ) ? this . htmlmin : false ,
24
+ htmlminOptions : { }
25
+ } , options , query ) ;
26
+
27
+ // minify html
28
+ if ( opts . htmlmin ) source = htmlmin . minify ( source , opts . htmlminOptions ) ;
29
+
30
+ // compile template
31
+ var template = ejs . compile ( source , merge ( opts , {
32
+ filename : path . relative ( process . cwd ( ) , this . resourcePath ) ,
33
+ webpack : this
34
+ } ) ) . toString ( ) ;
35
+
36
+ // minify js with terser
37
+ if ( opts . minimize ) template = terser . minify ( template , { output : { beautify : opts . beautify } } ) . code ;
37
38
38
39
return 'module.exports = ' + template ;
39
- } ;
40
+
41
+ } ;
0 commit comments