@@ -2,7 +2,6 @@ import {fileExists, findPathUp, readFileSync} from '@shopify/cli-kit/node/fs'
22import { dirname , joinPath , relativizePath , resolvePath } from '@shopify/cli-kit/node/path'
33import { AbortError } from '@shopify/cli-kit/node/error'
44import ts from 'typescript'
5- import { init , parse } from 'es-module-lexer'
65import { createRequire } from 'module'
76
87const require = createRequire ( import . meta. url )
@@ -63,21 +62,43 @@ async function fallbackResolve(importPath: string, baseDir: string): Promise<str
6362
6463async function parseAndResolveImports ( filePath : string ) : Promise < string [ ] > {
6564 try {
66- await init
67-
6865 const content = readFileSync ( filePath ) . toString ( )
6966 const resolvedPaths : string [ ] = [ ]
7067
7168 // Load TypeScript configuration once
7269 const { compilerOptions} = loadTsConfig ( filePath )
7370
74- const [ imports ] = parse ( content )
71+ // Determine script kind based on file extension
72+ let scriptKind = ts . ScriptKind . JSX
73+ if ( filePath . endsWith ( '.ts' ) ) {
74+ scriptKind = ts . ScriptKind . TS
75+ } else if ( filePath . endsWith ( '.tsx' ) ) {
76+ scriptKind = ts . ScriptKind . TSX
77+ }
78+
79+ const sourceFile = ts . createSourceFile ( filePath , content , ts . ScriptTarget . Latest , true , scriptKind )
7580
7681 const processedImports = new Set < string > ( )
82+ const importPaths : string [ ] = [ ]
83+
84+ const visit = ( node : ts . Node ) : void => {
85+ if ( ts . isImportDeclaration ( node ) && node . moduleSpecifier && ts . isStringLiteral ( node . moduleSpecifier ) ) {
86+ importPaths . push ( node . moduleSpecifier . text )
87+ } else if ( ts . isCallExpression ( node ) && node . expression . kind === ts . SyntaxKind . ImportKeyword ) {
88+ const firstArg = node . arguments [ 0 ]
89+ if ( firstArg && ts . isStringLiteral ( firstArg ) ) {
90+ importPaths . push ( firstArg . text )
91+ }
92+ } else if ( ts . isExportDeclaration ( node ) && node . moduleSpecifier && ts . isStringLiteral ( node . moduleSpecifier ) ) {
93+ importPaths . push ( node . moduleSpecifier . text )
94+ }
95+
96+ ts . forEachChild ( node , visit )
97+ }
7798
78- for ( const pattern of imports ) {
79- const importPath = pattern . n
99+ visit ( sourceFile )
80100
101+ for ( const importPath of importPaths ) {
81102 // Skip if already processed
82103 if ( ! importPath || processedImports . has ( importPath ) ) {
83104 continue
@@ -149,7 +170,7 @@ export function createTypeDefinition(
149170 // Throw specific error for the target that failed, matching the original getSharedTypeDefinition behavior
150171 throw new AbortError (
151172 `Type reference for ${ target } could not be found. You might be using the wrong @shopify/ui-extensions version.` ,
152- `Fix the error by ensuring you have the correct version of @shopify/ui-extensions, for example ${ year } .${ month } .0, in your dependencies.` ,
173+ `Fix the error by ensuring you have the correct version of @shopify/ui-extensions, for example ~ ${ year } .${ month } .0, in your dependencies.` ,
153174 )
154175 }
155176 }
@@ -160,8 +181,8 @@ export function createTypeDefinition(
160181 const target = targets [ 0 ] ?? ''
161182 return `//@ts-ignore\ndeclare module './${ relativePath } ' {\n const shopify: import('@shopify/ui-extensions/${ target } ').Api;\n const globalThis: { shopify: typeof shopify };\n}\n`
162183 } else if ( targets . length > 1 ) {
163- const unionType = targets . map ( ( target ) => `import('@shopify/ui-extensions/${ target } ').Api` ) . join ( ' | ' )
164- return `//@ts-ignore\ndeclare module './${ relativePath } ' {\n const shopify: ${ unionType } ;\n const globalThis: { shopify: typeof shopify };\n}\n`
184+ const unionType = targets . map ( ( target ) => ` import('@shopify/ui-extensions/${ target } ').Api` ) . join ( ' |\n ' )
185+ return `//@ts-ignore\ndeclare module './${ relativePath } ' {\n const shopify: \n ${ unionType } ;\n const globalThis: { shopify: typeof shopify };\n}\n`
165186 }
166187
167188 return null
@@ -173,7 +194,7 @@ export function createTypeDefinition(
173194 const { year, month} = parseApiVersion ( apiVersion ) ?? { year : 2025 , month : 10 }
174195 throw new AbortError (
175196 `Type reference could not be found. You might be using the wrong @shopify/ui-extensions version.` ,
176- `Fix the error by ensuring you have the correct version of @shopify/ui-extensions, for example ${ year } .${ month } .0, in your dependencies.` ,
197+ `Fix the error by ensuring you have the correct version of @shopify/ui-extensions, for example ~ ${ year } .${ month } .0, in your dependencies.` ,
177198 )
178199 }
179200}
0 commit comments