Skip to content
Open
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
7 changes: 7 additions & 0 deletions .changeset/wicked-dragons-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@vanilla-extract/integration': patch
---

Support Deno projects without a `package.json` file.

When Deno is detected, we'll first check for `deno.json` or `deno.jsonc` before falling back to `package.json`.
1 change: 1 addition & 0 deletions packages/integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"eval": "0.1.8",
"find-up": "^5.0.0",
"javascript-stringify": "^2.0.1",
"json5": "^2.2.3",
"mlly": "^1.4.2"
},
"devDependencies": {
Expand Down
51 changes: 44 additions & 7 deletions packages/integration/src/packageInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path';

import json5 from 'json5';
import fs from 'node:fs';
import findUp from 'find-up';

export interface PackageInfo {
Expand All @@ -8,7 +9,7 @@ export interface PackageInfo {
dirname: string;
}

function getClosestPackageInfo(directory: string) {
function getClosestPackageInfo(directory: string): PackageInfo | undefined {
const packageJsonPath = findUp.sync('package.json', {
cwd: directory,
});
Expand All @@ -24,6 +25,23 @@ function getClosestPackageInfo(directory: string) {
}
}

function getClosestDenoJson(directory: string): PackageInfo | undefined {
const denoJsonPath = findUp.sync(['deno.json', 'deno.jsonc'], {
cwd: directory,
});

if (denoJsonPath) {
const { name } = json5.parse(fs.readFileSync(denoJsonPath, 'utf-8'));

return {
name,
path: denoJsonPath,
dirname: path.dirname(denoJsonPath),
};
}
}

const isDeno = typeof process.versions.deno === 'string';
const packageInfoCache = new Map<string, PackageInfo>();

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

let packageInfo = getClosestPackageInfo(resolvedCwd);
let packageInfo: PackageInfo | undefined;

while (packageInfo && !packageInfo.name) {
packageInfo = getClosestPackageInfo(
path.resolve(packageInfo.dirname, '..'),
);
// Deno projects don't necessarily have a package.json
if (isDeno) {
packageInfo = getClosestDenoJson(resolvedCwd);

// No need to look further as nested deno.json files is not supported
// in Deno without being a workspace member. If they are a workspace
// member, they are expected to have the `name` field anyway.
}

if (!packageInfo) {
packageInfo = getClosestPackageInfo(resolvedCwd);

while (packageInfo && !packageInfo.name) {
packageInfo = getClosestPackageInfo(
path.resolve(packageInfo.dirname, '..'),
);
}
}

if (!packageInfo || !packageInfo.name) {
if (isDeno) {
throw new Error(
`Couldn't find parent deno.json, deno.jsonc or package.json with a name field from '${resolvedCwd}'`,
);
}

throw new Error(
`Couldn't find parent package.json with a name field from '${resolvedCwd}'`,
);
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.