Skip to content

Commit f134d01

Browse files
fix: support deno packages
1 parent 6714033 commit f134d01

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

.changeset/wicked-dragons-melt.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@vanilla-extract/integration': patch
3+
---
4+
5+
Support Deno projects without a `package.json` file.
6+
7+
When Deno is detected, we'll first check for `deno.json` or `deno.jsonc` before falling back to `package.json`.

packages/integration/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"eval": "0.1.8",
2525
"find-up": "^5.0.0",
2626
"javascript-stringify": "^2.0.1",
27+
"json5": "^2.2.3",
2728
"mlly": "^1.4.2"
2829
},
2930
"devDependencies": {

packages/integration/src/packageInfo.ts

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'path';
2-
2+
import json5 from 'json5';
3+
import fs from 'node:fs';
34
import findUp from 'find-up';
45

56
export interface PackageInfo {
@@ -8,7 +9,7 @@ export interface PackageInfo {
89
dirname: string;
910
}
1011

11-
function getClosestPackageInfo(directory: string) {
12+
function getClosestPackageInfo(directory: string): PackageInfo | undefined {
1213
const packageJsonPath = findUp.sync('package.json', {
1314
cwd: directory,
1415
});
@@ -24,6 +25,23 @@ function getClosestPackageInfo(directory: string) {
2425
}
2526
}
2627

28+
function getClosestDenoJson(directory: string): PackageInfo | undefined {
29+
const denoJsonPath = findUp.sync(['deno.json', 'deno.jsonc'], {
30+
cwd: directory,
31+
});
32+
33+
if (denoJsonPath) {
34+
const { name } = json5.parse(fs.readFileSync(denoJsonPath, 'utf-8'));
35+
36+
return {
37+
name,
38+
path: denoJsonPath,
39+
dirname: path.dirname(denoJsonPath),
40+
};
41+
}
42+
}
43+
44+
const isDeno = typeof process.versions.deno === 'string';
2745
const packageInfoCache = new Map<string, PackageInfo>();
2846

2947
export function getPackageInfo(cwd?: string | null): PackageInfo {
@@ -34,15 +52,34 @@ export function getPackageInfo(cwd?: string | null): PackageInfo {
3452
return cachedValue;
3553
}
3654

37-
let packageInfo = getClosestPackageInfo(resolvedCwd);
55+
let packageInfo: PackageInfo | undefined;
3856

39-
while (packageInfo && !packageInfo.name) {
40-
packageInfo = getClosestPackageInfo(
41-
path.resolve(packageInfo.dirname, '..'),
42-
);
57+
// Deno projects don't necessarily have a package.json
58+
if (isDeno) {
59+
packageInfo = getClosestDenoJson(resolvedCwd);
60+
61+
// No need to look further as nested deno.json files is not supported
62+
// in Deno without being a workspace member. If they are a workspace
63+
// member, they are expected to have the `name` field anyway.
64+
}
65+
66+
if (!packageInfo) {
67+
packageInfo = getClosestPackageInfo(resolvedCwd);
68+
69+
while (packageInfo && !packageInfo.name) {
70+
packageInfo = getClosestPackageInfo(
71+
path.resolve(packageInfo.dirname, '..'),
72+
);
73+
}
4374
}
4475

4576
if (!packageInfo || !packageInfo.name) {
77+
if (isDeno) {
78+
throw new Error(
79+
`Couldn't find parent deno.json, deno.jsonc or package.json with a name field from '${resolvedCwd}'`,
80+
);
81+
}
82+
4683
throw new Error(
4784
`Couldn't find parent package.json with a name field from '${resolvedCwd}'`,
4885
);

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)