UNPKG

1.18 kBJavaScriptView Raw
1import { keys } from './object.js';
2
3function badExports ( option, keys ) {
4 throw new Error( `'${option}' was specified for options.exports, but entry module has following exports: ${keys.join(', ')}` );
5}
6
7export default function getExportMode ( bundle, exportMode ) {
8 const exportKeys = keys( bundle.entryModule.exports )
9 .concat( keys( bundle.entryModule.reexports ) )
10 .concat( bundle.entryModule.exportAllSources ); // not keys, but makes our job easier this way
11
12 if ( exportMode === 'default' ) {
13 if ( exportKeys.length !== 1 || exportKeys[0] !== 'default' ) {
14 badExports( 'default', exportKeys );
15 }
16 } else if ( exportMode === 'none' && exportKeys.length ) {
17 badExports( 'none', exportKeys );
18 }
19
20 if ( !exportMode || exportMode === 'auto' ) {
21 if ( exportKeys.length === 0 ) {
22 exportMode = 'none';
23 } else if ( exportKeys.length === 1 && exportKeys[0] === 'default' ) {
24 exportMode = 'default';
25 } else {
26 exportMode = 'named';
27 }
28 }
29
30 if ( !/(?:default|named|none)/.test( exportMode ) ) {
31 throw new Error( `options.exports must be 'default', 'named', 'none', 'auto', or left unspecified (defaults to 'auto')` );
32 }
33
34 return exportMode;
35}