From f1b78a496034d53b0e9dfc276a4a1b1d691772c4 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Wed, 26 Jun 2024 17:19:16 +0800 Subject: [PATCH] Remove resolve package and refactor db & studio exports (#11331) --- .changeset/selfish-impalas-cough.md | 6 ++++ .changeset/strong-news-yawn.md | 5 +++ .npmrc | 6 ++-- packages/astro/package.json | 2 -- packages/astro/src/cli/install-package.ts | 37 +++-------------------- packages/db/package.json | 10 +++--- packages/studio/package.json | 2 +- pnpm-lock.yaml | 6 ---- 8 files changed, 24 insertions(+), 50 deletions(-) create mode 100644 .changeset/selfish-impalas-cough.md create mode 100644 .changeset/strong-news-yawn.md diff --git a/.changeset/selfish-impalas-cough.md b/.changeset/selfish-impalas-cough.md new file mode 100644 index 000000000000..50ec0ef309ec --- /dev/null +++ b/.changeset/selfish-impalas-cough.md @@ -0,0 +1,6 @@ +--- +'@astrojs/studio': patch +'@astrojs/db': patch +--- + +Relaxes exports condition to allow importing ESM from CJS diff --git a/.changeset/strong-news-yawn.md b/.changeset/strong-news-yawn.md new file mode 100644 index 000000000000..e3d828d8d03b --- /dev/null +++ b/.changeset/strong-news-yawn.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Removes `resolve` package and simplify internal resolve check diff --git a/.npmrc b/.npmrc index f44bad2825c8..4912828b6bdd 100644 --- a/.npmrc +++ b/.npmrc @@ -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 diff --git a/packages/astro/package.json b/packages/astro/package.json index b939cde064c1..abb3bd7ad2c5 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -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", @@ -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", diff --git a/packages/astro/src/cli/install-package.ts b/packages/astro/src/cli/install-package.ts index 8b20f3719b90..e7534e77d39a 100644 --- a/packages/astro/src/cli/install-package.ts +++ b/packages/astro/src/cli/install-package.ts @@ -1,5 +1,4 @@ import { createRequire } from 'node:module'; -import { pathToFileURL } from 'node:url'; import boxen from 'boxen'; import ci from 'ci-info'; import { execa } from 'execa'; @@ -7,10 +6,8 @@ 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; @@ -25,17 +22,9 @@ export async function getPackage( otherDeps: string[] = [] ): Promise { 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) { @@ -65,24 +54,6 @@ export async function getPackage( } } -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': diff --git a/packages/db/package.json b/packages/db/package.json index 1de1ba0d7865..62edbe2161d9 100644 --- a/packages/db/package.json +++ b/packages/db/package.json @@ -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" }, diff --git a/packages/studio/package.json b/packages/studio/package.json index 1615b85b7346..a8d36bafcd9a 100644 --- a/packages/studio/package.json +++ b/packages/studio/package.json @@ -17,7 +17,7 @@ "exports": { ".": { "types": "./dist/index.d.ts", - "import": "./dist/index.js" + "default": "./dist/index.js" }, "./package.json": "./package.json" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0c646b5c5d14..befae9b98c6a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -675,9 +675,6 @@ importers: rehype: specifier: ^13.0.1 version: 13.0.1 - resolve: - specifier: ^1.22.8 - version: 1.22.8 semver: specifier: ^7.6.2 version: 7.6.2 @@ -776,9 +773,6 @@ importers: '@types/prompts': specifier: ^2.4.9 version: 2.4.9 - '@types/resolve': - specifier: ^1.20.6 - version: 1.20.6 '@types/semver': specifier: ^7.5.8 version: 7.5.8