Skip to content

Commit

Permalink
Remove resolve package and refactor db & studio exports (#11331)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Jun 26, 2024
1 parent 9752a0b commit f1b78a4
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 50 deletions.
6 changes: 6 additions & 0 deletions .changeset/selfish-impalas-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@astrojs/studio': patch
'@astrojs/db': patch
---

Relaxes exports condition to allow importing ESM from CJS
5 changes: 5 additions & 0 deletions .changeset/strong-news-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Removes `resolve` package and simplify internal resolve check
6 changes: 3 additions & 3 deletions .npmrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ link-workspace-packages=true
save-workspace-protocol=false # This prevents the examples to have the `workspace:` prefix
auto-install-peers=false

# `github-slugger` is used by `vite-plugin-markdown-legacy`.
# Temporarily hoist this until we remove the feature.
public-hoist-pattern[]=github-slugger
# Vite's esbuild optimizer has trouble optimizing `@astrojs/lit/client-shim.js`
# which imports this dependency.
public-hoist-pattern[]=@webcomponents/template-shadowroot
# There's a lit dependency duplication somewhere causing multiple Lit versions error.
public-hoist-pattern[]=*lit*
# `astro sync` could try to import `@astrojs/db` but could fail due to linked dependencies in the monorepo.
# We hoist it here so that it can easily resolve `@astrojs/db` without hardcoded handling.
public-hoist-pattern[]=@astrojs/db
2 changes: 0 additions & 2 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@
"preferred-pm": "^3.1.3",
"prompts": "^2.4.2",
"rehype": "^13.0.1",
"resolve": "^1.22.8",
"semver": "^7.6.2",
"shiki": "^1.9.0",
"string-width": "^7.1.0",
Expand Down Expand Up @@ -209,7 +208,6 @@
"@types/js-yaml": "^4.0.9",
"@types/probe-image-size": "^7.2.4",
"@types/prompts": "^2.4.9",
"@types/resolve": "^1.20.6",
"@types/semver": "^7.5.8",
"@types/send": "^0.17.4",
"@types/unist": "^3.0.2",
Expand Down
37 changes: 4 additions & 33 deletions packages/astro/src/cli/install-package.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { createRequire } from 'node:module';
import { pathToFileURL } from 'node:url';
import boxen from 'boxen';
import ci from 'ci-info';
import { execa } from 'execa';
import { bold, cyan, dim, magenta } from 'kleur/colors';
import ora from 'ora';
import preferredPM from 'preferred-pm';
import prompts from 'prompts';
import resolvePackage from 'resolve';
import whichPm from 'which-pm';
import { type Logger } from '../core/logger/core.js';
const require = createRequire(import.meta.url);
import type { Logger } from '../core/logger/core.js';

type GetPackageOptions = {
skipAsk?: boolean;
Expand All @@ -25,17 +22,9 @@ export async function getPackage<T>(
otherDeps: string[] = []
): Promise<T | undefined> {
try {
// Custom resolution logic for @astrojs/db. Since it lives in our monorepo,
// the generic tryResolve() method doesn't work.
if (packageName === '@astrojs/db') {
const packageJsonLoc = require.resolve(packageName + '/package.json', {
paths: [options.cwd ?? process.cwd()],
});
const packageLoc = pathToFileURL(packageJsonLoc.replace(`package.json`, 'dist/index.js'));
const packageImport = await import(packageLoc.toString());
return packageImport as T;
}
await tryResolve(packageName, options.cwd ?? process.cwd());
// Try to resolve with `createRequire` first to prevent ESM caching of the package
// if it errors and fails here
createRequire(options.cwd ?? process.cwd()).resolve(packageName);
const packageImport = await import(packageName);
return packageImport as T;
} catch (e) {
Expand Down Expand Up @@ -65,24 +54,6 @@ export async function getPackage<T>(
}
}

function tryResolve(packageName: string, cwd: string) {
return new Promise((resolve, reject) => {
resolvePackage(
packageName,
{
basedir: cwd,
},
(err) => {
if (err) {
reject(err);
} else {
resolve(0);
}
}
);
});
}

function getInstallCommand(packages: string[], packageManager: string) {
switch (packageManager) {
case 'npm':
Expand Down
10 changes: 5 additions & 5 deletions packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@
"exports": {
".": {
"types": "./index.d.ts",
"import": "./dist/index.js"
"default": "./dist/index.js"
},
"./utils": {
"types": "./dist/utils.d.ts",
"import": "./dist/utils.js"
"default": "./dist/utils.js"
},
"./runtime": {
"types": "./dist/runtime/index.d.ts",
"import": "./dist/runtime/index.js"
"default": "./dist/runtime/index.js"
},
"./dist/runtime/virtual.js": {
"import": "./dist/runtime/virtual.js"
"default": "./dist/runtime/virtual.js"
},
"./types": {
"types": "./dist/core/types.d.ts",
"import": "./dist/core/types.js"
"default": "./dist/core/types.js"
},
"./package.json": "./package.json"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
"default": "./dist/index.js"
},
"./package.json": "./package.json"
},
Expand Down
6 changes: 0 additions & 6 deletions pnpm-lock.yaml

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

0 comments on commit f1b78a4

Please sign in to comment.