@@ -28,6 +28,40 @@ enum Platform {
28
28
JAVASCRIPT = 'javascript' ,
29
29
}
30
30
31
+ /**
32
+ * Splits user `prefer` option from compiler options object
33
+ * returns new object containing options and preferred platform.
34
+ * @param {CompileOptions } content - compiler options object
35
+ * @return {Object }
36
+ * @example in rollup.config.js
37
+ * buble(),
38
+ * compiler({
39
+ * prefer: 'javascript',
40
+ * }),
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 ;
50
+ }
51
+
52
+ /**
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
57
+ * @return {Array }
58
+ */
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
+ }
64
+
31
65
const PLATFORM_PRECEDENCE = [ Platform . NATIVE , Platform . JAVA , Platform . JAVASCRIPT ] ;
32
66
33
67
/**
@@ -41,10 +75,18 @@ export default function(
41
75
transforms : Array < Transform > ,
42
76
) : Promise < string > {
43
77
return new Promise ( ( resolve : ( stdOut : string ) => void , reject : ( error : any ) => void ) => {
44
- const instance = new compiler ( compileOptions ) ;
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
+
85
+ const instance = new compiler ( config ) ;
86
+
87
+ const firstSupportedPlatform = getFirstSupportedPlatform ( USER_PLATFORM_PRECEDENCE ) ;
45
88
46
- if ( getFirstSupportedPlatform ( PLATFORM_PRECEDENCE ) === Platform . NATIVE ) {
47
- // We would like to use the native platform instead of Java or Javascript on this system.
89
+ if ( firstSupportedPlatform !== Platform . JAVA ) {
48
90
49
91
// TODO(KB): Provide feedback on this API. It's a little strange to nullify the JAR_PATH
50
92
// and provide a fake java path.
0 commit comments