From b556dabdaa1aa5cb0d910911d0b74bffe61f9e4c Mon Sep 17 00:00:00 2001 From: Adam Skoufis Date: Wed, 6 Nov 2024 14:52:53 +1100 Subject: [PATCH] Async import `sort-package-json` --- .changeset/twelve-berries-reflect.md | 5 ++ packages/core/src/utils/files.ts | 8 ++- .../core/src/utils/setup-package-json.test.ts | 66 ++++++++++--------- packages/core/src/utils/setup-package-json.ts | 10 +-- 4 files changed, 51 insertions(+), 38 deletions(-) create mode 100644 .changeset/twelve-berries-reflect.md diff --git a/.changeset/twelve-berries-reflect.md b/.changeset/twelve-berries-reflect.md new file mode 100644 index 00000000..c8e704c4 --- /dev/null +++ b/.changeset/twelve-berries-reflect.md @@ -0,0 +1,5 @@ +--- +'@crackle/core': patch +--- + +Update `sort-package-json` dependency diff --git a/packages/core/src/utils/files.ts b/packages/core/src/utils/files.ts index 8efe538f..190b7879 100644 --- a/packages/core/src/utils/files.ts +++ b/packages/core/src/utils/files.ts @@ -1,7 +1,6 @@ import path from 'path'; import fse from 'fs-extra'; -import sortPackageJson from 'sort-package-json'; import type { Format, PackageJson } from '../types'; @@ -64,12 +63,15 @@ export const writePackageJson = async ({ }: { dir: string; contents: T; -}) => - writeIfRequired({ +}) => { + const { default: sortPackageJson } = await import('sort-package-json'); + + return writeIfRequired({ dir, fileName: 'package.json', contents: JSON.stringify(sortPackageJson(contents), null, 2).concat('\n'), }); +}; export const emptyDir = async (dir: string, skip = ['.git']): Promise => { if (!(await exists(dir))) { diff --git a/packages/core/src/utils/setup-package-json.test.ts b/packages/core/src/utils/setup-package-json.test.ts index 66caf5bd..a87f30b5 100644 --- a/packages/core/src/utils/setup-package-json.test.ts +++ b/packages/core/src/utils/setup-package-json.test.ts @@ -93,7 +93,9 @@ describe('diffPackageJson', () => { diffs, packageJson, expectedPackageJson, - }: ReturnType & { packageJson?: PackageJson }) => { + }: Awaited> & { + packageJson?: PackageJson; + }) => { expect(diffs).toMatchSnapshot('diffs'); expect( packageJson @@ -103,7 +105,7 @@ describe('diffPackageJson', () => { }; test('empty package.json', async () => { - const { diffs, expectedPackageJson } = diffPackageJson( + const { diffs, expectedPackageJson } = await diffPackageJson( packageRoot, {}, entries, @@ -113,17 +115,21 @@ describe('diffPackageJson', () => { }); test('correct package.json', async () => { - const { diffs } = diffPackageJson(packageRoot, correctPackageJson, entries); + const { diffs } = await diffPackageJson( + packageRoot, + correctPackageJson, + entries, + ); expect(diffs).toHaveLength(0); }); describe('incorrect package.json', () => { - test('main', () => { + test('main', async () => { const packageJson = structuredClone(correctPackageJson); packageJson.main = 'index.js'; - const { diffs, expectedPackageJson } = diffPackageJson( + const { diffs, expectedPackageJson } = await diffPackageJson( packageRoot, packageJson, entries, @@ -132,11 +138,11 @@ describe('diffPackageJson', () => { expectSnapshots({ diffs, packageJson, expectedPackageJson }); }); - test('module', () => { + test('module', async () => { const packageJson = structuredClone(correctPackageJson); packageJson.module = 'something/else.js'; - const { diffs, expectedPackageJson } = diffPackageJson( + const { diffs, expectedPackageJson } = await diffPackageJson( packageRoot, packageJson, entries, @@ -145,12 +151,12 @@ describe('diffPackageJson', () => { expectSnapshots({ diffs, packageJson, expectedPackageJson }); }); - test('types', () => { + test('types', async () => { const packageJson = structuredClone(correctPackageJson); // @ts-ignore delete packageJson.types; - const { diffs, expectedPackageJson } = diffPackageJson( + const { diffs, expectedPackageJson } = await diffPackageJson( packageRoot, packageJson, entries, @@ -159,10 +165,10 @@ describe('diffPackageJson', () => { expectSnapshots({ diffs, packageJson, expectedPackageJson }); }); - test('exports missing', () => { + test('exports missing', async () => { const { exports, ...packageJson } = structuredClone(correctPackageJson); - const { diffs, expectedPackageJson } = diffPackageJson( + const { diffs, expectedPackageJson } = await diffPackageJson( packageRoot, packageJson, entries, @@ -171,12 +177,12 @@ describe('diffPackageJson', () => { expectSnapshots({ diffs, packageJson, expectedPackageJson }); }); - test('files missing', () => { + test('files missing', async () => { const packageJson = structuredClone(correctPackageJson); // @ts-ignore delete packageJson.files; - const { diffs, expectedPackageJson } = diffPackageJson( + const { diffs, expectedPackageJson } = await diffPackageJson( packageRoot, packageJson, entries, @@ -185,12 +191,12 @@ describe('diffPackageJson', () => { expectSnapshots({ diffs, packageJson, expectedPackageJson }); }); - test('files out of order', () => { + test('files out of order', async () => { const packageJson = structuredClone(correctPackageJson); const [first, ...otherFiles] = packageJson.files; packageJson.files = [...otherFiles, first]; - const { diffs, expectedPackageJson } = diffPackageJson( + const { diffs, expectedPackageJson } = await diffPackageJson( packageRoot, packageJson, entries, @@ -199,7 +205,7 @@ describe('diffPackageJson', () => { expectSnapshots({ diffs, packageJson, expectedPackageJson }); }); - test('keys out of order', () => { + test('keys out of order', async () => { const { main, types, ...everythingElse } = correctPackageJson; const packageJson = { types, @@ -207,7 +213,7 @@ describe('diffPackageJson', () => { main, }; - const { diffs, expectedPackageJson } = diffPackageJson( + const { diffs, expectedPackageJson } = await diffPackageJson( packageRoot, packageJson, entries, @@ -217,10 +223,10 @@ describe('diffPackageJson', () => { }); describe('sideEffects', () => { - test('no flag', () => { + test('no flag', async () => { const packageJson = structuredClone(correctPackageJson); - const { diffs, expectedPackageJson } = diffPackageJson( + const { diffs, expectedPackageJson } = await diffPackageJson( packageRoot, packageJson, entries, @@ -229,13 +235,13 @@ describe('diffPackageJson', () => { expectSnapshots({ diffs, packageJson, expectedPackageJson }); }); - test.each([true, false])('boolean flag %s', (flag) => { + test.each([true, false])('boolean flag %s', async (flag) => { const packageJson = sortPackageJson({ ...correctPackageJson, sideEffects: flag, }); - const { diffs, expectedPackageJson } = diffPackageJson( + const { diffs, expectedPackageJson } = await diffPackageJson( packageRoot, packageJson, entries, @@ -244,7 +250,7 @@ describe('diffPackageJson', () => { expectSnapshots({ diffs, packageJson, expectedPackageJson }); }); - test('string flag', () => { + test('string flag', async () => { const packageJson = sortPackageJson({ ...correctPackageJson, // Parcel says sideEffects can be a string @@ -252,7 +258,7 @@ describe('diffPackageJson', () => { sideEffects: 'src/reset/**' as any, }); - const { diffs, expectedPackageJson } = diffPackageJson( + const { diffs, expectedPackageJson } = await diffPackageJson( packageRoot, packageJson, entries, @@ -261,13 +267,13 @@ describe('diffPackageJson', () => { expectSnapshots({ diffs, packageJson, expectedPackageJson }); }); - test('array flag with no overlap', () => { + test('array flag with no overlap', async () => { const packageJson = sortPackageJson({ ...correctPackageJson, sideEffects: ['src/reset/**'], }); - const { diffs, expectedPackageJson } = diffPackageJson( + const { diffs, expectedPackageJson } = await diffPackageJson( packageRoot, packageJson, entries, @@ -276,13 +282,13 @@ describe('diffPackageJson', () => { expectSnapshots({ diffs, packageJson, expectedPackageJson }); }); - test('array flag with partial overlap', () => { + test('array flag with partial overlap', async () => { const packageJson = sortPackageJson({ ...correctPackageJson, sideEffects: ['dist/side-effects/**', 'src/entries/css.*'], }); - const { diffs, expectedPackageJson } = diffPackageJson( + const { diffs, expectedPackageJson } = await diffPackageJson( packageRoot, packageJson, entries, @@ -291,13 +297,13 @@ describe('diffPackageJson', () => { expectSnapshots({ diffs, packageJson, expectedPackageJson }); }); - test('array flag with complete overlap', () => { + test('array flag with complete overlap', async () => { const packageJson = sortPackageJson({ ...correctPackageJson, sideEffects: ['dist/side-effects/**', '**/themes/**'], }); - const { diffs, expectedPackageJson } = diffPackageJson( + const { diffs, expectedPackageJson } = await diffPackageJson( packageRoot, packageJson, entries, @@ -338,7 +344,7 @@ describe('diffPackageJson', () => { types: 'index.d.ts', } satisfies PackageJson; - const { diffs, expectedPackageJson } = diffPackageJson( + const { diffs, expectedPackageJson } = await diffPackageJson( packageRoot, packageJson, entries, diff --git a/packages/core/src/utils/setup-package-json.ts b/packages/core/src/utils/setup-package-json.ts index 600d7f12..f3bc454f 100644 --- a/packages/core/src/utils/setup-package-json.ts +++ b/packages/core/src/utils/setup-package-json.ts @@ -6,7 +6,6 @@ import { isDeepStrictEqual } from 'util'; // @ts-ignore no declaration file import structuredClonePolyfill from '@ungap/structured-clone'; import fse from 'fs-extra'; -import { sortPackageJson } from 'sort-package-json'; import { distDir, sideEffectsDir } from '../constants'; import type { PackageEntryPoint as Entry, PackageJson } from '../types'; @@ -132,14 +131,14 @@ const getSideEffectsForPackage = ( }; }; -export const diffPackageJson = ( +export const diffPackageJson = async ( packageRoot: string, packageJson: PackageJson, entries: Entry[], -): { +): Promise<{ diffs: Difference[]; expectedPackageJson: PackageJson; -} => { +}> => { const diffs: Difference[] = []; // create expected package.json @@ -169,6 +168,7 @@ export const diffPackageJson = ( expected.sideEffects = sideEffects; } + const { default: sortPackageJson } = await import('sort-package-json'); expected = sortPackageJson(expected); // do checks against expected package.json @@ -220,7 +220,7 @@ const setupPackageJson = const packagePath = path.join(packageRoot, 'package.json'); const packageJson: PackageJson = await fse.readJson(packagePath, { fs }); - const { diffs, expectedPackageJson } = diffPackageJson( + const { diffs, expectedPackageJson } = await diffPackageJson( packageRoot, packageJson, entries,