fix(@stylexjs/babel-plugin): Support .js resolved file extension imports from .ts files #839
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What changed / motivation ?
In a TypeScript project using
moduleResolution: 'node16' | 'nodenext'
, relative imports must include the file extension because of the Node.js ESM resolver requires file extensions.Consider the following:
The babel plugin currently attempts to verify whether these relative import files actually exist. It does this by trying to resolve the import string relative to the source file. First by checking the import string itself, then checking each of the valid file extensions in turn (
js,ts,tsx,jsx,mjs,cjs
).The case above is rejected because it will attempt to resolve e.g.
../vars.stylex.js
,../vars.stylex.js.ts
but not../vars.stylex.ts
. This results in aOnly static values are allowed inside of a stylex.create() call
compile error.This change first checks whether the file extension in the relative import string is one of the recognized extensions, and if so, will remove the extension before attempting to resolve the valid extensions.
Linked PR/Issues
Related to #769, which has a similar issue but with the ESLint plugin instead. I'm happy to take a look at the ESLint plugin if this change is accepted.
Additional Context
I've added a test for this, but not sure it matches the existing code style because it makes use of
jest.mock(..)
for@dual-bundle/import-meta-resolve
. I don't love this, but I could not find any other test examples formoduleResolution: 'commonJS'
.Pre-flight checklist
Contribution Guidelines