Skip to content

Commit 7e6385b

Browse files
committed
adapt
1 parent 537a1c3 commit 7e6385b

File tree

7 files changed

+50
-98
lines changed

7 files changed

+50
-98
lines changed

src/commands/bootstrap.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { getWorkspaces } from '../utils/get-workspaces.js';
1010
import { rewriteCodeImports } from '../utils/rewrite-code-imports.js';
1111

1212
/** The default bob fields that should be within a package.json */
13-
export const presetFields = Object.freeze({
13+
export const presetFieldsDual = Object.freeze({
1414
type: 'module',
1515
main: 'dist/esm/index.js',
1616
typings: 'dist/typings/index.d.ts',
@@ -38,7 +38,7 @@ export const presetFields = Object.freeze({
3838
},
3939
});
4040

41-
export const presetFieldsESM = {
41+
export const presetFieldsOnlyESM = {
4242
type: 'module',
4343
main: 'dist/esm/index.js',
4444
typings: 'dist/typings/index.d.ts',
@@ -85,7 +85,7 @@ async function applyPackageJSONPresetConfig(
8585
packageJSONPath: string,
8686
packageJSON: Record<string, unknown>,
8787
) {
88-
Object.assign(packageJSON, presetFields);
88+
Object.assign(packageJSON, presetFieldsDual);
8989
await fse.writeFile(packageJSONPath, JSON.stringify(packageJSON, null, 2));
9090
}
9191

src/commands/build.ts

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { getRootPackageJSON } from '../utils/get-root-package-json.js';
1212
import { getWorkspacePackagePaths } from '../utils/get-workspace-package-paths.js';
1313
import { getWorkspaces } from '../utils/get-workspaces.js';
1414
import { rewriteExports } from '../utils/rewrite-exports.js';
15-
import { presetFields, presetFieldsESM } from './bootstrap.js';
15+
import { presetFieldsDual, presetFieldsOnlyESM } from './bootstrap.js';
1616

1717
export const DIST_DIR = 'dist';
1818

@@ -227,9 +227,9 @@ async function build({
227227
return;
228228
}
229229

230-
validatePackageJson(pkg, {
231-
includesCommonJS: config?.commonjs ?? true,
232-
});
230+
const dual = config?.commonjs ?? true;
231+
232+
validatePackageJson(pkg, { dual });
233233

234234
const declarations = await globby('**/*.d.ts', {
235235
cwd: getBuildPath('esm'),
@@ -269,7 +269,7 @@ async function build({
269269
),
270270
);
271271

272-
if (config?.commonjs === undefined) {
272+
if (dual) {
273273
// Transpile ESM to CJS and move CJS to dist/cjs only if there's something to transpile
274274
await fse.ensureDir(join(distPath, 'cjs'));
275275

@@ -369,9 +369,7 @@ function rewritePackageJson(pkg: Record<string, any>) {
369369
'engines',
370370
'name',
371371
'main',
372-
'module',
373372
'typings',
374-
'typescript',
375373
'type',
376374
];
377375

@@ -393,14 +391,10 @@ function rewritePackageJson(pkg: Record<string, any>) {
393391
const distDirStr = `${DIST_DIR}/`;
394392

395393
newPkg.main = newPkg.main.replace(distDirStr, '');
396-
newPkg.module = newPkg.module.replace(distDirStr, '');
397394
newPkg.typings = newPkg.typings.replace(distDirStr, '');
398-
newPkg.typescript = {
399-
definition: newPkg.typescript.definition.replace(distDirStr, ''),
400-
};
401395

402396
if (!pkg.exports) {
403-
newPkg.exports = presetFields.exports;
397+
newPkg.exports = presetFieldsDual.exports;
404398
}
405399
newPkg.exports = rewriteExports(pkg.exports, DIST_DIR);
406400

@@ -418,7 +412,7 @@ function rewritePackageJson(pkg: Record<string, any>) {
418412
export function validatePackageJson(
419413
pkg: any,
420414
opts: {
421-
includesCommonJS: boolean;
415+
dual: boolean;
422416
},
423417
) {
424418
function expect(key: string, expected: unknown) {
@@ -438,35 +432,29 @@ export function validatePackageJson(
438432
// 2. have an exports property
439433
// 3. have an exports and bin property
440434
if (Object.keys(pkg.bin ?? {}).length > 0) {
441-
if (opts.includesCommonJS === true) {
442-
expect('main', presetFields.main);
443-
expect('typings', presetFields.typings);
435+
if (opts.dual === true) {
436+
expect('main', presetFieldsDual.main);
437+
expect('typings', presetFieldsDual.typings);
444438
} else {
445-
expect('main', presetFieldsESM.main);
446-
expect('typings', presetFieldsESM.typings);
439+
expect('main', presetFieldsOnlyESM.main);
440+
expect('typings', presetFieldsOnlyESM.typings);
447441
}
448-
} else if (
449-
pkg.main !== undefined ||
450-
pkg.module !== undefined ||
451-
pkg.exports !== undefined ||
452-
pkg.typings !== undefined ||
453-
pkg.typescript !== undefined
454-
) {
455-
if (opts.includesCommonJS === true) {
442+
} else if (pkg.main !== undefined || pkg.exports !== undefined || pkg.typings !== undefined) {
443+
if (opts.dual === true) {
456444
// if there is no bin property, we NEED to check the exports.
457-
expect('main', presetFields.main);
458-
expect('typings', presetFields.typings);
445+
expect('main', presetFieldsDual.main);
446+
expect('typings', presetFieldsDual.typings);
459447

460448
// For now we enforce a top level exports property
461-
expect("exports['.'].require", presetFields.exports['.'].require);
462-
expect("exports['.'].import", presetFields.exports['.'].import);
463-
expect("exports['.'].default", presetFields.exports['.'].default);
449+
expect("exports['.'].require", presetFieldsDual.exports['.'].require);
450+
expect("exports['.'].import", presetFieldsDual.exports['.'].import);
451+
expect("exports['.'].default", presetFieldsDual.exports['.'].default);
464452
} else {
465-
expect('main', presetFieldsESM.main);
466-
expect('typings', presetFieldsESM.typings);
453+
expect('main', presetFieldsOnlyESM.main);
454+
expect('typings', presetFieldsOnlyESM.typings);
467455

468456
// For now, we enforce a top level exports property
469-
expect("exports['.']", presetFieldsESM.exports['.']);
457+
expect("exports['.']", presetFieldsOnlyESM.exports['.']);
470458
}
471459
}
472460
}

src/commands/check.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { getBobConfig } from '../config.js';
1010
import { getRootPackageJSON } from '../utils/get-root-package-json.js';
1111
import { getWorkspacePackagePaths } from '../utils/get-workspace-package-paths.js';
1212
import { getWorkspaces } from '../utils/get-workspaces.js';
13-
import { presetFields } from './bootstrap.js';
13+
import { presetFieldsDual } from './bootstrap.js';
1414

1515
const ExportsMapEntry = zod.object({
1616
default: zod.string(),
@@ -131,7 +131,7 @@ async function checkExportsMapIntegrity(args: {
131131
"Missing exports map within the 'package.json'.\n" +
132132
exportsMapResult.error.message +
133133
'\nCorrect Example:\n' +
134-
JSON.stringify(presetFields.exports, null, 2),
134+
JSON.stringify(presetFieldsDual.exports, null, 2),
135135
);
136136
}
137137

src/config.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ const BobConfigModel = zod.optional(
55
[
66
zod.literal(false),
77
zod.object({
8-
commonjs: zod.optional(zod.literal(false), {
9-
description: 'Omit CommonJS output.',
10-
}),
8+
commonjs: zod
9+
.boolean({
10+
description:
11+
'Enable CommonJS output creating a dual output ESM+CJS. If set to `false`, will generate only ESM output.',
12+
})
13+
.optional()
14+
.default(true),
1115
build: zod.union(
1216
[
1317
zod.literal(false),

test/__fixtures__/simple-monorepo-pnpm/packages/c/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "c",
3+
"type": "module",
34
"engines": {
45
"node": ">= 14.0.0"
56
},
6-
"main": "dist/cjs/index.js",
7+
"main": "dist/esm/index.js",
78
"exports": {
89
".": {
910
"require": {

test/__fixtures__/simple-monorepo/packages/c/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "c",
3+
"type": "module",
34
"engines": {
45
"node": ">= 14.0.0"
56
},
6-
"main": "dist/cjs/index.js",
7+
"main": "dist/esm/index.js",
78
"exports": {
89
".": {
910
"require": {

test/integration.spec.ts

Lines changed: 12 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,8 @@ it('can bundle a simple project', async () => {
4444
"engines": {
4545
"node": ">= 12.0.0"
4646
},
47-
"main": "cjs/index.js",
48-
"module": "esm/index.js",
47+
"main": "esm/index.js",
4948
"typings": "typings/index.d.ts",
50-
"typescript": {
51-
"definition": "typings/index.d.ts"
52-
},
5349
"type": "module",
5450
"exports": {
5551
".": {
@@ -140,12 +136,8 @@ it('can build a monorepo project', async () => {
140136
"engines": {
141137
"node": ">= 14.0.0"
142138
},
143-
"main": "cjs/index.js",
144-
"module": "esm/index.js",
139+
"main": "esm/index.js",
145140
"typings": "typings/index.d.ts",
146-
"typescript": {
147-
"definition": "typings/index.d.ts"
148-
},
149141
"type": "module",
150142
"exports": {
151143
".": {
@@ -226,12 +218,8 @@ it('can build a monorepo project', async () => {
226218
"engines": {
227219
"node": ">= 14.0.0"
228220
},
229-
"main": "cjs/index.js",
230-
"module": "esm/index.js",
221+
"main": "esm/index.js",
231222
"typings": "typings/index.d.ts",
232-
"typescript": {
233-
"definition": "typings/index.d.ts"
234-
},
235223
"type": "module",
236224
"exports": {
237225
".": {
@@ -283,12 +271,9 @@ it('can build a monorepo project', async () => {
283271
"engines": {
284272
"node": ">= 14.0.0"
285273
},
286-
"main": "cjs/index.js",
287-
"module": "esm/index.js",
274+
"main": "esm/index.js",
288275
"typings": "typings/index.d.ts",
289-
"typescript": {
290-
"definition": "typings/index.d.ts"
291-
},
276+
"type": "module",
292277
"exports": {
293278
".": {
294279
"require": {
@@ -332,11 +317,7 @@ it('can build an esm only project', async () => {
332317
"node": ">= 14.0.0"
333318
},
334319
"main": "esm/index.js",
335-
"module": "esm/index.js",
336320
"typings": "typings/index.d.ts",
337-
"typescript": {
338-
"definition": "typings/index.d.ts"
339-
},
340321
"type": "module",
341322
"exports": {
342323
".": {
@@ -379,12 +360,8 @@ it('can build a types only project', async () => {
379360
"engines": {
380361
"node": ">= 14.0.0"
381362
},
382-
"main": "cjs/index.js",
383-
"module": "esm/index.js",
363+
"main": "esm/index.js",
384364
"typings": "typings/index.d.ts",
385-
"typescript": {
386-
"definition": "typings/index.d.ts"
387-
},
388365
"type": "module",
389366
"exports": {
390367
".": {
@@ -488,12 +465,8 @@ it('can build a monorepo pnpm project', async () => {
488465
"engines": {
489466
"node": ">= 14.0.0"
490467
},
491-
"main": "cjs/index.js",
492-
"module": "esm/index.js",
468+
"main": "esm/index.js",
493469
"typings": "typings/index.d.ts",
494-
"typescript": {
495-
"definition": "typings/index.d.ts"
496-
},
497470
"type": "module",
498471
"exports": {
499472
".": {
@@ -574,12 +547,8 @@ it('can build a monorepo pnpm project', async () => {
574547
"engines": {
575548
"node": ">= 14.0.0"
576549
},
577-
"main": "cjs/index.js",
578-
"module": "esm/index.js",
550+
"main": "esm/index.js",
579551
"typings": "typings/index.d.ts",
580-
"typescript": {
581-
"definition": "typings/index.d.ts"
582-
},
583552
"type": "module",
584553
"exports": {
585554
".": {
@@ -631,12 +600,9 @@ it('can build a monorepo pnpm project', async () => {
631600
"engines": {
632601
"node": ">= 14.0.0"
633602
},
634-
"main": "cjs/index.js",
635-
"module": "esm/index.js",
603+
"main": "esm/index.js",
636604
"typings": "typings/index.d.ts",
637-
"typescript": {
638-
"definition": "typings/index.d.ts"
639-
},
605+
"type": "module",
640606
"exports": {
641607
".": {
642608
"require": {
@@ -678,12 +644,8 @@ it('can bundle a tsconfig-build-json project', async () => {
678644
"engines": {
679645
"node": ">= 14.0.0"
680646
},
681-
"main": "cjs/index.js",
682-
"module": "esm/index.js",
647+
"main": "esm/index.js",
683648
"typings": "typings/index.d.ts",
684-
"typescript": {
685-
"definition": "typings/index.d.ts"
686-
},
687649
"type": "module",
688650
"exports": {
689651
".": {
@@ -809,12 +771,8 @@ it('can bundle a simple project with additional exports', async () => {
809771
"engines": {
810772
"node": ">= 12.0.0"
811773
},
812-
"main": "cjs/index.js",
813-
"module": "esm/index.js",
774+
"main": "esm/index.js",
814775
"typings": "typings/index.d.ts",
815-
"typescript": {
816-
"definition": "typings/index.d.ts"
817-
},
818776
"type": "module",
819777
"exports": {
820778
".": {

0 commit comments

Comments
 (0)