forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetGlobals.js
43 lines (35 loc) · 1005 Bytes
/
getGlobals.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
export default function getGlobals ( imports, { globals, onerror, onwarn } ) {
const globalFn = getGlobalFn( globals );
return imports.map( x => {
let name = globalFn( x.source.value );
if ( !name ) {
if ( x.name.startsWith( '__import' ) ) {
const error = new Error( `Could not determine name for imported module '${x.source.value}' – use options.globals` );
if ( onerror ) {
onerror( error );
} else {
throw error;
}
}
else {
const warning = {
message: `No name was supplied for imported module '${x.source.value}'. Guessing '${x.name}', but you should use options.globals`
};
if ( onwarn ) {
onwarn( warning );
} else {
console.warn( warning ); // eslint-disable-line no-console
}
}
name = x.name;
}
return name;
});
}
function getGlobalFn ( globals ) {
if ( typeof globals === 'function' ) return globals;
if ( typeof globals === 'object' ) {
return id => globals[ id ];
}
return () => undefined;
}