Skip to content

Commit

Permalink
crackle fix: don't overwrite package.json if not needed (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrm007 authored Feb 7, 2024
1 parent 28b16d2 commit 6125c6b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/core-fix-package.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@crackle/core': patch
---

`crackle fix`: don't overwrite `package.json` if not needed
18 changes: 10 additions & 8 deletions packages/core/src/entries/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import fs from 'fs/promises';
import path from 'path';
import process from 'process';

import type { Rollup } from 'vite';

import { type EnhancedConfig, type PartialConfig, getConfig } from '../config';
import { distDir } from '../constants';
import { createBundle } from '../package-utils/bundle';
Expand Down Expand Up @@ -87,13 +85,17 @@ const build = async (config: EnhancedConfig, packageName: string) => {

await updateGitignore(config.root, entries);

const cssExports = (bundles as Rollup.RollupOutput[])
.flatMap((bundle) => bundle.output)
.map((output) => output.fileName)
.filter((fileName) => fileName.endsWith('.css'))
.map((fileName) => path.join(distDir, fileName));
const cssExports =
Array.isArray(bundles) &&
bundles
.flatMap((bundle) => bundle.output)
.map((output) => output.fileName)
.filter((fileName) => fileName.endsWith('.css'))
.map((fileName) => path.join(distDir, fileName));

await updatePackageJsonExports(config.root, cssExports);
if (cssExports && cssExports.length > 0) {
await updatePackageJsonExports(config.root, cssExports);
}

logger.success(`Finished building \`${packageName}\`!`);
};
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/utils/setup-package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ const setupPackageJson =
entries,
);

if (write) {
if (write && diffs.length > 0) {
await writePackageJson({
dir: packageRoot,
contents: expectedPackageJson,
Expand All @@ -249,9 +249,7 @@ export const updatePackageJsonExports = async (
const packageJson: PackageJson = await fse.readJson(packagePath, { fs });

const packageExports = packageJson.exports as Exports;

const lastKey = Object.keys(packageExports).pop()!;
const lastExport = packageExports[lastKey];
const [lastKey, lastExport] = Object.entries(packageExports).pop()!;

delete packageExports[lastKey];
for (const entry of exports) {
Expand Down

0 comments on commit 6125c6b

Please sign in to comment.