Skip to content

Commit 223de44

Browse files
committed
implement modern commonjs
1 parent b023519 commit 223de44

File tree

2 files changed

+42
-42
lines changed

2 files changed

+42
-42
lines changed

src/commands/build.ts

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -499,55 +499,55 @@ async function setPackageJsonsType(
499499
const rootPkgJsonPath = join(cwd, 'package.json');
500500
const rootContents = await fse.readFile(rootPkgJsonPath, 'utf8');
501501
const rootPkg = JSON.parse(rootContents);
502+
const workspaces = await getWorkspaces(rootPkg);
503+
const isSinglePackage = workspaces === null;
502504

503505
const reverts: (() => Promise<void>)[] = [];
504506

505-
if ('workspaces' in rootPkg) {
506-
for (const pkgJsonPath of [
507-
// we also want to modify the root package.json
508-
// TODO: do we?
509-
rootPkgJsonPath,
510-
// get all package.jsons from the defined workspaces
511-
...(await globby(
512-
rootPkg.workspaces.map((w: string) => w + '/package.json'),
513-
{ cwd, absolute: true },
514-
)),
515-
]) {
516-
const contents =
517-
pkgJsonPath === rootPkgJsonPath
518-
? // no need to re-read the root package.json
519-
rootContents
520-
: await fse.readFile(pkgJsonPath, 'utf8');
521-
const endsWithNewline = contents.endsWith('\n');
522-
523-
const pkg = JSON.parse(contents);
524-
if (pkg.type != null && pkg.type !== 'commonjs' && pkg.type !== 'module') {
525-
throw new Error(`Invalid "type" property value "${pkg.type}" in ${pkgJsonPath}`);
526-
}
507+
for (const pkgJsonPath of [
508+
// we also want to modify the root package.json TODO: do we?
509+
rootPkgJsonPath,
510+
...(isSinglePackage
511+
? []
512+
: await globby(
513+
workspaces.map((w: string) => w + '/package.json'),
514+
{ cwd, absolute: true },
515+
)),
516+
]) {
517+
const contents =
518+
pkgJsonPath === rootPkgJsonPath
519+
? // no need to re-read the root package.json
520+
rootContents
521+
: await fse.readFile(pkgJsonPath, 'utf8');
522+
const endsWithNewline = contents.endsWith('\n');
523+
524+
const pkg = JSON.parse(contents);
525+
if (pkg.type != null && pkg.type !== 'commonjs' && pkg.type !== 'module') {
526+
throw new Error(`Invalid "type" property value "${pkg.type}" in ${pkgJsonPath}`);
527+
}
527528

528-
const originalType: PackageJsonType | undefined = pkg.type;
529-
const differentType =
530-
(pkg.type ||
531-
// default when the type is not defined
532-
'commonjs') !== type;
529+
const originalType: PackageJsonType | undefined = pkg.type;
530+
const differentType =
531+
(pkg.type ||
532+
// default when the type is not defined
533+
'commonjs') !== type;
534+
535+
// change only if the provided type is different
536+
if (differentType) {
537+
pkg.type = type;
538+
await fse.writeFile(
539+
pkgJsonPath,
540+
JSON.stringify(pkg, null, ' ') + (endsWithNewline ? '\n' : ''),
541+
);
533542

534-
// change only if the provided type is different
535-
if (differentType) {
536-
pkg.type = type;
543+
// revert change, of course only if we changed something
544+
reverts.push(async () => {
545+
pkg.type = originalType;
537546
await fse.writeFile(
538547
pkgJsonPath,
539548
JSON.stringify(pkg, null, ' ') + (endsWithNewline ? '\n' : ''),
540549
);
541-
542-
// revert change, of course only if we changed something
543-
reverts.push(async () => {
544-
pkg.type = originalType;
545-
await fse.writeFile(
546-
pkgJsonPath,
547-
JSON.stringify(pkg, null, ' ') + (endsWithNewline ? '\n' : ''),
548-
);
549-
});
550-
}
550+
});
551551
}
552552
}
553553

test/integration.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ it('can build a monorepo project', async () => {
204204
__exportStar(require("./foo.js"), exports);
205205
exports.b = 'SUP' + foo_js_1.b;
206206
function foo() {
207-
return Promise.resolve().then(() => require('./foo.js'));
207+
return import('./foo.js');
208208
}
209209
`);
210210
expect(await fse.readFile(files.b['typings/index.d.ts'], 'utf8')).toMatchInlineSnapshot(`
@@ -552,7 +552,7 @@ it('can build a monorepo pnpm project', async () => {
552552
__exportStar(require("./foo.js"), exports);
553553
exports.b = 'SUP' + foo_js_1.b;
554554
function foo() {
555-
return Promise.resolve().then(() => require('./foo.js'));
555+
return import('./foo.js');
556556
}
557557
`);
558558
expect(await fse.readFile(files.b['typings/index.d.ts'], 'utf8')).toMatchInlineSnapshot(`

0 commit comments

Comments
 (0)