Repository: rollup/rollup
File: src/ast/nodes/ImportExpression.ts (TypeScript)

Code before:
			this.scope.addAccessedGlobals(accessedGlobals, accessedGlobalsByScope);
		}
	}

	setInternalResolution(inlineNamespace: NamespaceVariable): void {
		this.inlineNamespace = inlineNamespace;
	}

	private getDynamicImportMechanismAndHelper(
		resolution: Module | ExternalModule | string | null,
		exportMode: 'none' | 'named' | 'default' | 'external',
		{
			compact,
			dynamicImportInCjs,
			format,
			generatedCode: { arrowFunctions },
			interop
		}: NormalizedOutputOptions,
		{ _, getDirectReturnFunction, getDirectReturnIifeLeft }: GenerateCodeSnippets,
		pluginDriver: PluginDriver,
		ownChunk: Chunk,
		targetChunk: Chunk | null
	): { helper: string | null; mechanism: DynamicImportMechanism | null } {
		function getChunkInfoWithPath(chunk: Chunk): PreRenderedChunkWithFilename {
			return { preliminaryFilename: chunk.getFileName(), ...chunk.getPreRenderedChunkInfo() };
		}
		const mechanism = pluginDriver.hookFirstSync('renderDynamicImport', [
			{
				chunk: getChunkInfoWithPath(ownChunk),
				customResolution: typeof this.resolution === 'string' ? this.resolution : null,
				format,
				getTargetChunkImports(): DynamicImportTargetChunk[] | null {
					if (targetChunk === null) return null;
					const chunkInfos: DynamicImportTargetChunk[] = [];
					const importerPath = ownChunk.getFileName();
					for (const dep of targetChunk.dependencies) {
						if (dep instanceof ExternalChunk) {
							chunkInfos.push({
								filename: dep.getFileName(),
								resolvedImportPath: `'${dep.getImportPath(importerPath)}'`,
								type: 'external'
							});
						} else {
							chunkInfos.push({
								chunk: getChunkInfoWithPath(dep),
								resolvedImportPath: `'${dep.getImportPath(importerPath)}'`,
								type: 'internal'
							});
						}
					}
					return chunkInfos;

Review comment (refactor):
It looks to me like we can extract this function to the top level to avoid recreating it on every call?

Code after:
		if (helper) {
			accessedGlobals.push(helper);
		}
		if (accessedGlobals.length > 0) {
			this.scope.addAccessedGlobals(accessedGlobals, accessedGlobalsByScope);
		}
	}

	setInternalResolution(inlineNamespace: NamespaceVariable): void {
		this.inlineNamespace = inlineNamespace;
	}

	private getDynamicImportMechanismAndHelper(
		resolution: Module | ExternalModule | string | null,
		exportMode: 'none' | 'named' | 'default' | 'external',
		{
			compact,
			dynamicImportInCjs,
			format,
			generatedCode: { arrowFunctions },
			interop
		}: NormalizedOutputOptions,
		{ _, getDirectReturnFunction, getDirectReturnIifeLeft }: GenerateCodeSnippets,
		pluginDriver: PluginDriver,
		ownChunk: Chunk,
		targetChunk: Chunk | null
	): { helper: string | null; mechanism: DynamicImportMechanism | null } {
		const mechanism = pluginDriver.hookFirstSync('renderDynamicImport', [
			{
				chunk: getChunkInfoWithPath(ownChunk),
				customResolution: typeof this.resolution === 'string' ? this.resolution : null,
				format,
				getTargetChunkImports(): DynamicImportTargetChunk[] | null {
					if (targetChunk === null) return null;
					const chunkInfos: DynamicImportTargetChunk[] = [];
					const importerPath = ownChunk.getFileName();
					for (const dep of targetChunk.dependencies) {
						const resolvedImportPath = `'${dep.getImportPath(importerPath)}'`;
						if (dep instanceof ExternalChunk) {
							chunkInfos.push({
								fileName: dep.getFileName(),
								resolvedImportPath,
								type: 'external'
							});
						} else {
							chunkInfos.push({
								chunk: dep.getPreRenderedChunkInfo(),
								fileName: dep.getFileName(),
								resolvedImportPath,
								type: 'internal'
							});