Skip to content

Commit 5a73adf

Browse files
authored
fix: emit error when source files are missing/mismatched (#122)
1 parent d592501 commit 5a73adf

File tree

2 files changed

+64
-18
lines changed

2 files changed

+64
-18
lines changed

src/utils/get-source-path.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,29 @@ const extensionMap = {
3030
const distExtensions = Object.keys(extensionMap) as (keyof typeof extensionMap)[];
3131

3232
export const getSourcePath = async (
33-
exportEntry: ExportEntry,
33+
{ outputPath }: ExportEntry,
3434
source: string,
3535
dist: string,
3636
) => {
37-
const sourcePathUnresolved = source + exportEntry.outputPath.slice(dist.length);
38-
39-
for (const distExtension of distExtensions) {
40-
if (exportEntry.outputPath.endsWith(distExtension)) {
41-
const sourcePath = await tryExtensions(
42-
sourcePathUnresolved.slice(0, -distExtension.length),
43-
extensionMap[distExtension],
44-
);
45-
46-
if (sourcePath) {
47-
return {
48-
input: sourcePath.path,
49-
srcExtension: sourcePath.extension,
50-
distExtension,
51-
};
52-
}
37+
const sourcePathUnresolved = source + outputPath.slice(dist.length);
38+
39+
const distExtension = distExtensions.find(extension => outputPath.endsWith(extension));
40+
if (distExtension) {
41+
const sourcePathWithoutExtension = sourcePathUnresolved.slice(0, -distExtension.length);
42+
const sourcePath = await tryExtensions(
43+
sourcePathWithoutExtension,
44+
extensionMap[distExtension],
45+
);
46+
47+
if (sourcePath) {
48+
return {
49+
input: sourcePath.path,
50+
srcExtension: sourcePath.extension,
51+
distExtension,
52+
};
5353
}
54+
throw new Error(`Could not find matching source file for export path: ${stringify(outputPath)}; Expected: ${sourcePathWithoutExtension}[${extensionMap[distExtension].join('|')}]`);
5455
}
5556

56-
throw new Error(`Could not find matching source file for export path ${stringify(exportEntry.outputPath)}`);
57+
throw new Error(`Package.json output path contains invalid extension: ${stringify(outputPath)}; Expected: ${distExtensions.join(', ')}`);
5758
};

tests/specs/error-cases.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,50 @@ export default testSuite(({ describe }, nodePath: string) => {
106106
expect(pkgrollProcess.stderr).toMatch('Ignoring entry outside of ./dist/ directory: package.json#main="/dist/main.js"');
107107
expect(pkgrollProcess.stderr).toMatch('No export entries found in package.json');
108108
});
109+
110+
test('cannot find matching source file', async () => {
111+
await using fixture = await createFixture({
112+
...packageFixture(),
113+
'package.json': createPackageJson({
114+
name: 'pkg',
115+
main: 'dist/missing.js',
116+
module: 'dist/missing.mjs',
117+
}),
118+
});
119+
120+
const pkgrollProcess = await pkgroll(
121+
[],
122+
{
123+
cwd: fixture.path,
124+
nodePath,
125+
reject: false,
126+
},
127+
);
128+
expect(pkgrollProcess.exitCode).toBe(1);
129+
expect(pkgrollProcess.stderr).toMatch('Could not find matching source file for export path');
130+
expect(pkgrollProcess.stderr).toMatch('Expected: ./src/missing[.js|.ts|.tsx|.mts|.cts]');
131+
});
132+
133+
test('unexpected extension', async () => {
134+
await using fixture = await createFixture({
135+
...packageFixture(),
136+
'package.json': createPackageJson({
137+
name: 'pkg',
138+
main: 'dist/index.foo',
139+
}),
140+
});
141+
142+
const pkgrollProcess = await pkgroll(
143+
[],
144+
{
145+
cwd: fixture.path,
146+
nodePath,
147+
reject: false,
148+
},
149+
);
150+
expect(pkgrollProcess.exitCode).toBe(1);
151+
expect(pkgrollProcess.stderr).toMatch('Error: Package.json output path contains invalid extension');
152+
expect(pkgrollProcess.stderr).toMatch('Expected: .d.ts, .d.mts, .d.cts, .js, .mjs, .cjs');
153+
});
109154
});
110155
});

0 commit comments

Comments
 (0)