Skip to content

Commit ff946bc

Browse files
fix(exports): correctly generate export paths for nested index files (#559)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent c67935d commit ff946bc

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

src/features/exports.test.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,89 @@ describe.concurrent('generateExports', () => {
317317
}
318318
`)
319319
})
320+
321+
test('windows-like paths for subpackages', async ({ expect }) => {
322+
const results = generateExports(
323+
FAKE_PACKAGE_JSON,
324+
cwd,
325+
{
326+
es: [
327+
genChunk('index.js'),
328+
genChunk('index.d.ts'),
329+
genChunk(String.raw`foo\index.js`),
330+
genChunk(String.raw`foo\index.d.ts`),
331+
genChunk(String.raw`bar\baz.js`),
332+
genChunk(String.raw`bar\baz.d.ts`),
333+
],
334+
},
335+
{},
336+
)
337+
await expect(results).resolves.toMatchInlineSnapshot(`
338+
{
339+
"exports": {
340+
".": "./index.js",
341+
"./bar/baz": "./bar/baz.js",
342+
"./foo": "./foo/index.js",
343+
"./package.json": "./package.json",
344+
},
345+
"main": "./index.js",
346+
"module": "./index.js",
347+
"publishExports": undefined,
348+
"types": "./index.d.ts",
349+
}
350+
`)
351+
})
352+
353+
test('windows-like paths for subpackages with dual format', async ({
354+
expect,
355+
}) => {
356+
const results = generateExports(
357+
FAKE_PACKAGE_JSON,
358+
cwd,
359+
{
360+
es: [
361+
genChunk('index.js'),
362+
genChunk('index.d.ts'),
363+
genChunk(String.raw`foo\index.js`),
364+
genChunk(String.raw`foo\index.d.ts`),
365+
genChunk(String.raw`bar\baz.js`),
366+
genChunk(String.raw`bar\baz.d.ts`),
367+
],
368+
cjs: [
369+
genChunk('index.cjs'),
370+
genChunk('index.d.cts'),
371+
genChunk(String.raw`foo\index.cjs`),
372+
genChunk(String.raw`foo\index.d.cts`),
373+
genChunk(String.raw`bar\baz.cjs`),
374+
genChunk(String.raw`bar\baz.d.cts`),
375+
],
376+
},
377+
{},
378+
)
379+
await expect(results).resolves.toMatchInlineSnapshot(`
380+
{
381+
"exports": {
382+
".": {
383+
"import": "./index.js",
384+
"require": "./index.cjs",
385+
},
386+
"./bar/baz": {
387+
"import": "./bar/baz.js",
388+
"require": "./bar/baz.cjs",
389+
},
390+
"./foo": {
391+
"import": "./foo/index.js",
392+
"require": "./foo/index.cjs",
393+
},
394+
"./package.json": "./package.json",
395+
},
396+
"main": "./index.cjs",
397+
"module": "./index.js",
398+
"publishExports": undefined,
399+
"types": "./index.d.cts",
400+
}
401+
`)
402+
})
320403
})
321404

322405
function genChunk(fileName: string) {

src/features/exports.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,16 @@ export async function generateExports(
116116
for (const chunk of chunksByFormat) {
117117
if (chunk.type !== 'chunk' || !chunk.isEntry) continue
118118

119+
const normalizedName = slash(chunk.fileName)
119120
const ext = path.extname(chunk.fileName)
120-
let name = chunk.fileName.slice(0, -ext.length)
121+
let name = normalizedName.slice(0, -ext.length)
121122

122123
const isDts = name.endsWith('.d')
123124
if (isDts) {
124125
name = name.slice(0, -2)
125126
}
126127
const isIndex = onlyOneEntry || name === 'index'
127-
const distFile = `${outDirRelative ? `./${outDirRelative}` : '.'}/${chunk.fileName}`
128+
const distFile = `${outDirRelative ? `./${outDirRelative}` : '.'}/${normalizedName}`
128129

129130
if (isIndex) {
130131
name = '.'
@@ -141,9 +142,10 @@ export async function generateExports(
141142
module = distFile
142143
}
143144
}
145+
} else if (name.endsWith('/index')) {
146+
name = `./${name.slice(0, -6)}`
144147
} else {
145-
const isDirIndex = name.endsWith('/index')
146-
name = isDirIndex ? `./${name.slice(0, -6)}` : `./${name}`
148+
name = `./${name}`
147149
}
148150

149151
let subExport = exportsMap.get(name)

0 commit comments

Comments
 (0)