Skip to content

Commit 6eab363

Browse files
authored
Test nested package exports map and check whether files exist (#266)
1 parent 228c394 commit 6eab363

File tree

7 files changed

+185
-0
lines changed

7 files changed

+185
-0
lines changed

.changeset/neat-eyes-help.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'bob-the-bundler': patch
3+
---
4+
5+
Check whether package exports map files exist

src/commands/check.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ async function checkExportsMapIntegrity(args: {
170170
const cjsFilePaths = await globby(cjsResult, {
171171
cwd: args.cwd,
172172
});
173+
if (!cjsFilePaths.length) {
174+
throw new Error(
175+
`No files found matching the path '${cjsResult}' in '${key}' for '${args.packageJSON.name}'.`,
176+
);
177+
}
173178

174179
const limit = pLimit(20);
175180
await Promise.all(
@@ -212,6 +217,11 @@ async function checkExportsMapIntegrity(args: {
212217
const esmFilePaths = await globby(esmResult, {
213218
cwd: args.cwd,
214219
});
220+
if (!esmFilePaths.length) {
221+
throw new Error(
222+
`No files found matching the path '${esmResult}' in '${key}' for '${args.packageJSON.name}'.`,
223+
);
224+
}
215225

216226
const limit = pLimit(20);
217227
await Promise.all(
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "simple-exports",
3+
"type": "module",
4+
"engines": {
5+
"node": ">= 12.0.0",
6+
"pnpm": ">= 8.0.0"
7+
},
8+
"main": "dist/cjs/index.js",
9+
"module": "dist/esm/index.js",
10+
"exports": {
11+
".": {
12+
"require": {
13+
"types": "./dist/typings/index.d.cts",
14+
"default": "./dist/cjs/index.js"
15+
},
16+
"import": {
17+
"types": "./dist/typings/index.d.ts",
18+
"default": "./dist/esm/index.js"
19+
},
20+
"default": {
21+
"types": "./dist/typings/index.d.ts",
22+
"default": "./dist/esm/index.js"
23+
}
24+
},
25+
"./sub": {
26+
"require": {
27+
"types": "./dist/typings/sub/index.d.cts",
28+
"default": "./dist/cjs/sub/index.js"
29+
},
30+
"import": {
31+
"types": "./dist/typings/sub/index.d.ts",
32+
"default": "./dist/esm/sub/index.js"
33+
},
34+
"default": {
35+
"types": "./dist/typings/sub/index.d.ts",
36+
"default": "./dist/esm/sub/index.js"
37+
}
38+
},
39+
"./package.json": "./package.json"
40+
},
41+
"typings": "dist/typings/index.d.ts",
42+
"publishConfig": {
43+
"directory": "dist",
44+
"access": "public"
45+
},
46+
"typescript": {
47+
"definition": "dist/typings/index.d.ts"
48+
}
49+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const someLetter = 'a';
2+
3+
export default { b: 'c' };
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const someOtherLetter = 'd';
2+
3+
export default { e: 'f' };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"module": "ESNext",
4+
"skipLibCheck": true,
5+
"declaration": true,
6+
"outDir": "dist"
7+
}
8+
}

test/integration.spec.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,3 +745,110 @@ it('can bundle a tsconfig-build-json project', async () => {
745745
cwd: path.resolve(fixturesFolder, 'simple'),
746746
});
747747
});
748+
749+
it('can bundle a simple project with additional exports', async () => {
750+
const proj = path.join(fixturesFolder, 'simple-exports');
751+
const dist = path.join(proj, 'dist');
752+
753+
await fse.remove(dist);
754+
755+
await expect(execa('node', [binaryFolder, 'build'], { cwd: proj })).resolves.toEqual(
756+
expect.objectContaining({
757+
exitCode: 0,
758+
}),
759+
);
760+
761+
await expect(fse.readFile(path.join(dist, 'cjs', 'index.js'), 'utf8')).resolves
762+
.toMatchInlineSnapshot(`
763+
"use strict";
764+
Object.defineProperty(exports, "__esModule", { value: true });
765+
exports.someLetter = void 0;
766+
exports.someLetter = 'a';
767+
exports.default = { b: 'c' };
768+
`);
769+
await expect(fse.readFile(path.join(dist, 'typings', 'index.d.ts'), 'utf8')).resolves
770+
.toMatchInlineSnapshot(`
771+
export declare const someLetter = "a";
772+
declare const _default: {
773+
b: string;
774+
};
775+
export default _default;
776+
`);
777+
await expect(fse.readFile(path.join(dist, 'esm', 'index.js'), 'utf8')).resolves
778+
.toMatchInlineSnapshot(`
779+
export var someLetter = 'a';
780+
export default { b: 'c' };
781+
`);
782+
783+
await expect(fse.readFile(path.join(dist, 'cjs', 'sub', 'index.js'), 'utf8')).resolves
784+
.toMatchInlineSnapshot(`
785+
"use strict";
786+
Object.defineProperty(exports, "__esModule", { value: true });
787+
exports.someOtherLetter = void 0;
788+
exports.someOtherLetter = 'd';
789+
exports.default = { e: 'f' };
790+
`);
791+
await expect(fse.readFile(path.join(dist, 'typings', 'sub', 'index.d.ts'), 'utf8')).resolves
792+
.toMatchInlineSnapshot(`
793+
export declare const someOtherLetter = "d";
794+
declare const _default: {
795+
e: string;
796+
};
797+
export default _default;
798+
`);
799+
await expect(fse.readFile(path.join(dist, 'esm', 'sub', 'index.js'), 'utf8')).resolves
800+
.toMatchInlineSnapshot(`
801+
export var someOtherLetter = 'd';
802+
export default { e: 'f' };
803+
`);
804+
805+
await expect(fse.readFile(path.join(dist, 'package.json'), 'utf8')).resolves
806+
.toMatchInlineSnapshot(`
807+
{
808+
"name": "simple-exports",
809+
"engines": {
810+
"node": ">= 12.0.0"
811+
},
812+
"main": "cjs/index.js",
813+
"module": "esm/index.js",
814+
"typings": "typings/index.d.ts",
815+
"typescript": {
816+
"definition": "typings/index.d.ts"
817+
},
818+
"type": "module",
819+
"exports": {
820+
".": {
821+
"require": {
822+
"types": "./typings/index.d.cts",
823+
"default": "./cjs/index.js"
824+
},
825+
"import": {
826+
"types": "./typings/index.d.ts",
827+
"default": "./esm/index.js"
828+
},
829+
"default": {
830+
"types": "./typings/index.d.ts",
831+
"default": "./esm/index.js"
832+
}
833+
},
834+
"./sub": {
835+
"require": {
836+
"types": "./typings/sub/index.d.cts",
837+
"default": "./cjs/sub/index.js"
838+
},
839+
"import": {
840+
"types": "./typings/sub/index.d.ts",
841+
"default": "./esm/sub/index.js"
842+
},
843+
"default": {
844+
"types": "./typings/sub/index.d.ts",
845+
"default": "./esm/sub/index.js"
846+
}
847+
},
848+
"./package.json": "./package.json"
849+
}
850+
}
851+
`);
852+
853+
await execa('node', [binaryFolder, 'check'], { cwd: proj });
854+
});

0 commit comments

Comments
 (0)