Skip to content

Commit b03c207

Browse files
authored
fix: Fix broken imports for components with the same name imported from different folders (styleguidist#1567)
Closes styleguidist#1566
1 parent 3469cbb commit b03c207

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/client/utils/__tests__/transpileImports.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ const C = cat$0.C;
8585
`);
8686
});
8787

88+
describe.each([
89+
['./cat/capybara/hamster', '__cat_capybara_hamster'],
90+
['../cat/capybara/hamster', '___cat_capybara_hamster'],
91+
['cat/capybara/hamster', 'cat_capybara_hamster'],
92+
])('transpile default imports via relative path', (modulePath, transpiled) => {
93+
test(`${modulePath}`, () => {
94+
const result = transpileImports(`import B from '${modulePath}'`);
95+
expect(result).toMatchInlineSnapshot(`
96+
"const ${transpiled}$0 = require('${modulePath}');
97+
const B = ${transpiled}$0.default || ${transpiled}$0;"
98+
`);
99+
});
100+
});
101+
88102
test('return code if there are no imports', () => {
89103
const code = `<Button />`;
90104
const result = transpileImports(code);

src/client/utils/rewriteImports.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const UNNAMED = /import\s*['"]([^'"]+)['"];?/gi;
55
const NAMED = /import\s*(\*\s*as)?\s*(\w*?)\s*,?\s*(?:\{([\s\S]*?)\})?\s*from\s*['"]([^'"]+)['"];?/gi;
66

7-
function alias(key: string): {key: string; name: string} {
7+
function alias(key: string): { key: string; name: string } {
88
key = key.trim();
99
const name = key.split(' as ');
1010
if (name.length > 1) {
@@ -15,13 +15,7 @@ function alias(key: string): {key: string; name: string} {
1515

1616
let num: number;
1717
function generate(keys: string[], dep: string, base: string, fn: string): string {
18-
const tmp =
19-
(dep
20-
.split('/')
21-
.pop() as string)
22-
.replace(/\W/g, '_') +
23-
'$' +
24-
num++; // uniqueness
18+
const tmp = dep.replace(/\W/g, '_') + '$' + num++; // uniqueness
2519
const name = alias(tmp).name;
2620

2721
dep = `${fn}('${dep}')`;
@@ -41,7 +35,6 @@ function generate(keys: string[], dep: string, base: string, fn: string): string
4135
return out;
4236
}
4337

44-
4538
export default function(str: string, fn = 'require'): string {
4639
num = 0;
4740
return str

0 commit comments

Comments
 (0)