本文整理汇总了TypeScript中magic-string.Bundle类的典型用法代码示例。如果您正苦于以下问题:TypeScript Bundle类的具体用法?TypeScript Bundle怎么用?TypeScript Bundle使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Bundle类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: generate
generate(result: string, options: CompileOptions, { banner = '', sharedPath, helpers, name, format }: GenerateOptions ) {
const pattern = /\[✂(\d+)-(\d+)$/;
const module = wrapModule(result, format, name, options, banner, sharedPath, helpers, this.imports, this.source);
const parts = module.split('✂]');
const finalChunk = parts.pop();
const compiled = new Bundle({ separator: '' });
function addString(str: string) {
compiled.addSource({
content: new MagicString(str),
});
}
const { filename } = options;
// special case — the source file doesn't actually get used anywhere. we need
// to add an empty file to populate map.sources and map.sourcesContent
if (!parts.length) {
compiled.addSource({
filename,
content: new MagicString(this.source).remove(0, this.source.length),
});
}
parts.forEach((str: string) => {
const chunk = str.replace(pattern, '');
if (chunk) addString(chunk);
const match = pattern.exec(str);
const snippet = this.code.snip(+match[1], +match[2]);
compiled.addSource({
filename,
content: snippet,
});
});
addString(finalChunk);
const { css, cssMap } = this.customElement ?
{ css: null, cssMap: null } :
this.stylesheet.render(options.cssOutputFilename, true);
return {
ast: this.ast,
code: compiled.toString(),
map: compiled.generateMap({
includeContent: true,
file: options.outputFilename,
}),
css,
cssMap
};
}
示例2: getIndentString
export default function getIndentString (magicString: MagicStringBundle, options: { indent?: boolean }) {
if (options.indent === true) {
return magicString.getIndentString();
}
return options.indent || '';
}
示例3: system
export default function system(
magicString: MagicStringBundle,
{
dependencies,
exports,
indentString: t,
intro,
outro,
usesTopLevelAwait,
varOrConst
}: FinaliserOptions,
options: OutputOptions
) {
const n = options.compact ? '' : '\n';
const _ = options.compact ? '' : ' ';
const dependencyIds = dependencies.map(m => `'${m.id}'`);
const importBindings: string[] = [];
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${_}()${_}{}`
)
//.........这里部分代码省略.........
示例4: 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);
}
示例5: system
export default function system(
magicString: MagicStringBundle,
{
graph,
indentString: t,
intro,
outro,
dependencies,
exports,
usesTopLevelAwait
}: FinaliserOptions,
options: OutputOptions
) {
const n = options.compact ? '' : '\n';
const _ = options.compact ? '' : ' ';
const dependencyIds = dependencies.map(m => `'${m.id}'`);
const importBindings: string[] = [];
let starExcludes: Set<string>;
const setters: string[] = [];
const varOrConst = graph.varOrConst;
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}`));
});
// 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}${
//.........这里部分代码省略.........
示例6: 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
//.........这里部分代码省略.........
示例7: 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);
}
示例8: 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;
}
示例9:
parts.forEach((str: string) => {
const chunk = str.replace(pattern, '');
if (chunk) addString(chunk);
const match = pattern.exec(str);
const snippet = this.code.snip(+match[1], +match[2]);
compiled.addSource({
filename,
content: snippet,
});
});
示例10: addString
function addString(str: string) {
compiled.addSource({
content: new MagicString(str),
});
}