Support dot folders and adjust regex to support sub-paths

This commit is contained in:
Valentin Palkovic 2025-03-07 15:21:23 +01:00
parent 2213e7f121
commit 0519e6083e
2 changed files with 20 additions and 2 deletions

View File

@ -180,6 +180,23 @@ describe('transformImportFiles', () => {
);
});
it('should transform import declarations with sub-paths', async () => {
const sourceContents = dedent`
import { other } from '@storybook/theming/create';
`;
const sourceFiles = [join('src', 'test.ts')];
vi.mocked(readFile).mockResolvedValueOnce(sourceContents);
const errors = await transformImportFiles(sourceFiles, false);
expect(errors).toHaveLength(0);
expect(writeFile).toHaveBeenCalledWith(
sourceFiles[0],
expect.stringContaining(`from 'storybook/internal/theming/create'`)
);
});
it('should transform require calls', async () => {
const sourceContents = dedent`
const something = require('@storybook/components');

View File

@ -45,9 +45,9 @@ function transformImports(source: string) {
for (const [from, to] of Object.entries(consolidatedPackages)) {
// Match the package name when it's inside either single or double quotes
const regex = new RegExp(`(['"])${from}\\1`, 'g');
const regex = new RegExp(`(['"])${from}(.*)\\1`, 'g');
if (regex.test(transformed)) {
transformed = transformed.replace(regex, `$1${to}$1`);
transformed = transformed.replace(regex, `$1${to}$2$1`);
hasChanges = true;
}
}
@ -180,6 +180,7 @@ export const consolidatedImports: Fix<ConsolidatedOptions> = {
const sourceFiles = await globby([glob], {
...commonGlobOptions(''),
ignore: ['**/node_modules/**'],
dot: true,
cwd: projectRoot,
});