@@ -27,42 +27,41 @@ enum Platform {
27
27
JAVA = 'java' ,
28
28
JAVASCRIPT = 'javascript' ,
29
29
}
30
+ const DEFAULT_PLATFORM_PRECEDENCE = [ Platform . NATIVE , Platform . JAVA , Platform . JAVASCRIPT ] ;
30
31
31
32
/**
32
- * Splits user `prefer ` option from compiler options object
33
+ * Splits user `platform ` option from compiler options object
33
34
* returns new object containing options and preferred platform.
34
35
* @param {CompileOptions } content - compiler options object
35
36
* @return {Object }
36
37
* @example in rollup.config.js
37
- * buble(),
38
38
* compiler({
39
- * prefer : 'javascript',
39
+ * platform : 'javascript',
40
40
* }),
41
41
*/
42
- function filterContent ( content : CompileOptions ) {
43
- let prefer : any = '' ;
44
- if ( 'prefer' in content ) {
45
- prefer = content [ 'prefer' ] ;
46
- delete content . prefer ;
47
- } ;
48
- const res = { config : content , prefer : prefer } ;
49
- return res ;
42
+ function filterContent ( content : CompileOptions ) : [ CompileOptions , Platform ] {
43
+ let platform : string = '' ;
44
+ if ( 'platform' in content && typeof content . platform === 'string' ) {
45
+ platform = content . platform ;
46
+ delete content . platform ;
47
+ }
48
+ return [ content , platform as Platform ] ;
50
49
}
51
50
52
51
/**
53
- * Finds prefered user platform precedence in list of defaults
54
- * and re-orders the list with prefered option first.
55
- * @param {Array } haystack - array of allowed platform strings
56
- * @param {String } needle - preferred platform string
52
+ * Reorders platform preferences based on configuration.
53
+ * @param {String } platformPreference - preferred platform string
57
54
* @return {Array }
58
55
*/
59
- function reOrder ( haystack : string [ ] , needle : string ) {
60
- const index = haystack . indexOf ( needle ) ;
61
- const precedent = haystack . splice ( index , 1 ) ;
62
- return precedent . concat ( haystack ) ;
63
- }
56
+ function orderPlatforms ( platformPreference : Platform | string ) : Array < Platform > {
57
+ if ( platformPreference === '' ) {
58
+ return DEFAULT_PLATFORM_PRECEDENCE ;
59
+ }
64
60
65
- const PLATFORM_PRECEDENCE = [ Platform . NATIVE , Platform . JAVA , Platform . JAVASCRIPT ] ;
61
+ const index = DEFAULT_PLATFORM_PRECEDENCE . indexOf ( platformPreference as Platform ) ;
62
+ const newPlatformPreferences = DEFAULT_PLATFORM_PRECEDENCE . splice ( index , 1 ) ;
63
+ return newPlatformPreferences . concat ( DEFAULT_PLATFORM_PRECEDENCE ) ;
64
+ }
66
65
67
66
/**
68
67
* Run Closure Compiler and `postCompilation` Transforms on input source.
@@ -75,19 +74,11 @@ export default function(
75
74
transforms : Array < Transform > ,
76
75
) : Promise < string > {
77
76
return new Promise ( ( resolve : ( stdOut : string ) => void , reject : ( error : any ) => void ) => {
78
-
79
- const options = filterContent ( compileOptions ) ;
80
-
81
- const { prefer, config } = options ;
82
-
83
- const USER_PLATFORM_PRECEDENCE = ( prefer !== '' ) ? reOrder ( PLATFORM_PRECEDENCE , prefer ) : PLATFORM_PRECEDENCE ;
84
-
77
+ const [ config , platform ] = filterContent ( compileOptions ) ;
85
78
const instance = new compiler ( config ) ;
86
-
87
- const firstSupportedPlatform = getFirstSupportedPlatform ( USER_PLATFORM_PRECEDENCE ) ;
79
+ const firstSupportedPlatform = getFirstSupportedPlatform ( orderPlatforms ( platform ) ) ;
88
80
89
81
if ( firstSupportedPlatform !== Platform . JAVA ) {
90
-
91
82
// TODO(KB): Provide feedback on this API. It's a little strange to nullify the JAR_PATH
92
83
// and provide a fake java path.
93
84
instance . JAR_PATH = null ;
0 commit comments