本文整理匯總了TypeScript中magic-string.Bundle.append方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Bundle.append方法的具體用法?TypeScript Bundle.append怎麽用?TypeScript Bundle.append使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類magic-string.Bundle
的用法示例。
在下文中一共展示了Bundle.append方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: iife
export default function iife(
magicString: MagicStringBundle,
{
graph,
namedExportsMode,
hasExports,
indentString: t,
intro,
outro,
dependencies,
exports
}: FinaliserOptions,
options: OutputOptions
) {
const _ = options.compact ? '' : ' ';
const n = options.compact ? '' : '\n';
const { extend, name } = options;
const isNamespaced = name && name.indexOf('.') !== -1;
const possibleVariableAssignment = !extend && !isNamespaced;
if (name && possibleVariableAssignment && !isLegal(name)) {
error({
code: 'ILLEGAL_IDENTIFIER_AS_NAME',
message: `Given name (${name}) is not legal JS identifier. If you need this you can try --extend option`
});
}
warnOnBuiltins(graph, dependencies);
const external = trimEmptyImports(dependencies);
const deps = external.map(dep => dep.globalName || 'null');
const args = external.map(m => m.name);
if (hasExports && !name) {
error({
code: 'INVALID_OPTION',
message: `You must supply output.name for IIFE bundles`
});
}
if (extend) {
deps.unshift(`(${thisProp(name)}${_}=${_}${thisProp(name)}${_}||${_}{})`);
args.unshift('exports');
} else if (namedExportsMode && hasExports) {
deps.unshift('{}');
args.unshift('exports');
}
const useStrict = options.strict !== false ? `${t}'use strict';${n}${n}` : ``;
let wrapperIntro = `(function${_}(${args})${_}{${n}${useStrict}`;
if (hasExports && !extend) {
wrapperIntro =
(isNamespaced ? thisProp(name) : `${graph.varOrConst} ${name}`) + `${_}=${_}${wrapperIntro}`;
}
if (isNamespaced) {
wrapperIntro =
setupNamespace(name, 'this', false, options.globals, options.compact) + wrapperIntro;
}
let wrapperOutro = `${n}${n}}(${deps}));`;
if (!extend && namedExportsMode && hasExports) {
wrapperOutro = `${n}${n}${t}return exports;${wrapperOutro}`;
}
// var foo__default = 'default' in foo ? foo['default'] : foo;
const interopBlock = getInteropBlock(dependencies, options, graph.varOrConst);
if (interopBlock) magicString.prepend(interopBlock + n + n);
if (intro) magicString.prepend(intro);
const exportBlock = getExportBlock(
exports,
dependencies,
namedExportsMode,
options.interop,
options.compact
);
if (exportBlock) magicString.append(n + n + exportBlock);
if (outro) magicString.append(outro);
return magicString
.indent(t)
.prepend(wrapperIntro)
.append(wrapperOutro);
}
示例2: system
//.........這裏部分代碼省略.........
let starExcludes: Set<string>;
const setters: string[] = [];
dependencies.forEach(({ imports, reexports }) => {
const setter: string[] = [];
if (imports) {
imports.forEach(specifier => {
importBindings.push(specifier.local);
if (specifier.imported === '*') {
setter.push(`${specifier.local}${_}=${_}module;`);
} else {
setter.push(`${specifier.local}${_}=${_}module.${specifier.imported};`);
}
});
}
if (reexports) {
let createdSetter = false;
// bulk-reexport form
if (
reexports.length > 1 ||
(reexports.length === 1 &&
(reexports[0].reexported === '*' || reexports[0].imported === '*'))
) {
// star reexports
reexports.forEach(specifier => {
if (specifier.reexported !== '*') return;
// need own exports list for deduping in star export case
if (!starExcludes) {
starExcludes = getStarExcludes({ dependencies, exports });
}
if (!createdSetter) {
setter.push(`${varOrConst} _setter${_}=${_}{};`);
createdSetter = true;
}
setter.push(`for${_}(var _$p${_}in${_}module)${_}{`);
setter.push(`${t}if${_}(!_starExcludes[_$p])${_}_setter[_$p]${_}=${_}module[_$p];`);
setter.push('}');
});
// star import reexport
reexports.forEach(specifier => {
if (specifier.imported !== '*' || specifier.reexported === '*') return;
setter.push(`exports('${specifier.reexported}',${_}module);`);
});
// reexports
reexports.forEach(specifier => {
if (specifier.reexported === '*' || specifier.imported === '*') return;
if (!createdSetter) {
setter.push(`${varOrConst} _setter${_}=${_}{};`);
createdSetter = true;
}
setter.push(`_setter.${specifier.reexported}${_}=${_}module.${specifier.imported};`);
});
if (createdSetter) {
setter.push('exports(_setter);');
}
} else {
// single reexport
reexports.forEach(specifier => {
setter.push(`exports('${specifier.reexported}',${_}module.${specifier.imported});`);
});
}
}
setters.push(setter.join(`${n}${t}${t}${t}`));
});
const registeredName = options.name ? `'${options.name}',${_}` : '';
let wrapperStart =
`System.register(${registeredName}[` +
dependencyIds.join(`,${_}`) +
`],${_}function${_}(exports,${_}module)${_}{${n}${t}'use strict';` +
getStarExcludesBlock(starExcludes, varOrConst, _, t, n) +
getImportBindingsBlock(importBindings, _, t, n) +
`${n}${t}return${_}{${
setters.length
? `${n}${t}${t}setters:${_}[${setters
.map(s =>
s
? `function${_}(module)${_}{${n}${t}${t}${t}${s}${n}${t}${t}}`
: `function${_}()${_}{}`
)
.join(`,${_}`)}],`
: ''
}${n}`;
wrapperStart +=
`${t}${t}execute:${_}${usesTopLevelAwait ? `async${_}` : ''}function${_}()${_}{${n}${n}` +
getHoistedExportsBlock(exports, _, t, n);
const wrapperEnd =
`${n}${n}` +
getMissingExportsBlock(exports, _, t, n) +
`${t}${t}}${n}${t}}${options.compact ? '' : ';'}${n}});`;
if (intro) magicString.prepend(intro);
if (outro) magicString.append(outro);
return magicString
.indent(`${t}${t}${t}`)
.append(wrapperEnd)
.prepend(wrapperStart);
}
示例3: umd
//.........這裏部分代碼省略.........
factoryArgs.unshift('exports');
}
const amdOptions = options.amd || {};
const amdParams =
(amdOptions.id ? `'${amdOptions.id}',${_}` : ``) +
(amdDeps.length ? `[${amdDeps.join(`,${_}`)}],${_}` : ``);
const define = amdOptions.define || 'define';
const cjsExport = !namedExportsMode && hasExports ? `module.exports${_}=${_}` : ``;
const useStrict = options.strict !== false ? `${_}'use strict';${n}` : ``;
let iifeExport;
if (options.noConflict === true) {
const noConflictExportsVar = options.compact ? 'e' : 'exports';
let factory;
if (!namedExportsMode && hasExports) {
factory = `var ${noConflictExportsVar}${_}=${_}${assignToDeepVariable(
options.name,
globalVar,
options.globals,
options.compact,
`${factoryVar}(${globalDeps.join(`,${_}`)})`
)};`;
} else if (namedExportsMode) {
const module = globalDeps.shift();
factory =
`var ${noConflictExportsVar}${_}=${_}${module};${n}` +
`${t}${t}${factoryVar}(${[noConflictExportsVar].concat(globalDeps).join(`,${_}`)});`;
}
iifeExport =
`(function${_}()${_}{${n}` +
`${t}${t}var current${_}=${_}${safeAccess(options.name, globalVar, _)};${n}` +
`${t}${t}${factory}${n}` +
`${t}${t}${noConflictExportsVar}.noConflict${_}=${_}function${_}()${_}{${_}` +
`${globalProp(options.name, globalVar)}${_}=${_}current;${_}return ${noConflictExportsVar}${
options.compact ? '' : '; '
}};${n}` +
`${t}}())`;
} else {
iifeExport = `${factoryVar}(${globalDeps.join(`,${_}`)})`;
if (!namedExportsMode && hasExports) {
iifeExport = assignToDeepVariable(
options.name,
globalVar,
options.globals,
options.compact,
iifeExport
);
}
}
const iifeNeedsGlobal =
hasExports || (options.noConflict === true && namedExportsMode) || globalDeps.length > 0;
const globalParam = iifeNeedsGlobal ? `${globalVar},${_}` : '';
const globalArg = iifeNeedsGlobal ? `this,${_}` : '';
const iifeStart = iifeNeedsGlobal ? `(${globalVar}${_}=${_}${globalVar}${_}||${_}self,${_}` : '';
const iifeEnd = iifeNeedsGlobal ? ')' : '';
const cjsIntro = iifeNeedsGlobal
? `${t}typeof exports${_}===${_}'object'${_}&&${_}typeof module${_}!==${_}'undefined'${_}?` +
`${_}${cjsExport}${factoryVar}(${cjsDeps.join(`,${_}`)})${_}:${n}`
: '';
const wrapperIntro =
`(function${_}(${globalParam}${factoryVar})${_}{${n}` +
cjsIntro +
`${t}typeof ${define}${_}===${_}'function'${_}&&${_}${define}.amd${_}?${_}${define}(${amdParams}${factoryVar})${_}:${n}` +
`${t}${iifeStart}${iifeExport}${iifeEnd};${n}` +
`}(${globalArg}function${_}(${factoryArgs.join(', ')})${_}{${useStrict}${n}`;
const wrapperOutro = n + n + '}));';
// var foo__default = 'default' in foo ? foo['default'] : foo;
const interopBlock = getInteropBlock(dependencies, options, varOrConst);
if (interopBlock) magicString.prepend(interopBlock + n + n);
if (intro) magicString.prepend(intro);
const exportBlock = getExportBlock(
exports,
dependencies,
namedExportsMode,
options.interop,
options.compact,
t
);
if (exportBlock) magicString.append(n + n + exportBlock);
if (namedExportsMode && hasExports && options.esModule)
magicString.append(n + n + (options.compact ? compactEsModuleExport : esModuleExport));
if (outro) magicString.append(outro);
return magicString
.trim()
.indent(t)
.append(wrapperOutro)
.prepend(wrapperIntro);
}
示例4: system
//.........這裏部分代碼省略.........
}
setter.push(`for${_}(var _$p${_}in${_}module)${_}{`);
setter.push(`${t}if${_}(!_starExcludes[_$p])${_}_setter[_$p]${_}=${_}module[_$p];`);
setter.push('}');
});
// star import reexport
reexports.forEach(specifier => {
if (specifier.imported !== '*' || specifier.reexported === '*') return;
setter.push(`exports('${specifier.reexported}',${_}module);`);
});
// reexports
reexports.forEach(specifier => {
if (specifier.reexported === '*' || specifier.imported === '*') return;
if (!createdSetter) {
setter.push(`${varOrConst} _setter${_}=${_}{};`);
createdSetter = true;
}
setter.push(`_setter.${specifier.reexported}${_}=${_}module.${specifier.imported};`);
});
if (createdSetter) {
setter.push('exports(_setter);');
}
} else {
// single reexport
reexports.forEach(specifier => {
setter.push(`exports('${specifier.reexported}',${_}module.${specifier.imported});`);
});
}
}
setters.push(setter.join(`${n}${t}${t}${t}`));
});
// function declarations hoist
const hoistedExports: string[] = [];
const hoistedExportObjs = exports.filter(expt => expt.hoisted || expt.uninitialized);
if (hoistedExportObjs.length === 1) {
const expt = hoistedExportObjs[0];
hoistedExports.push(
`exports('${expt.exported}',${_}${expt.uninitialized ? 'void 0' : expt.local});`
);
} else if (hoistedExportObjs.length > 1) {
hoistedExports.push(`exports({`);
for (let i = 0; i < hoistedExportObjs.length; i++) {
const expt = hoistedExportObjs[i];
hoistedExports.push(
`${t}${expt.exported}:${_}${expt.uninitialized ? 'void 0' : expt.local}${
i === hoistedExportObjs.length - 1 ? '' : ','
}`
);
}
hoistedExports.push(`});`);
}
const starExcludesSection = !starExcludes
? ''
: `${n}${t}${varOrConst} _starExcludes${_}=${_}{${_}${Array.from(starExcludes).join(
`:${_}1,${_}`
)}${starExcludes.size ? `:${_}1` : ''}${_}};`;
const importBindingsSection = importBindings.length
? `${n}${t}var ${importBindings.join(`,${_}`)};`
: '';
const registeredName = options.name ? `'${options.name}',${_}` : '';
let wrapperStart = `System.register(${registeredName}[${dependencyIds.join(
`,${_}`
)}],${_}function${_}(exports,${_}module)${_}{${n}`;
wrapperStart += `${t}'use strict';${starExcludesSection}${importBindingsSection}${n}`;
wrapperStart += `${t}return${_}{${
setters.length
? `${n}${t}${t}setters:${_}[${setters
.map(
s =>
s
? `function${_}(module)${_}{${n}${t}${t}${t}${s}${n}${t}${t}}`
: `function${_}()${_}{}`
)
.join(`,${_}`)}],`
: ''
}${n}`;
wrapperStart += `${t}${t}execute:${_}${
usesTopLevelAwait ? `async${_}` : ''
}function${_}()${_}{${n}${n}`;
if (hoistedExports.length)
wrapperStart += `${t}${t}${t}` + hoistedExports.join(`${n}${t}${t}${t}`) + n + n;
let wrapperEnd = `${n}${n}${t}${t}}`;
wrapperEnd += `${n}${t}}${options.compact ? '' : ';'}`;
wrapperEnd += `${n}});`;
if (intro) magicString.prepend(intro);
if (outro) magicString.append(outro);
return magicString
.indent(`${t}${t}${t}`)
.append(wrapperEnd)
.prepend(wrapperStart);
}
示例5: amd
export default function amd(
magicString: MagicStringBundle,
{
graph,
namedExportsMode,
hasExports,
indentString,
intro,
outro,
dynamicImport,
needsAmdModule,
dependencies,
exports,
isEntryModuleFacade
}: FinaliserOptions,
options: OutputOptions
) {
warnOnBuiltins(graph, dependencies);
const deps = dependencies.map(m => `'${m.id}'`);
const args = dependencies.map(m => m.name);
const n = options.compact ? '' : '\n';
const _ = options.compact ? '' : ' ';
if (namedExportsMode && hasExports) {
args.unshift(`exports`);
deps.unshift(`'exports'`);
}
if (dynamicImport) {
args.unshift('require');
deps.unshift(`'require'`);
}
if (needsAmdModule) {
args.unshift('module');
deps.unshift(`'module'`);
}
const amdOptions = options.amd || {};
const params =
(amdOptions.id ? `'${amdOptions.id}',${_}` : ``) +
(deps.length ? `[${deps.join(`,${_}`)}],${_}` : ``);
const useStrict = options.strict !== false ? `${_}'use strict';` : ``;
const define = amdOptions.define || 'define';
const wrapperStart = `${define}(${params}function${_}(${args.join(
`,${_}`
)})${_}{${useStrict}${n}${n}`;
// var foo__default = 'default' in foo ? foo['default'] : foo;
const interopBlock = getInteropBlock(dependencies, options, graph.varOrConst);
if (interopBlock) magicString.prepend(interopBlock + n + n);
if (intro) magicString.prepend(intro);
const exportBlock = getExportBlock(
exports,
dependencies,
namedExportsMode,
options.interop,
options.compact
);
if (exportBlock) magicString.append(n + n + exportBlock);
if (namedExportsMode && hasExports && isEntryModuleFacade && options.esModule)
magicString.append(`${n}${n}${options.compact ? compactEsModuleExport : esModuleExport}`);
if (outro) magicString.append(outro);
return magicString
.indent(indentString)
.append(n + n + '});')
.prepend(wrapperStart);
}
示例6: cjs
export default function cjs(
magicString: MagicStringBundle,
{
dependencies,
exports,
hasExports,
indentString: t,
intro,
isEntryModuleFacade,
namedExportsMode,
outro,
varOrConst
}: FinaliserOptions,
options: OutputOptions
) {
const n = options.compact ? '' : '\n';
const _ = options.compact ? '' : ' ';
intro =
(options.strict === false ? intro : `'use strict';${n}${n}${intro}`) +
(namedExportsMode && hasExports && isEntryModuleFacade && options.esModule
? `${options.compact ? compactEsModuleExport : esModuleExport}${n}${n}`
: '');
let needsInterop = false;
const interop = options.interop !== false;
let importBlock: string;
let definingVariable = false;
importBlock = '';
for (const {
id,
namedExportsMode,
isChunk,
name,
reexports,
imports,
exportsNames,
exportsDefault
} of dependencies) {
if (!reexports && !imports) {
if (importBlock) {
importBlock += !options.compact || definingVariable ? `;${n}` : ',';
}
definingVariable = false;
importBlock += `require('${id}')`;
} else {
importBlock +=
options.compact && definingVariable ? ',' : `${importBlock ? `;${n}` : ''}${varOrConst} `;
definingVariable = true;
if (!interop || isChunk || !exportsDefault || !namedExportsMode) {
importBlock += `${name}${_}=${_}require('${id}')`;
} else {
needsInterop = true;
if (exportsNames)
importBlock += `${name}${_}=${_}require('${id}')${
options.compact ? ',' : `;\n${varOrConst} `
}${name}__default${_}=${_}${INTEROP_DEFAULT_VARIABLE}(${name})`;
else importBlock += `${name}${_}=${_}${INTEROP_DEFAULT_VARIABLE}(require('${id}'))`;
}
}
}
if (importBlock) importBlock += ';';
if (needsInterop) {
const ex = options.compact ? 'e' : 'ex';
intro +=
`function ${INTEROP_DEFAULT_VARIABLE}${_}(${ex})${_}{${_}return${_}` +
`(${ex}${_}&&${_}(typeof ${ex}${_}===${_}'object')${_}&&${_}'default'${_}in ${ex})${_}` +
`?${_}${ex}['default']${_}:${_}${ex}${options.compact ? '' : '; '}}${n}${n}`;
}
if (importBlock) intro += importBlock + n + n;
const exportBlock = getExportBlock(
exports,
dependencies,
namedExportsMode,
options.interop,
options.compact,
t,
`module.exports${_}=${_}`
);
magicString.prepend(intro);
if (exportBlock) magicString.append(n + n + exportBlock);
if (outro) magicString.append(outro);
return magicString;
}
示例7: esm
//.........這裏部分代碼省略.........
const n = options.compact ? '' : '\n';
const importBlock = dependencies
.map(({ id, reexports, imports, name }) => {
if (!reexports && !imports) {
return `import${_}'${id}';`;
}
let output = '';
if (imports) {
const defaultImport = imports.find(specifier => specifier.imported === 'default');
const starImport = imports.find(specifier => specifier.imported === '*');
if (starImport) {
output += `import${_}*${_}as ${starImport.local} from${_}'${id}';`;
if (imports.length > 1) output += n;
}
if (defaultImport && imports.length === 1) {
output += `import ${defaultImport.local} from${_}'${id}';`;
} else if (!starImport || imports.length > 1) {
output += `import ${defaultImport ? `${defaultImport.local},${_}` : ''}{${_}${imports
.filter(specifier => specifier !== defaultImport && specifier !== starImport)
.map(specifier => {
if (specifier.imported === specifier.local) {
return specifier.imported;
} else {
return `${specifier.imported} as ${specifier.local}`;
}
})
.join(`,${_}`)}${_}}${_}from${_}'${id}';`;
}
}
if (reexports) {
if (imports) output += n;
const starExport = reexports.find(specifier => specifier.reexported === '*');
const namespaceReexport = reexports.find(
specifier => specifier.imported === '*' && specifier.reexported !== '*'
);
if (starExport) {
output += `export${_}*${_}from${_}'${id}';`;
if (reexports.length === 1) {
return output;
}
output += n;
}
if (namespaceReexport) {
if (
!imports ||
!imports.some(specifier => specifier.imported === '*' && specifier.local === name)
)
output += `import${_}*${_}as ${name} from${_}'${id}';${n}`;
output += `export${_}{${_}${
name === namespaceReexport.reexported
? name
: `${name} as ${namespaceReexport.reexported}`
} };`;
if (reexports.length === (starExport ? 2 : 1)) {
return output;
}
output += n;
}
output += `export${_}{${_}${reexports
.filter(specifier => specifier !== starExport && specifier !== namespaceReexport)
.map(specifier => {
if (specifier.imported === specifier.reexported) {
return specifier.imported;
} else {
return `${specifier.imported} as ${specifier.reexported}`;
}
})
.join(`,${_}`)}${_}}${_}from${_}'${id}';`;
}
return output;
})
.join(n);
if (importBlock) intro += importBlock + n + n;
if (intro) magicString.prepend(intro);
const exportBlock: string[] = [];
const exportDeclaration: string[] = [];
exports.forEach(specifier => {
if (specifier.exported === 'default') {
exportBlock.push(`export default ${specifier.local};`);
} else {
exportDeclaration.push(
specifier.exported === specifier.local
? specifier.local
: `${specifier.local} as ${specifier.exported}`
);
}
});
if (exportDeclaration.length) {
exportBlock.push(`export${_}{${_}${exportDeclaration.join(`,${_}`)}${_}};`);
}
if (exportBlock.length) magicString.append(n + n + exportBlock.join(n).trim());
if (outro) magicString.append(outro);
return magicString.trim();
}
示例8: umd
//.........這裏部分代碼省略.........
if (hasExports && !options.name) {
error({
code: 'INVALID_OPTION',
message: 'You must supply output.name for UMD bundles'
});
}
warnOnBuiltins(graph, dependencies);
const amdDeps = dependencies.map(m => `'${m.id}'`);
const cjsDeps = dependencies.map(m => `require('${m.id}')`);
const trimmed = trimEmptyImports(dependencies);
const globalDeps = trimmed.map(module => globalProp(module.globalName));
const args = trimmed.map(m => m.name);
if (namedExportsMode && hasExports) {
amdDeps.unshift(`'exports'`);
cjsDeps.unshift(`exports`);
globalDeps.unshift(
`(${setupNamespace(options.name, 'global', true, options.globals, options.compact)}${_}=${_}${
options.extend ? `${globalProp(options.name)}${_}||${_}` : ''
}{})`
);
args.unshift('exports');
}
const amdOptions = options.amd || {};
const amdParams =
(amdOptions.id ? `'${amdOptions.id}',${_}` : ``) +
(amdDeps.length ? `[${amdDeps.join(`,${_}`)}],${_}` : ``);
const define = amdOptions.define || 'define';
const cjsExport = !namedExportsMode && hasExports ? `module.exports${_}=${_}` : ``;
const defaultExport =
!namedExportsMode && hasExports
? `${setupNamespace(options.name, 'global', true, options.globals, options.compact)}${_}=${_}`
: '';
const useStrict = options.strict !== false ? `${_}'use strict';${n}` : ``;
let globalExport;
if (options.noConflict === true) {
let factory;
if (!namedExportsMode && hasExports) {
factory = `var exports${_}=${_}factory(${globalDeps});`;
} else if (namedExportsMode) {
const module = globalDeps.shift();
factory = `var exports${_}=${_}${module};${n}`;
factory += `${t}${t}factory(${['exports'].concat(globalDeps)});`;
}
globalExport = `(function()${_}{${n}`;
globalExport += `${t}${t}var current${_}=${_}${safeAccess(options.name, options.compact)};${n}`;
globalExport += `${t}${t}${factory}${n}`;
globalExport += `${t}${t}${globalProp(options.name)}${_}=${_}exports;${n}`;
globalExport += `${t}${t}exports.noConflict${_}=${_}function()${_}{${_}`;
globalExport += `${globalProp(options.name)}${_}=${_}current;${_}return exports${
options.compact ? '' : '; '
}};${n}`;
globalExport += `${t}})()`;
} else {
globalExport = `(${defaultExport}factory(${globalDeps}))`;
}
let wrapperIntro = `(function${_}(global,${_}factory)${_}{${n}`;
wrapperIntro += `${t}typeof exports${_}===${_}'object'${_}&&${_}typeof module${_}!==${_}'undefined'${_}?`;
wrapperIntro += `${_}${cjsExport}factory(${cjsDeps.join(`,${_}`)})${_}:${n}`;
wrapperIntro += `${t}typeof ${define}${_}===${_}'function'${_}&&${_}${define}.amd${_}?${_}${define}(${amdParams}factory)${_}:${n}`;
wrapperIntro += `${t}${globalExport};${n}`;
wrapperIntro += `}(this,${_}(function${_}(${args})${_}{${useStrict}${n}`;
// var foo__default = 'default' in foo ? foo['default'] : foo;
const interopBlock = getInteropBlock(dependencies, options, graph.varOrConst);
if (interopBlock) magicString.prepend(interopBlock + n + n);
if (intro) magicString.prepend(intro);
const exportBlock = getExportBlock(
exports,
dependencies,
namedExportsMode,
options.interop,
options.compact
);
if (exportBlock) magicString.append(n + n + exportBlock);
if (namedExportsMode && hasExports && options.esModule)
magicString.append(n + n + (options.compact ? compactEsModuleExport : esModuleExport));
if (outro) magicString.append(outro);
return magicString
.trim()
.indent(t)
.append(wrapperOutro)
.prepend(wrapperIntro);
}
示例9: cjs
//.........這裏部分代碼省略.........
let needsInterop = false;
const varOrConst = graph.varOrConst;
const interop = options.interop !== false;
let importBlock: string;
if (options.compact) {
let definingVariable = false;
importBlock = '';
dependencies.forEach(
({
id,
namedExportsMode,
isChunk,
name,
reexports,
imports,
exportsNames,
exportsDefault
}) => {
if (!reexports && !imports) {
importBlock += definingVariable ? ';' : ',';
definingVariable = false;
importBlock += `require('${id}')`;
} else {
importBlock += definingVariable ? ',' : `${importBlock ? ';' : ''}${varOrConst} `;
definingVariable = true;
if (!interop || isChunk || !exportsDefault || !namedExportsMode) {
importBlock += `${name}=require('${id}')`;
} else {
needsInterop = true;
if (exportsNames)
importBlock += `${name}=require('${id}'),${name}__default=_interopDefault(${name})`;
else importBlock += `${name}=_interopDefault(require('${id}'))`;
}
}
}
);
if (importBlock.length) importBlock += ';';
} else {
importBlock = dependencies
.map(
({
id,
namedExportsMode,
isChunk,
name,
reexports,
imports,
exportsNames,
exportsDefault
}) => {
if (!reexports && !imports) return `require('${id}');`;
if (!interop || isChunk || !exportsDefault || !namedExportsMode)
return `${varOrConst} ${name} = require('${id}');`;
needsInterop = true;
if (exportsNames)
return (
`${varOrConst} ${name} = require('${id}');` +
`\n${varOrConst} ${name}__default = _interopDefault(${name});`
);
return `${varOrConst} ${name} = _interopDefault(require('${id}'));`;
}
)
.join('\n');
}
if (needsInterop) {
if (options.compact)
intro += `function _interopDefault(e){return(e&&(typeof e==='object')&&'default'in e)?e['default']:e}`;
else
intro += `function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }\n\n`;
}
if (importBlock) intro += importBlock + n + n;
const exportBlock = getExportBlock(
exports,
dependencies,
namedExportsMode,
options.interop,
options.compact,
`module.exports${_}=${_}`
);
magicString.prepend(intro);
if (exportBlock) magicString.append(n + n + exportBlock);
if (outro) magicString.append(outro);
return magicString;
}