diff --git a/editor/src/core/shared/import-shared-utils.ts b/editor/src/core/shared/import-shared-utils.ts index d94912b4dd8d..b967704fa00b 100644 --- a/editor/src/core/shared/import-shared-utils.ts +++ b/editor/src/core/shared/import-shared-utils.ts @@ -4,6 +4,7 @@ import { importAlias, importDetails } from './project-file-types' import { absolutePathFromRelativePath } from '../../utils/path-utils' import { stripExtension } from '../../components/custom-code/custom-code-utils' import type { FilePathMappings } from '../model/project-file-utils' +import { applyFilePathMappingsToFilePath } from '../workers/common/project-file-utils' export function renameDuplicateImports( existingImports: Imports, @@ -13,7 +14,8 @@ export function renameDuplicateImports( ): ImportsMergeResolution { function absolutePath(relativePath: string): string { const rawAbsolutePath = absolutePathFromRelativePath(targetFilePath, false, relativePath) - const absoluteImportSource = stripExtension(rawAbsolutePath) + const afterFilePathMapping = applyFilePathMappingsToFilePath(rawAbsolutePath, filePathMappings) + const absoluteImportSource = stripExtension(afterFilePathMapping) return absoluteImportSource } const existingNames = getAllImportsUniqueNames(existingImports) diff --git a/editor/src/core/workers/common/project-file-utils.spec.ts b/editor/src/core/workers/common/project-file-utils.spec.ts index 4059b5f9fb56..f34bbf262f25 100644 --- a/editor/src/core/workers/common/project-file-utils.spec.ts +++ b/editor/src/core/workers/common/project-file-utils.spec.ts @@ -84,6 +84,25 @@ describe('mergeImports', () => { }) }) + it('does not duplicate same import from aliased sources', () => { + const result = mergeImports( + '/src/code.js', + [[/@h2\/([^/]*)/y, ['/app/components/hydrogen/$1']]], + { '@h2/ProductCard': importDetails(null, [importAlias('Card')], null) }, + { + '/app/components/hydrogen/ProductCard.jsx': importDetails( + null, + [importAlias('Card')], + null, + ), + }, + ) + expect(result.imports).toEqual({ + '@h2/ProductCard': importDetails(null, [importAlias('Card')], null), + }) + expect(result.duplicateNameMapping).toEqual(new Map()) + }) + it('handles duplicate imports', () => { const result = mergeImports( '/src/code.js', diff --git a/editor/src/core/workers/common/project-file-utils.ts b/editor/src/core/workers/common/project-file-utils.ts index 9e787098535b..ddc685e50c6a 100644 --- a/editor/src/core/workers/common/project-file-utils.ts +++ b/editor/src/core/workers/common/project-file-utils.ts @@ -132,7 +132,8 @@ export function mergeImports( allImportSources.forEach((importSource) => { const rawAbsolutePath = absolutePathFromRelativePath(fileUri, false, importSource) - const absoluteImportSource = stripExtension(rawAbsolutePath) + const unaliasedPath = applyFilePathMappingsToFilePath(rawAbsolutePath, filePathMappings) + const absoluteImportSource = stripExtension(unaliasedPath) if (fileUriWithoutExtension === absoluteImportSource) { // Prevent accidentally importing the current file return