Skip to content

Commit 5702f22

Browse files
weltkristoferbaxter
authored andcommitted
fixes issue #158 (#162)
* stubs user platform precedence option * adds types * adds comments * reverts to type any * extends CompileOptions type * amends comments * amends CompileOptions types
1 parent 0bbdb27 commit 5702f22

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

src/compiler.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,40 @@ enum Platform {
2828
JAVASCRIPT = 'javascript',
2929
}
3030

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+
3165
const PLATFORM_PRECEDENCE = [Platform.NATIVE, Platform.JAVA, Platform.JAVASCRIPT];
3266

3367
/**
@@ -41,10 +75,18 @@ export default function(
4175
transforms: Array<Transform>,
4276
): Promise<string> {
4377
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);
4588

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) {
4890

4991
// TODO(KB): Provide feedback on this API. It's a little strange to nullify the JAR_PATH
5092
// and provide a fake java path.

0 commit comments

Comments
 (0)