Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(externals): apply production condition to package.exports #867

Merged
merged 1 commit into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions src/rollup/plugins/externals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { existsSync, promises as fsp } from "node:fs";
import { resolve, dirname, normalize, join, isAbsolute } from "pathe";
import type { PackageJson } from "pkg-types";
import { nodeFileTrace, NodeFileTraceOptions } from "@vercel/nft";
import type { Plugin } from "rollup";
import { resolvePath, isValidNodeImport, normalizeid } from "mlly";
Expand Down Expand Up @@ -181,13 +182,13 @@ export function externals(opts: NodeExternalsOptions): Plugin {
const packageJSONCache = new Map(); // pkgDir => contents
const getPackageJson = async (pkgDir: string) => {
if (packageJSONCache.has(pkgDir)) {
return packageJSONCache.get(pkgDir);
return packageJSONCache.get(pkgDir) as PackageJson;
}
const pkgJSON = JSON.parse(
await fsp.readFile(resolve(pkgDir, "package.json"), "utf8")
);
packageJSONCache.set(pkgDir, pkgJSON);
return pkgJSON;
return pkgJSON as PackageJson;
};

// Resolve traced files
Expand Down Expand Up @@ -239,7 +240,7 @@ export function externals(opts: NodeExternalsOptions): Plugin {
versions: Record<
string,
{
pkgJSON: Record<string, any>;
pkgJSON: PackageJson;
path: string;
files: string[];
}
Expand All @@ -256,7 +257,7 @@ export function externals(opts: NodeExternalsOptions): Plugin {
() => {} // TODO: Only catch ENOENT
);
if (!pkgJSON) {
pkgJSON = { name: pkgName, version: "0.0.0" };
pkgJSON = <PackageJson>{ name: pkgName, version: "0.0.0" };
}
if (!tracedPackage) {
tracedPackage = {
Expand Down Expand Up @@ -301,6 +302,8 @@ export function externals(opts: NodeExternalsOptions): Plugin {
}

// Copy package.json
const pkgJSON = pkg.versions[version].pkgJSON;
applyProductionCondition(pkgJSON.exports);
const pkgJSONPath = join(
opts.outDir,
"node_modules",
Expand All @@ -310,7 +313,7 @@ export function externals(opts: NodeExternalsOptions): Plugin {
await fsp.mkdir(dirname(pkgJSONPath), { recursive: true });
await fsp.writeFile(
pkgJSONPath,
JSON.stringify(pkg.versions[version].pkgJSON, null, 2),
JSON.stringify(pkgJSON, null, 2),
"utf8"
);
};
Expand Down Expand Up @@ -434,6 +437,22 @@ function parseNodeModulePath(path: string) {
};
}

export function applyProductionCondition(exports: PackageJson["exports"]) {
if (!exports || typeof exports === "string") {
return;
}
if (exports.production) {
if (typeof exports.production === "string") {
exports.default = exports.production;
} else {
Object.assign(exports, exports.production);
}
}
for (const key in exports) {
applyProductionCondition(exports[key]);
}
}

async function isFile(file: string) {
try {
const stat = await fsp.stat(file);
Expand Down
85 changes: 85 additions & 0 deletions test/unit/externals.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { expect, describe, it } from "vitest";
import { applyProductionCondition } from "../../src/rollup/plugins/externals";

describe("externals:applyProductionCondition", () => {
const applyProductionConditionCases = [
{
name: "[email protected]",
in: {
".": {
types: "./dist/vue-router.d.ts",
node: {
import: {
production: "./dist/vue-router.node.mjs",
development: "./dist/vue-router.node.mjs",
default: "./dist/vue-router.node.mjs",
pi0 marked this conversation as resolved.
Show resolved Hide resolved
},
require: {
production: "./dist/vue-router.prod.cjs",
development: "./dist/vue-router.cjs",
default: "./index.js",
},
},
import: "./dist/vue-router.mjs",
require: "./index.js",
},
"./dist/*": "./dist/*",
"./vetur/*": "./vetur/*",
"./package.json": "./package.json",
},
out: {
".": {
types: "./dist/vue-router.d.ts",
node: {
import: {
production: "./dist/vue-router.node.mjs",
development: "./dist/vue-router.node.mjs",
default: "./dist/vue-router.node.mjs",
},
require: {
production: "./dist/vue-router.prod.cjs",
development: "./dist/vue-router.cjs",
default: "./dist/vue-router.prod.cjs",
},
},
import: "./dist/vue-router.mjs",
require: "./index.js",
},
"./dist/*": "./dist/*",
"./vetur/*": "./vetur/*",
"./package.json": "./package.json",
},
},
{
name: "[email protected]",
in: {
".": {
production: {
require: "./dist/prod/index.cjs",
import: "./dist/prod/index.mjs",
},
types: "./index.d.ts",
require: "./dist/index.cjs",
import: "./dist/index.mjs",
},
},
out: {
".": {
import: "./dist/prod/index.mjs",
production: {
import: "./dist/prod/index.mjs",
require: "./dist/prod/index.cjs",
},
require: "./dist/prod/index.cjs",
types: "./index.d.ts",
},
},
},
];
for (const t of applyProductionConditionCases) {
it(t.name, () => {
applyProductionCondition(t.in as any);
expect(t.in).toEqual(t.out);
});
}
});