Skip to content

Commit

Permalink
Use workspace protocol for theme peer dependency (#4372)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwooding authored Nov 4, 2024
1 parent 26bf747 commit 0e61b6e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 36 deletions.
2 changes: 1 addition & 1 deletion packages/ag-grid-theme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
"del": "^7.0.0"
},
"peerDependencies": {
"@salt-ds/theme": "^1.23.0"
"@salt-ds/theme": "workspace:^"
}
}
14 changes: 13 additions & 1 deletion packages/ag-grid-theme/scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { argv } from "node:process";
import { deleteSync } from "del";
import esbuild from "esbuild";
import fs from "fs-extra";
import { transformWorkspaceDeps } from "../../../scripts/transformWorkspaceDeps.mjs";

const FILES_TO_COPY = ["README.md", "LICENSE", "CHANGELOG.md", "package.json"];
const FILES_TO_COPY = ["README.md", "LICENSE", "CHANGELOG.md"];

const cwd = process.cwd();
const packageJson = (
Expand Down Expand Up @@ -39,6 +40,17 @@ if (argv.includes("--watch")) {
await context.dispose();
}

await fs.writeJSON(
path.join(buildFolder, "package.json"),
{
...packageJson,
peerDependencies: await transformWorkspaceDeps(
packageJson.peerDependencies,
),
},
{ spaces: 2 },
);

for (const file of FILES_TO_COPY) {
const from = path.join(cwd, file);
const to = path.join(buildFolder, file);
Expand Down
35 changes: 2 additions & 33 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { rollup } from "rollup";
import esbuild from "rollup-plugin-esbuild";
import postcss from "rollup-plugin-postcss";
import { makeTypings } from "./makeTypings.mjs";
import { transformWorkspaceDeps } from "./transformWorkspaceDeps.mjs";
import { distinct } from "./utils.mjs";

const cwd = process.cwd();
Expand Down Expand Up @@ -99,43 +100,11 @@ await bundle.write({

await bundle.close();

const workspacePackages = Object.entries(packageJson.dependencies ?? {}).filter(
([, version]) => version.match(/^workspace:/),
);

const workspaceDependencies = {};

for (const [name, version] of workspacePackages) {
// strip workspace:
const strippedVersion = version.slice(10);

if (
strippedVersion !== "~" &&
strippedVersion !== "*" &&
strippedVersion !== "^"
) {
workspaceDependencies[name] = strippedVersion;
continue;
}

const packageJson = (
await import(`${name}/package.json`, {
with: { type: "json" },
})
).default;

workspaceDependencies[name] =
`${strippedVersion !== "*" ? strippedVersion : ""}${packageJson.version}`;
}

await fs.writeJSON(
path.join(outputDir, "package.json"),
{
...packageJson,
dependencies: {
...packageJson.dependencies,
...workspaceDependencies,
},
dependencies: await transformWorkspaceDeps(packageJson.dependencies),
main: "dist-cjs/index.js",
module: "dist-es/index.js",
typings: "dist-types/index.d.ts",
Expand Down
35 changes: 35 additions & 0 deletions scripts/transformWorkspaceDeps.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export async function transformWorkspaceDeps(dependencies = {}) {
const workspacePackages = Object.entries(dependencies).filter(([, version]) =>
version.match(/^workspace:/),
);

const workspaceDependencies = {};

for (const [name, version] of workspacePackages) {
// strip workspace:
const strippedVersion = version.slice(10);

if (
strippedVersion !== "~" &&
strippedVersion !== "*" &&
strippedVersion !== "^"
) {
workspaceDependencies[name] = strippedVersion;
continue;
}

const packageJson = (
await import(`${name}/package.json`, {
with: { type: "json" },
})
).default;

workspaceDependencies[name] =
`${strippedVersion !== "*" ? strippedVersion : ""}${packageJson.version}`;
}

return {
...dependencies,
...workspaceDependencies,
};
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3332,7 +3332,7 @@ __metadata:
dependencies:
del: "npm:^7.0.0"
peerDependencies:
"@salt-ds/theme": ^1.23.0
"@salt-ds/theme": "workspace:^"
languageName: unknown
linkType: soft

Expand Down

0 comments on commit 0e61b6e

Please sign in to comment.