Skip to content

Commit 7949216

Browse files
committed
fix(exports): include non-entry chunks in exports when all is true
Closes #752
1 parent e4230d1 commit 7949216

File tree

2 files changed

+91
-4
lines changed

2 files changed

+91
-4
lines changed

src/features/pkg/exports.test.ts

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,82 @@ describe('generateExports', () => {
405405
`)
406406
})
407407

408+
test('export all includes non-entry chunks', async ({ expect }) => {
409+
const results = generateExports(
410+
{
411+
es: [genChunk('index.js'), genChunk('utils.js', false)],
412+
},
413+
{
414+
exports: { all: true },
415+
},
416+
)
417+
await expect(results).resolves.toMatchInlineSnapshot(`
418+
{
419+
"exports": {
420+
".": "./index.js",
421+
"./*": "./*",
422+
"./utils": "./utils.js",
423+
},
424+
"main": undefined,
425+
"module": undefined,
426+
"publishExports": undefined,
427+
"types": undefined,
428+
}
429+
`)
430+
})
431+
432+
test('export all excludes virtual modules', async ({ expect }) => {
433+
const results = generateExports(
434+
{
435+
es: [
436+
genChunk('index.js'),
437+
genChunk('virtual.js', false, '\0virtual-module'),
438+
],
439+
},
440+
{
441+
exports: { all: true },
442+
},
443+
)
444+
await expect(results).resolves.toMatchInlineSnapshot(`
445+
{
446+
"exports": {
447+
".": "./index.js",
448+
"./*": "./*",
449+
},
450+
"main": undefined,
451+
"module": undefined,
452+
"publishExports": undefined,
453+
"types": undefined,
454+
}
455+
`)
456+
})
457+
458+
test('export all excludes node_modules chunks', async ({ expect }) => {
459+
const results = generateExports(
460+
{
461+
es: [
462+
genChunk('index.js'),
463+
genChunk('lodash.js', false, '/project/node_modules/lodash/index.js'),
464+
],
465+
},
466+
{
467+
exports: { all: true },
468+
},
469+
)
470+
await expect(results).resolves.toMatchInlineSnapshot(`
471+
{
472+
"exports": {
473+
".": "./index.js",
474+
"./*": "./*",
475+
},
476+
"main": undefined,
477+
"module": undefined,
478+
"publishExports": undefined,
479+
"types": undefined,
480+
}
481+
`)
482+
})
483+
408484
test('windows-like paths for subpackages', async ({ expect }) => {
409485
const results = generateExports({
410486
es: [
@@ -604,13 +680,13 @@ describe('generateExports', () => {
604680
})
605681
})
606682

607-
function genChunk(fileName: string, isEntry = true) {
683+
function genChunk(fileName: string, isEntry = true, facadeModuleId?: string) {
608684
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
609685
return {
610686
type: 'chunk',
611687
fileName,
612688
isEntry,
613-
facadeModuleId: path.resolve(`./SRC/${fileName}`),
689+
facadeModuleId: facadeModuleId ?? path.resolve(`./SRC/${fileName}`),
614690
outDir: cwd,
615691
} as RolldownChunk
616692
}

src/features/pkg/exports.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readFileSync, writeFileSync } from 'node:fs'
22
import path from 'node:path'
3-
import { RE_CSS, RE_DTS } from 'rolldown-plugin-dts/filename'
3+
import { RE_CSS, RE_DTS, RE_NODE_MODULES } from 'rolldown-plugin-dts/filename'
44
import { detectIndentation } from '../../utils/format.ts'
55
import { stripExtname } from '../../utils/fs.ts'
66
import { matchPattern, slash, typeAssert } from '../../utils/general.ts'
@@ -183,10 +183,21 @@ export async function generateExports(
183183
// Filter out non-entry chunks and excluded files
184184
const filteredChunks = chunksByFormat.filter(
185185
(chunk): chunk is RolldownCodeChunk => {
186-
if (chunk.type !== 'chunk' || !chunk.isEntry) {
186+
if (chunk.type !== 'chunk') {
187187
return false
188188
}
189189

190+
if (!chunk.isEntry) {
191+
if (!all) return false
192+
193+
if (
194+
chunk.facadeModuleId?.[0] === '\0' ||
195+
(chunk.facadeModuleId && RE_NODE_MODULES.test(chunk.facadeModuleId))
196+
) {
197+
return false
198+
}
199+
}
200+
190201
const [name] = getExportName(chunk)
191202
return !shouldExclude(name, exclude)
192203
},

0 commit comments

Comments
 (0)