Skip to content

Commit

Permalink
feat(swc-plugins): Add mapping from swc_plugin_runner to swc_core (
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Aug 27, 2024
1 parent c8b6f61 commit ce7b5a2
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 33 deletions.
12 changes: 12 additions & 0 deletions swc-plugins/app/api/versions/from-plugin-runner/[version]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default async function Page({
params: { version },
}: {
params: { version: string };
}) {
// TODO: Reverse mapping from plugin runner version to swc version
return (
<div>
<h1>swc_plugin_runner: {version}</h1>
</div>
);
}
2 changes: 1 addition & 1 deletion swc-plugins/app/import/ranges/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default async function Page() {
});
}

const runtimes = ["@swc/core", "next", "rspack"];
const runtimes = ["@swc/core", "next", "rspack", "farm"];

for (const runtime of runtimes) {
await db.swcRuntime.upsert({
Expand Down
44 changes: 25 additions & 19 deletions swc-plugins/app/import/runtime/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ export async function POST(req: NextRequest) {
});
const api = await createCaller();

const items: {
runtimeId: bigint;
version: string;
compatRangeId: bigint;
swcCoreVersion: string;
}[] = [];

for (const version of versions) {
const compatRange = await api.compatRange.byVersion({
version: version.swcCoreVersion,
Expand All @@ -50,20 +43,33 @@ export async function POST(req: NextRequest) {
continue;
}

items.push({
runtimeId: rt.id,
// Just to ensure it's a valid semver
version: version.version.replace("v", ""),
compatRangeId: compatRange.id,
// Just to ensure it's a valid semver
swcCoreVersion: version.swcCoreVersion.replace("v", ""),
});
try {
await db.swcRuntimeVersion.upsert({
where: {
runtimeId_version: {
runtimeId: rt.id,
version: version.version.replace("v", ""),
},
},
update: {
compatRangeId: compatRange.id,
swcCoreVersion: version.swcCoreVersion.replace("v", ""),
},
create: {
runtimeId: rt.id,
version: version.version.replace("v", ""),
compatRangeId: compatRange.id,
swcCoreVersion: version.swcCoreVersion.replace("v", ""),
},
});
} catch (e) {
console.error(
`Failed to create compat range for ${version.swcCoreVersion}: ${e}`
);
continue;
}
}

await db.swcRuntimeVersion.createMany({
data: items,
});

return NextResponse.json({
ok: true,
});
Expand Down
5 changes: 4 additions & 1 deletion swc-plugins/lib/api/compatRange/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ export const compatRangeRouter = router({
});

for (const range of versions) {
if (semver.gt(version, range.from) && semver.lt(version, range.to)) {
if (
semver.gt(version, range.from) &&
(range.to === "*" || semver.lt(version, range.to))
) {
return range;
}
}
Expand Down
22 changes: 12 additions & 10 deletions swc-plugins/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,18 @@ model SwcRuntime {
}

model SwcRuntimeVersion {
id BigInt @id @default(autoincrement())
runtime SwcRuntime @relation(fields: [runtimeId], references: [id], onDelete: Cascade)
runtimeId BigInt
version String
/// The version of `swc_core` used by the plugin.
swcCoreVersion String
compatRange CompatRange @relation(fields: [compatRangeId], references: [id])
compatRangeId BigInt
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
id BigInt @id @default(autoincrement())
runtime SwcRuntime @relation(fields: [runtimeId], references: [id], onDelete: Cascade)
runtimeId BigInt
version String
/// The version of `swc_core` used by the runtime.
swcCoreVersion String
/// The version of `swc_plugin_runner` used by the runtime.
swcPluginRunnerVersion String?
compatRange CompatRange @relation(fields: [compatRangeId], references: [id])
compatRangeId BigInt
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([runtimeId, version])
}
Expand Down
4 changes: 2 additions & 2 deletions swc-plugins/scripts/import-runtime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ console.log("Cargo.lock path:", cargoLockPath);
console.log("Relative path to Cargo.lock:", relativePathToCargoLock);

// Get all git tags
const gitTags = (await $$`git tag`.text()).split("\n");
const gitTags = (await $$`git tag`.text()).trim().split("\n").reverse();

const data = {
runtime: runtimeName,
Expand Down Expand Up @@ -62,7 +62,7 @@ for (const tag of gitTags) {
}

// Send the data to the server
if (data.versions.length >= 20) {
if (data.versions.length >= 10) {
await fetch("http://localhost:50000/import/runtime", {
method: "POST",
headers: {
Expand Down

0 comments on commit ce7b5a2

Please sign in to comment.