@@ -26,12 +26,40 @@ import camelCase from 'camelcase';
2626
2727const removeScope = name => name . replace ( / ^ @ .* \/ / , '' ) ;
2828
29+ // Convert booleans and int define= values to literals.
30+ // This is more intuitive than `microbundle --define A=1` producing A="1".
31+ // See: https://github.com/terser-js/terser#conditional-compilation-api
32+ const toTerserLiteral = ( value , name ) => {
33+ // --define A="1",B='true' produces string:
34+ const matches = value . match ( / ^ ( [ ' " ] ) ( .+ ) \1$ / ) ;
35+ if ( matches ) {
36+ return [ matches [ 2 ] , name ] ;
37+ }
38+
39+ // --define A=1,B=true produces int/boolean literal:
40+ if ( / ^ ( t r u e | f a l s e | \d + ) $ / i. test ( value ) ) {
41+ return [ value , '@' + name ] ;
42+ }
43+
44+ // default: behaviour from Terser (@prefix=1 produces expression/literal, unprefixed=1 produces string literal):
45+ } ;
46+
2947// Parses values of the form "$=jQuery,React=react" into key-value object pairs.
30- const parseMappingArgument = globalStrings => {
48+ const parseMappingArgument = ( globalStrings , processValue ) => {
3149 const globals = { } ;
3250 globalStrings . split ( ',' ) . forEach ( globalString => {
33- const [ localName , globalName ] = globalString . split ( '=' ) ;
34- globals [ localName ] = globalName ;
51+ let [ key , value ] = globalString . split ( '=' ) ;
52+ if ( processValue ) {
53+ const r = processValue ( value , key ) ;
54+ if ( r !== undefined ) {
55+ if ( Array . isArray ( r ) ) {
56+ [ value , key ] = r ;
57+ } else {
58+ value = r ;
59+ }
60+ }
61+ }
62+ globals [ key ] = value ;
3563 } ) ;
3664 return globals ;
3765} ;
@@ -325,7 +353,10 @@ function createConfig(options, entry, format, writeMeta) {
325353
326354 let defines = { } ;
327355 if ( options . define ) {
328- defines = Object . assign ( defines , parseMappingArgument ( options . define ) ) ;
356+ defines = Object . assign (
357+ defines ,
358+ parseMappingArgument ( options . define , toTerserLiteral ) ,
359+ ) ;
329360 }
330361
331362 function replaceName ( filename , name ) {
0 commit comments