本文整理汇总了TypeScript中magic-string.Bundle.trim方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Bundle.trim方法的具体用法?TypeScript Bundle.trim怎么用?TypeScript Bundle.trim使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类magic-string.Bundle
的用法示例。
在下文中一共展示了Bundle.trim方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: 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();
}
示例2: umd
export default function umd(
magicString: MagicStringBundle,
{
dependencies,
exports,
hasExports,
indentString: t,
intro,
namedExportsMode,
outro,
varOrConst,
warn
}: FinaliserOptions,
options: OutputOptions
) {
const _ = options.compact ? '' : ' ';
const n = options.compact ? '' : '\n';
const factoryVar = options.compact ? 'f' : 'factory';
const globalVar = options.compact ? 'g' : 'global';
if (hasExports && !options.name) {
error({
code: 'INVALID_OPTION',
message: 'You must supply "output.name" for UMD bundles.'
});
}
warnOnBuiltins(warn, dependencies);
const amdDeps = dependencies.map(m => `'${m.id}'`);
const cjsDeps = dependencies.map(m => `require('${m.id}')`);
const trimmedImports = trimEmptyImports(dependencies);
const globalDeps = trimmedImports.map(module => globalProp(module.globalName, globalVar));
const factoryArgs = trimmedImports.map(m => m.name);
if (namedExportsMode && (hasExports || options.noConflict === true)) {
amdDeps.unshift(`'exports'`);
cjsDeps.unshift(`exports`);
globalDeps.unshift(
assignToDeepVariable(
options.name,
globalVar,
options.globals,
options.compact,
`${options.extend ? `${globalProp(options.name, globalVar)}${_}||${_}` : ''}{}`
)
);
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
//.........这里部分代码省略.........
示例3: umd
export default function umd(
magicString: MagicStringBundle,
{
graph,
namedExportsMode,
hasExports,
indentString: t,
intro,
outro,
dependencies,
exports
}: FinaliserOptions,
options: OutputOptions
) {
const _ = options.compact ? '' : ' ';
const n = options.compact ? '' : '\n';
const wrapperOutro = n + n + '})));';
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);
//.........这里部分代码省略.........