Skip to content

Commit 06ffe7e

Browse files
fix(@stylexjs/babel-plugin): Support .js resolved file extension imports from .ts files (#839)
* Try resolving all file extensions on the base file name instead of the import string
1 parent ecba220 commit 06ffe7e

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

packages/babel-plugin/__tests__/evaluation/stylex-import-evaluation-test.js

+43
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
'use strict';
1111

1212
jest.autoMockOff();
13+
jest.mock('@dual-bundle/import-meta-resolve');
1314

1415
/* eslint-disable quotes */
1516
const { transformSync } = require('@babel/core');
1617
const stylexPlugin = require('../../src/index');
1718
const jsx = require('@babel/plugin-syntax-jsx');
1819
const { utils } = require('@stylexjs/shared');
20+
const { moduleResolve } = require('@dual-bundle/import-meta-resolve');
1921

2022
const hash = utils.hash;
2123

@@ -402,4 +404,45 @@ describe('Evaluation of imported values works based on configuration', () => {
402404
`);
403405
});
404406
});
407+
408+
describe('Module resolution commonJS', () => {
409+
afterEach(() => {
410+
moduleResolve.mockReset();
411+
});
412+
413+
test('Recognizes .ts stylex imports when resolving .js relative imports', () => {
414+
moduleResolve.mockImplementation((value) => {
415+
if (!value.endsWith('/otherFile.stylex.ts')) {
416+
throw new Error('File not found');
417+
}
418+
return new URL('file:///project/otherFile.stylex.ts');
419+
});
420+
421+
const transformation = transform(
422+
`
423+
import stylex from 'stylex';
424+
import { MyTheme } from './otherFile.stylex.js';
425+
const styles = stylex.create({
426+
red: {
427+
color: MyTheme.__themeName__,
428+
}
429+
});
430+
stylex(styles.red);
431+
`,
432+
{
433+
unstable_moduleResolution: { type: 'commonJS' },
434+
},
435+
);
436+
437+
expect(transformation.code).toMatchInlineSnapshot(`
438+
"import _inject from "@stylexjs/stylex/lib/stylex-inject";
439+
var _inject2 = _inject;
440+
import stylex from 'stylex';
441+
import './otherFile.stylex.js';
442+
import { MyTheme } from './otherFile.stylex.js';
443+
_inject2(".xoh8dld{color:xb897f7}", 3000);
444+
"xoh8dld";"
445+
`);
446+
});
447+
});
405448
});

packages/babel-plugin/src/utils/state-manager.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -657,18 +657,26 @@ function possibleAliasedPaths(
657657
return result;
658658
}
659659

660+
// Try importing without adding any extension
661+
// and then every supported extension
662+
const getPossibleFilePaths = (filePath: string) => {
663+
const extension = path.extname(filePath);
664+
const filePathHasCodeExtension = EXTENSIONS.includes(extension);
665+
const filePathNoCodeExtension = filePathHasCodeExtension
666+
? filePath.slice(0, -extension.length)
667+
: filePath;
668+
669+
return [filePath, ...EXTENSIONS.map((ext) => filePathNoCodeExtension + ext)];
670+
};
671+
660672
// a function that resolves the absolute path of a file when given the
661673
// relative path of the file from the source file
662674
const filePathResolver = (
663675
relativeFilePath: string,
664676
sourceFilePath: string,
665677
aliases: StyleXStateOptions['aliases'],
666678
): ?string => {
667-
// Try importing without adding any extension
668-
// and then every supported extension
669-
for (const ext of ['', ...EXTENSIONS]) {
670-
const importPathStr = relativeFilePath + ext;
671-
679+
for (const importPathStr of getPossibleFilePaths(relativeFilePath)) {
672680
// Try to resolve relative paths as is
673681
if (importPathStr.startsWith('.')) {
674682
try {
@@ -712,13 +720,9 @@ const addFileExtension = (
712720
};
713721

714722
const matchesFileSuffix = (allowedSuffix: string) => (filename: string) =>
715-
filename.endsWith(`${allowedSuffix}.js`) ||
716-
filename.endsWith(`${allowedSuffix}.ts`) ||
717-
filename.endsWith(`${allowedSuffix}.tsx`) ||
718-
filename.endsWith(`${allowedSuffix}.jsx`) ||
719-
filename.endsWith(`${allowedSuffix}.mjs`) ||
720-
filename.endsWith(`${allowedSuffix}.cjs`) ||
721-
filename.endsWith(allowedSuffix);
723+
['', ...EXTENSIONS].some((extension) =>
724+
filename.endsWith(`${allowedSuffix}${extension}`),
725+
);
722726

723727
const getProgramPath = (path: NodePath<>): null | NodePath<t.Program> => {
724728
let programPath = path;

0 commit comments

Comments
 (0)