Skip to content

Commit f1b78a4

Browse files
authored
Remove resolve package and refactor db & studio exports (#11331)
1 parent 9752a0b commit f1b78a4

File tree

8 files changed

+24
-50
lines changed

8 files changed

+24
-50
lines changed

.changeset/selfish-impalas-cough.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@astrojs/studio': patch
3+
'@astrojs/db': patch
4+
---
5+
6+
Relaxes exports condition to allow importing ESM from CJS

.changeset/strong-news-yawn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Removes `resolve` package and simplify internal resolve check

.npmrc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ link-workspace-packages=true
44
save-workspace-protocol=false # This prevents the examples to have the `workspace:` prefix
55
auto-install-peers=false
66

7-
# `github-slugger` is used by `vite-plugin-markdown-legacy`.
8-
# Temporarily hoist this until we remove the feature.
9-
public-hoist-pattern[]=github-slugger
107
# Vite's esbuild optimizer has trouble optimizing `@astrojs/lit/client-shim.js`
118
# which imports this dependency.
129
public-hoist-pattern[]=@webcomponents/template-shadowroot
1310
# There's a lit dependency duplication somewhere causing multiple Lit versions error.
1411
public-hoist-pattern[]=*lit*
12+
# `astro sync` could try to import `@astrojs/db` but could fail due to linked dependencies in the monorepo.
13+
# We hoist it here so that it can easily resolve `@astrojs/db` without hardcoded handling.
14+
public-hoist-pattern[]=@astrojs/db

packages/astro/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@
172172
"preferred-pm": "^3.1.3",
173173
"prompts": "^2.4.2",
174174
"rehype": "^13.0.1",
175-
"resolve": "^1.22.8",
176175
"semver": "^7.6.2",
177176
"shiki": "^1.9.0",
178177
"string-width": "^7.1.0",
@@ -209,7 +208,6 @@
209208
"@types/js-yaml": "^4.0.9",
210209
"@types/probe-image-size": "^7.2.4",
211210
"@types/prompts": "^2.4.9",
212-
"@types/resolve": "^1.20.6",
213211
"@types/semver": "^7.5.8",
214212
"@types/send": "^0.17.4",
215213
"@types/unist": "^3.0.2",

packages/astro/src/cli/install-package.ts

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import { createRequire } from 'node:module';
2-
import { pathToFileURL } from 'node:url';
32
import boxen from 'boxen';
43
import ci from 'ci-info';
54
import { execa } from 'execa';
65
import { bold, cyan, dim, magenta } from 'kleur/colors';
76
import ora from 'ora';
87
import preferredPM from 'preferred-pm';
98
import prompts from 'prompts';
10-
import resolvePackage from 'resolve';
119
import whichPm from 'which-pm';
12-
import { type Logger } from '../core/logger/core.js';
13-
const require = createRequire(import.meta.url);
10+
import type { Logger } from '../core/logger/core.js';
1411

1512
type GetPackageOptions = {
1613
skipAsk?: boolean;
@@ -25,17 +22,9 @@ export async function getPackage<T>(
2522
otherDeps: string[] = []
2623
): Promise<T | undefined> {
2724
try {
28-
// Custom resolution logic for @astrojs/db. Since it lives in our monorepo,
29-
// the generic tryResolve() method doesn't work.
30-
if (packageName === '@astrojs/db') {
31-
const packageJsonLoc = require.resolve(packageName + '/package.json', {
32-
paths: [options.cwd ?? process.cwd()],
33-
});
34-
const packageLoc = pathToFileURL(packageJsonLoc.replace(`package.json`, 'dist/index.js'));
35-
const packageImport = await import(packageLoc.toString());
36-
return packageImport as T;
37-
}
38-
await tryResolve(packageName, options.cwd ?? process.cwd());
25+
// Try to resolve with `createRequire` first to prevent ESM caching of the package
26+
// if it errors and fails here
27+
createRequire(options.cwd ?? process.cwd()).resolve(packageName);
3928
const packageImport = await import(packageName);
4029
return packageImport as T;
4130
} catch (e) {
@@ -65,24 +54,6 @@ export async function getPackage<T>(
6554
}
6655
}
6756

68-
function tryResolve(packageName: string, cwd: string) {
69-
return new Promise((resolve, reject) => {
70-
resolvePackage(
71-
packageName,
72-
{
73-
basedir: cwd,
74-
},
75-
(err) => {
76-
if (err) {
77-
reject(err);
78-
} else {
79-
resolve(0);
80-
}
81-
}
82-
);
83-
});
84-
}
85-
8657
function getInstallCommand(packages: string[], packageManager: string) {
8758
switch (packageManager) {
8859
case 'npm':

packages/db/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@
1717
"exports": {
1818
".": {
1919
"types": "./index.d.ts",
20-
"import": "./dist/index.js"
20+
"default": "./dist/index.js"
2121
},
2222
"./utils": {
2323
"types": "./dist/utils.d.ts",
24-
"import": "./dist/utils.js"
24+
"default": "./dist/utils.js"
2525
},
2626
"./runtime": {
2727
"types": "./dist/runtime/index.d.ts",
28-
"import": "./dist/runtime/index.js"
28+
"default": "./dist/runtime/index.js"
2929
},
3030
"./dist/runtime/virtual.js": {
31-
"import": "./dist/runtime/virtual.js"
31+
"default": "./dist/runtime/virtual.js"
3232
},
3333
"./types": {
3434
"types": "./dist/core/types.d.ts",
35-
"import": "./dist/core/types.js"
35+
"default": "./dist/core/types.js"
3636
},
3737
"./package.json": "./package.json"
3838
},

packages/studio/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"exports": {
1818
".": {
1919
"types": "./dist/index.d.ts",
20-
"import": "./dist/index.js"
20+
"default": "./dist/index.js"
2121
},
2222
"./package.json": "./package.json"
2323
},

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)