@@ -44,6 +44,31 @@ const toTerserLiteral = (value, name) => {
4444 // default: behaviour from Terser (@prefix=1 produces expression/literal, unprefixed=1 produces string literal):
4545} ;
4646
47+ // Normalize Terser options from microbundle's relaxed JSON format (mutates argument in-place)
48+ function normalizeMinifyOptions ( minifyOptions ) {
49+ const mangle = minifyOptions . mangle || ( minifyOptions . mangle = { } ) ;
50+ let properties = mangle . properties ;
51+
52+ // allow top-level "properties" key to override mangle.properties (including {properties:false}):
53+ if ( minifyOptions . properties != null ) {
54+ properties = mangle . properties =
55+ minifyOptions . properties &&
56+ Object . assign ( properties , minifyOptions . properties ) ;
57+ }
58+
59+ // allow previous format ({ mangle:{regex:'^_',reserved:[]} }):
60+ if ( minifyOptions . regex || minifyOptions . reserved ) {
61+ if ( ! properties ) properties = mangle . properties = { } ;
62+ properties . regex = properties . regex || minifyOptions . regex ;
63+ properties . reserved = properties . reserved || minifyOptions . reserved ;
64+ }
65+
66+ if ( properties ) {
67+ if ( properties . regex ) properties . regex = new RegExp ( properties . regex ) ;
68+ properties . reserved = [ ] . concat ( properties . reserved || [ ] ) ;
69+ }
70+ }
71+
4772// Parses values of the form "$=jQuery,React=react" into key-value object pairs.
4873const parseMappingArgument = ( globalStrings , processValue ) => {
4974 const globals = { } ;
@@ -387,7 +412,9 @@ function createConfig(options, entry, format, writeMeta) {
387412 // let rollupName = safeVariableName(basename(entry).replace(/\.js$/, ''));
388413
389414 let nameCache = { } ;
390- let mangleOptions = options . pkg . mangle || false ;
415+ const bareNameCache = nameCache ;
416+ // Support "minify" field and legacy "mangle" field via package.json:
417+ let minifyOptions = options . pkg . minify || options . pkg . mangle || { } ;
391418
392419 const useTypescript = extname ( entry ) === '.ts' || extname ( entry ) === '.tsx' ;
393420
@@ -400,10 +427,22 @@ function createConfig(options, entry, format, writeMeta) {
400427 nameCache = JSON . parse (
401428 fs . readFileSync ( resolve ( options . cwd , 'mangle.json' ) , 'utf8' ) ,
402429 ) ;
430+ // mangle.json can contain a "minify" field, same format as the pkg.mangle:
431+ if ( nameCache . minify ) {
432+ minifyOptions = Object . assign (
433+ { } ,
434+ minifyOptions || { } ,
435+ nameCache . minify ,
436+ ) ;
437+ }
403438 } catch ( e ) { }
404439 }
405440 loadNameCache ( ) ;
406441
442+ normalizeMinifyOptions ( minifyOptions ) ;
443+
444+ if ( nameCache === bareNameCache ) nameCache = null ;
445+
407446 let shebang ;
408447
409448 let config = {
@@ -539,28 +578,22 @@ function createConfig(options, entry, format, writeMeta) {
539578 terser ( {
540579 sourcemap : true ,
541580 output : { comments : false } ,
542- compress : {
543- keep_infinity : true ,
544- pure_getters : true ,
545- global_defs : defines ,
546- passes : 10 ,
547- } ,
581+ compress : Object . assign (
582+ {
583+ keep_infinity : true ,
584+ pure_getters : true ,
585+ global_defs : defines ,
586+ passes : 10 ,
587+ } ,
588+ minifyOptions . compress || { } ,
589+ ) ,
548590 warnings : true ,
549591 ecma : 5 ,
550592 toplevel : format === 'cjs' || format === 'es' ,
551- mangle : {
552- properties : mangleOptions
553- ? {
554- regex : mangleOptions . regex
555- ? new RegExp ( mangleOptions . regex )
556- : null ,
557- reserved : mangleOptions . reserved || [ ] ,
558- }
559- : false ,
560- } ,
593+ mangle : Object . assign ( { } , minifyOptions . mangle || { } ) ,
561594 nameCache,
562595 } ) ,
563- mangleOptions && {
596+ nameCache && {
564597 // before hook
565598 options : loadNameCache ,
566599 // after hook
0 commit comments