-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
iife.js
93 lines (71 loc) · 2.95 KB
/
iife.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { blank } from '../utils/object.js';
import { getName } from '../utils/map-helpers.js';
import error from '../utils/error.js';
import getInteropBlock from './shared/getInteropBlock.js';
import getExportBlock from './shared/getExportBlock.js';
import getGlobalNameMaker from './shared/getGlobalNameMaker.js';
import { property, keypath } from './shared/sanitize.js';
import warnOnBuiltins from './shared/warnOnBuiltins.js';
import trimEmptyImports from './shared/trimEmptyImports.js';
import { isLegal } from '../utils/identifier-helpers.js';
function setupNamespace ( keypath ) {
const parts = keypath.split( '.' );
parts.pop();
let acc = 'this';
return parts
.map( part => ( acc += property( part ), `${acc} = ${acc} || {};` ) )
.join( '\n' ) + '\n';
}
const thisProp = name => `this${keypath( name )}`;
export default function iife ( bundle, magicString, { exportMode, indentString, intro, outro }, options ) {
const globalNameMaker = getGlobalNameMaker( options.globals || blank(), bundle, 'null' );
const { extend, moduleName: name } = options;
const isNamespaced = name && ~name.indexOf( '.' );
const justVariable = !extend && !isNamespaced;
if ( name && justVariable && !isLegal(name) ) {
error({
code: 'ILLEGAL_IDENTIFIER_AS_NAME',
message: `Given moduleName (${name}) is not legal JS identifier. If you need this you can try --extend option`
});
}
warnOnBuiltins( bundle );
const external = trimEmptyImports( bundle.externalModules );
const dependencies = external.map( globalNameMaker );
const args = external.map( getName );
if ( exportMode !== 'none' && !name ) {
error({
code: 'INVALID_OPTION',
message: `You must supply options.moduleName for IIFE bundles`
});
}
if ( extend ) {
dependencies.unshift( `(${thisProp(name)} = ${thisProp(name)} || {})` );
args.unshift( 'exports' );
} else if ( exportMode === 'named' ) {
dependencies.unshift( '{}' );
args.unshift( 'exports' );
}
const useStrict = options.useStrict !== false ? `${indentString}'use strict';\n\n` : ``;
let wrapperIntro = `(function (${args}) {\n${useStrict}`;
if ( exportMode !== 'none' && !extend) {
wrapperIntro = ( isNamespaced ? thisProp(name) : `${bundle.varOrConst} ${name}` ) + ` = ${wrapperIntro}`;
}
if ( isNamespaced ) {
wrapperIntro = setupNamespace( name ) + wrapperIntro;
}
let wrapperOutro = `\n\n}(${dependencies}));`;
if (justVariable && exportMode === 'named') {
wrapperOutro = `\n\n${indentString}return exports;${wrapperOutro}`;
}
// var foo__default = 'default' in foo ? foo['default'] : foo;
const interopBlock = getInteropBlock( bundle, options );
if ( interopBlock ) magicString.prepend( interopBlock + '\n\n' );
if ( intro ) magicString.prepend( intro );
const exportBlock = getExportBlock( bundle, exportMode );
if ( exportBlock ) magicString.append( '\n\n' + exportBlock );
if ( outro ) magicString.append( outro );
return magicString
.indent( indentString )
.prepend( wrapperIntro )
.append( wrapperOutro );
}