You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte/compiler/generate/utils/getGlobals.js

44 lines
1005 B

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;
}