diff --git a/packages/babel-plugin/__tests__/evaluation/stylex-import-evaluation-test.js b/packages/babel-plugin/__tests__/evaluation/stylex-import-evaluation-test.js index 2685e2d6..9e23cdfc 100644 --- a/packages/babel-plugin/__tests__/evaluation/stylex-import-evaluation-test.js +++ b/packages/babel-plugin/__tests__/evaluation/stylex-import-evaluation-test.js @@ -406,9 +406,16 @@ describe('Evaluation of imported values works based on configuration', () => { }); describe('Module resolution commonJS', () => { + afterEach(() => { + moduleResolve.mockReset(); + }); + test('Recognizes .ts stylex imports when resolving .js relative imports', () => { - moduleResolve.mockReturnValue({ - pathname: '/project/otherFile.stylex.ts', + moduleResolve.mockImplementation((value) => { + if (!value.endsWith('/otherFile.stylex.ts')) { + throw new Error('File not found'); + } + return new URL('file:///project/otherFile.stylex.ts'); }); const transformation = transform( diff --git a/packages/babel-plugin/src/utils/state-manager.js b/packages/babel-plugin/src/utils/state-manager.js index b69a5152..5be6f8d6 100644 --- a/packages/babel-plugin/src/utils/state-manager.js +++ b/packages/babel-plugin/src/utils/state-manager.js @@ -657,18 +657,16 @@ function possibleAliasedPaths( return result; } +// Try importing without adding any extension +// and then every supported extension const getPossibleFilePaths = (filePath: string) => { const extension = path.extname(filePath); - const filePathWithoutSourceExtension = - extension === '' || !EXTENSIONS.includes(extension) - ? filePath - : filePath.slice(0, -extension.length); - // Try importing without adding any extension - // and then every supported extension - return [ - filePath, - ...EXTENSIONS.map((ext) => filePathWithoutSourceExtension + ext), - ]; + const filePathHasCodeExtension = EXTENSIONS.includes(extension); + const filePathNoCodeExtension = filePathHasCodeExtension + ? filePath.slice(0, -extension.length) + : filePath; + + return [filePath, ...EXTENSIONS.map((ext) => filePathNoCodeExtension + ext)]; }; // a function that resolves the absolute path of a file when given the @@ -695,7 +693,7 @@ const filePathResolver = ( try { return moduleResolve(possiblePath, url.pathToFileURL(sourceFilePath)) .pathname; - } catch (error) { + } catch { continue; } }