0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Remove resolve package and refactor db & studio exports (#11331)

This commit is contained in:
Bjorn Lu 2024-06-26 17:19:16 +08:00 committed by GitHub
parent 9752a0b275
commit f1b78a4960
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 24 additions and 50 deletions

View file

@ -0,0 +1,6 @@
---
'@astrojs/studio': patch
'@astrojs/db': patch
---
Relaxes exports condition to allow importing ESM from CJS

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Removes `resolve` package and simplify internal resolve check

6
.npmrc
View file

@ -4,11 +4,11 @@ link-workspace-packages=true
save-workspace-protocol=false # This prevents the examples to have the `workspace:` prefix save-workspace-protocol=false # This prevents the examples to have the `workspace:` prefix
auto-install-peers=false 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` # Vite's esbuild optimizer has trouble optimizing `@astrojs/lit/client-shim.js`
# which imports this dependency. # which imports this dependency.
public-hoist-pattern[]=@webcomponents/template-shadowroot public-hoist-pattern[]=@webcomponents/template-shadowroot
# There's a lit dependency duplication somewhere causing multiple Lit versions error. # There's a lit dependency duplication somewhere causing multiple Lit versions error.
public-hoist-pattern[]=*lit* 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

View file

@ -172,7 +172,6 @@
"preferred-pm": "^3.1.3", "preferred-pm": "^3.1.3",
"prompts": "^2.4.2", "prompts": "^2.4.2",
"rehype": "^13.0.1", "rehype": "^13.0.1",
"resolve": "^1.22.8",
"semver": "^7.6.2", "semver": "^7.6.2",
"shiki": "^1.9.0", "shiki": "^1.9.0",
"string-width": "^7.1.0", "string-width": "^7.1.0",
@ -209,7 +208,6 @@
"@types/js-yaml": "^4.0.9", "@types/js-yaml": "^4.0.9",
"@types/probe-image-size": "^7.2.4", "@types/probe-image-size": "^7.2.4",
"@types/prompts": "^2.4.9", "@types/prompts": "^2.4.9",
"@types/resolve": "^1.20.6",
"@types/semver": "^7.5.8", "@types/semver": "^7.5.8",
"@types/send": "^0.17.4", "@types/send": "^0.17.4",
"@types/unist": "^3.0.2", "@types/unist": "^3.0.2",

View file

@ -1,5 +1,4 @@
import { createRequire } from 'node:module'; import { createRequire } from 'node:module';
import { pathToFileURL } from 'node:url';
import boxen from 'boxen'; import boxen from 'boxen';
import ci from 'ci-info'; import ci from 'ci-info';
import { execa } from 'execa'; import { execa } from 'execa';
@ -7,10 +6,8 @@ import { bold, cyan, dim, magenta } from 'kleur/colors';
import ora from 'ora'; import ora from 'ora';
import preferredPM from 'preferred-pm'; import preferredPM from 'preferred-pm';
import prompts from 'prompts'; import prompts from 'prompts';
import resolvePackage from 'resolve';
import whichPm from 'which-pm'; import whichPm from 'which-pm';
import { type Logger } from '../core/logger/core.js'; import type { Logger } from '../core/logger/core.js';
const require = createRequire(import.meta.url);
type GetPackageOptions = { type GetPackageOptions = {
skipAsk?: boolean; skipAsk?: boolean;
@ -25,17 +22,9 @@ export async function getPackage<T>(
otherDeps: string[] = [] otherDeps: string[] = []
): Promise<T | undefined> { ): Promise<T | undefined> {
try { try {
// Custom resolution logic for @astrojs/db. Since it lives in our monorepo, // Try to resolve with `createRequire` first to prevent ESM caching of the package
// the generic tryResolve() method doesn't work. // if it errors and fails here
if (packageName === '@astrojs/db') { createRequire(options.cwd ?? process.cwd()).resolve(packageName);
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());
const packageImport = await import(packageName); const packageImport = await import(packageName);
return packageImport as T; return packageImport as T;
} catch (e) { } catch (e) {
@ -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) { function getInstallCommand(packages: string[], packageManager: string) {
switch (packageManager) { switch (packageManager) {
case 'npm': case 'npm':

View file

@ -17,22 +17,22 @@
"exports": { "exports": {
".": { ".": {
"types": "./index.d.ts", "types": "./index.d.ts",
"import": "./dist/index.js" "default": "./dist/index.js"
}, },
"./utils": { "./utils": {
"types": "./dist/utils.d.ts", "types": "./dist/utils.d.ts",
"import": "./dist/utils.js" "default": "./dist/utils.js"
}, },
"./runtime": { "./runtime": {
"types": "./dist/runtime/index.d.ts", "types": "./dist/runtime/index.d.ts",
"import": "./dist/runtime/index.js" "default": "./dist/runtime/index.js"
}, },
"./dist/runtime/virtual.js": { "./dist/runtime/virtual.js": {
"import": "./dist/runtime/virtual.js" "default": "./dist/runtime/virtual.js"
}, },
"./types": { "./types": {
"types": "./dist/core/types.d.ts", "types": "./dist/core/types.d.ts",
"import": "./dist/core/types.js" "default": "./dist/core/types.js"
}, },
"./package.json": "./package.json" "./package.json": "./package.json"
}, },

View file

@ -17,7 +17,7 @@
"exports": { "exports": {
".": { ".": {
"types": "./dist/index.d.ts", "types": "./dist/index.d.ts",
"import": "./dist/index.js" "default": "./dist/index.js"
}, },
"./package.json": "./package.json" "./package.json": "./package.json"
}, },

View file

@ -675,9 +675,6 @@ importers:
rehype: rehype:
specifier: ^13.0.1 specifier: ^13.0.1
version: 13.0.1 version: 13.0.1
resolve:
specifier: ^1.22.8
version: 1.22.8
semver: semver:
specifier: ^7.6.2 specifier: ^7.6.2
version: 7.6.2 version: 7.6.2
@ -776,9 +773,6 @@ importers:
'@types/prompts': '@types/prompts':
specifier: ^2.4.9 specifier: ^2.4.9
version: 2.4.9 version: 2.4.9
'@types/resolve':
specifier: ^1.20.6
version: 1.20.6
'@types/semver': '@types/semver':
specifier: ^7.5.8 specifier: ^7.5.8
version: 7.5.8 version: 7.5.8