Skip to content

Commit

Permalink
feat(externals): improved output package.json (#1607)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Aug 20, 2023
1 parent bc9644e commit 1d651e5
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/rollup/plugins/externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { existsSync, promises as fsp } from "node:fs";
import { platform } from "node:os";
import { resolve, dirname, normalize, join, isAbsolute, relative } from "pathe";
import type { PackageJson } from "pkg-types";
import { readPackageJSON } from "pkg-types";
import { readPackageJSON, writePackageJSON } from "pkg-types";
import { nodeFileTrace, NodeFileTraceOptions } from "@vercel/nft";
import type { Plugin } from "rollup";
import {
Expand All @@ -26,6 +26,7 @@ export interface NodeExternalsOptions {
| RegExp
| ((id: string, importer?: string) => Promise<boolean> | boolean)
>;
rootDir?: string;
outDir?: string;
trace?: boolean;
traceOptions?: NodeFileTraceOptions;
Expand Down Expand Up @@ -290,6 +291,8 @@ export function externals(opts: NodeExternalsOptions): Plugin {
tracedFile.pkgVersion = pkgJSON.version;
}

const usedAliases: Record<string, string> = {};

const writePackage = async (
name: string,
version: string,
Expand Down Expand Up @@ -325,6 +328,7 @@ export function externals(opts: NodeExternalsOptions): Plugin {

// Link aliases
if (opts.traceAlias && pkgPath in opts.traceAlias) {
usedAliases[opts.traceAlias[pkgPath]] = version;
await linkPackage(pkgPath, opts.traceAlias[pkgPath]);
}
};
Expand Down Expand Up @@ -440,26 +444,25 @@ export function externals(opts: NodeExternalsOptions): Plugin {
}

// Write an informative package.json
const bundledDependencies = Object.fromEntries(
Object.values(tracedPackages)
.sort((a, b) => a.name.localeCompare(b.name))
.map((pkg) => [pkg.name, Object.keys(pkg.versions).join(" || ")])
);

await fsp.writeFile(
resolve(opts.outDir, "package.json"),
JSON.stringify(
{
name: "nitro-output",
version: "0.0.0",
private: true,
bundledDependencies,
},
null,
2
const userPkg = await readPackageJSON(
opts.rootDir || process.cwd()
).catch(() => ({}) as PackageJson);

await writePackageJSON(resolve(opts.outDir, "package.json"), {
name: (userPkg.name || "server") + "-prod",
version: userPkg.version || "0.0.0",
type: "module",
private: true,
dependencies: Object.fromEntries(
[
...Object.values(tracedPackages).map((pkg) => [
pkg.name,
Object.keys(pkg.versions)[0],
]),
...Object.entries(usedAliases),
].sort(([a], [b]) => a[0].localeCompare(b[0]))
),
"utf8"
);
});
},
};
}
Expand Down

0 comments on commit 1d651e5

Please sign in to comment.