當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript Bundle.trim方法代碼示例

本文整理匯總了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();
}
開發者ID:IvanSanchez,項目名稱:rollup,代碼行數:101,代碼來源:esm.ts

示例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
//.........這裏部分代碼省略.........
開發者ID:robbie-mac,項目名稱:rollup,代碼行數:101,代碼來源:umd.ts

示例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);
//.........這裏部分代碼省略.........
開發者ID:IvanSanchez,項目名稱:rollup,代碼行數:101,代碼來源:umd.ts


注:本文中的magic-string.Bundle.trim方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。