8000 chore: remove import-meta-resolve (#13629) · sveltejs/kit@bd1c046 · GitHub
[go: up one dir, main page]

Skip to content

Commit bd1c046

Browse files
authored
chore: remove import-meta-resolve (#13629)
* remove import-meta-resolve * changeset
1 parent dfb4b9e commit bd1c046

File tree

8 files changed

+127
-85
lines changed

8 files changed

+127
-85
lines changed

.changeset/famous-points-switch.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@sveltejs/adapter-auto': patch
3+
'@sveltejs/kit': patch
4+
---
5+
6+
chore: remove `import-meta-resolve` dependency

packages/adapter-auto/index.js

Lines changed: 75 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { execSync } from 'node:child_process';
2-
import { pathToFileURL } from 'node:url';
3-
import { resolve } from 'import-meta-resolve';
42
import { adapters } from './adapters.js';
5-
import { dirname, join } from 'node:path';
6-
import { existsSync } from 'node:fs';
3+
import path from 'node:path';
4+
import fs from 'node:fs';
75
import process from 'node:process';
86

97
/** @type {Record<string, (name: string, version: string) => string>} */
@@ -17,12 +15,15 @@ const commands = {
1715
function detect_lockfile() {
1816
let dir = process.cwd();
1917

18+
/** @param {string} file */
19+
const exists = (file) => fs.existsSync(path.join(dir, file));
20+
2021
do {
21-
if (existsSync(join(dir, 'pnpm-lock.yaml'))) return 'pnpm';
22-
if (existsSync(join(dir, 'yarn.lock'))) return 'yarn';
23-
if (existsSync(join(dir, 'package-lock.json'))) return 'npm';
24-
if (existsSync(join(dir, 'bun.lockb')) || existsSync(join(dir, 'bun.lock'))) return 'bun';
25-
} while (dir !== (dir = dirname(dir)));
22+
if (exists('pnpm-lock.yaml')) return 'pnpm';
23+
if (exists('yarn.lock')) return 'yarn';
24+
if (exists('package-lock.json')) return 'npm';
25+
if (exists('bun.lockb') || exists('bun.lock')) return 'bun';
26+
} while (dir !== (dir = path.dirname(dir)));
2627

2728
return 'npm';
2829
}
@@ -38,12 +39,40 @@ function detect_package_manager() {
3839
}
3940
}
4041

41-
/** @param {string} name */
42-
function import_from_cwd(name) {
43-
const cwd = pathToFileURL(process.cwd()).href;
44-
const url = resolve(name, cwd + '/x.js');
42+
/**
43+
* Resolves a peer dependency relative to the current CWD. Duplicated with `packages/kit`
44+
* @param {string} dependency
45+
*/
46+
function resolve_peer(dependency) {
47+
let [name, ...parts] = dependency.split('/');
48+
if (name[0] === '@') name += `/${parts.shift()}`;
49+
50+
let dir = process.cwd();
51+
52+
while (!fs.existsSync(`${dir}/node_modules/${name}/package.json`)) {
53+
if (dir === (dir = path.dirname(dir))) {
54+
throw new Error(
55+
`Could not resolve peer dependency "${name}" relative to your project — please install it and try again.`
56+
);
57+
}
58+
}
59+
60+
const pkg_dir = `${dir}/node_modules/${name}`;
61+
const pkg = JSON.parse(fs.readFileSync(`${pkg_dir}/package.json`, 'utf-8'));
62+
63+
const subpackage = ['.', ...parts].join('/');
64+
65+
let exported = pkg.exports[subpackage];
66+
67+
while (typeof exported !== 'string') {
68+
if (!exported) {
69+
throw new Error(`Could not find valid "${subpackage}" export in ${name}/package.json`);
70+
}
71+
72+
exported = exported['import'] ?? exported['default'];
73+
}
4574

46-
return import(url);
75+
return path.resolve(pkg_dir, exported);
4776
}
4877

4978
/** @typedef {import('@sveltejs/kit').Adapter} Adapter */
@@ -56,47 +85,43 @@ async function get_adapter() {
5685

5786
if (!match) return;
5887

59-
/** @type {{ default: () => Adapter }} */
60-
let module;
88+
/** @type {string} */
89+
let resolved;
6190

6291
try {
63-
module = await import_from_cwd(match.module);
64-
} catch (error) {
65-
if (
66-
error.code === 'ERR_MODULE_NOT_FOUND' &&
67-
error.message.startsWith(`Cannot find package '${match.module}'`)
68-
) {
69-
const package_manager = detect_package_manager();
70-
const command = commands[package_manager](match.module, match.version);
71-
72-
try {
73-
console.log(`Installing ${match.module}...`);
74-
75-
execSync(command, {
76-
stdio: 'inherit',
77-
env: {
78-
...process.env,
79-
NODE_ENV: undefined
80-
}
81-
});
82-
83-
module = await import_from_cwd(match.module);
84-
85-
console.log(`Successfully installed ${match.module}.`);
86-
console.warn(
87-
`\nIf you plan on staying on this deployment platform, consider replacing @sveltejs/adapter-auto with ${match.module}. This will give you faster and more robust installs, and more control over deployment configuration.\n`
88-
);
89-
} catch (e) {
90-
throw new Error(
91-
`Could not install ${match.module}. Please install it yourself by adding it to your package.json's devDependencies and try building your project again.`,
92-
{ cause: e }
93-
);
94-
}
95-
} else {
96-
throw error;
92+
resolved = resolve_peer(match.module);
93+
} catch {
94+
const package_manager = detect_package_manager();
95+
const command = commands[package_manager](match.module, match.version);
96+
97+
try {
98+
console.log(`Installing ${match.module}...`);
99+
100+
execSync(command, {
101+
stdio: 'inherit',
102+
env: {
103+
...process.env,
104+
NODE_ENV: undefined
105+
}
106+
});
107+
108+
resolved = resolve_peer(match.module);
109+
110+
console.log(`Successfully installed ${match.module}.`);
111+
console.warn(
112+
`\nIf you plan on staying on this deployment platform, consider replacing @sveltejs/adapter-auto with ${match.module}. This will give you faster and more robust installs, and more control over deployment configuration.\n`
113+
);
114+
} catch (e) {
115+
throw new Error(
116+
`Could not install ${match.module}. Please install it yourself by adding it to your package.json's devDependencies and try building your project again.`,
117+
{ cause: e }
118+
);
97119
}
98120
}
99121

122+
/** @type {{ default: () => Adapter }} */
123+
const module = await import(resolved);
124+
100125
const adapter = module.default();
101126

102127
return {

packages/adapter-auto/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@
4646
"typescript": "^5.3.3",
4747
"vitest": "^3.1.1"
4848
},
49-
"dependencies": {
50-
"import-meta-resolve": "^4.1.0"
51-
},
5249
"peerDependencies": {
5350
"@sveltejs/kit": "^2.0.0"
5451
}

packages/kit/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"cookie": "^0.6.0",
2323
"devalue": "^5.1.0",
2424
"esm-env": "^1.2.2",
25-
"import-meta-resolve": "^4.1.0",
2625
"kleur": "^4.1.5",
2726
"magic-string": "^0.30.5",
2827
"mrmime": "^2.0.0",

packages/kit/src/core/sync/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
33
import { mkdirp } from '../../utils/filesystem.js';
4-
import { resolve_peer_dependency } from '../../utils/import.js';
4+
import { import_peer } from '../../utils/import.js';
55

66
/** @type {{ VERSION: string }} */
7-
const { VERSION } = await resolve_peer_dependency('svelte/compiler');
7+
const { VERSION } = await import_peer('svelte/compiler');
88

99
/** @type {Map<string, string>} */
1010
const previous_contents = new Map();

packages/kit/src/exports/vite/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
sveltekit_paths,
3535
sveltekit_server
3636
} from './module_ids.js';
37-
import { resolve_peer_dependency } from '../../utils/import.js';
37+
import { import_peer } from '../../utils/import.js';
3838
import { compact } from '../../utils/array.js';
3939

4040
const cwd = process.cwd();
@@ -155,7 +155,7 @@ export async function sveltekit() {
155155
...svelte_config.vitePlugin
156156
};
157157

158-
const { svelte } = await resolve_peer_dependency('@sveltejs/vite-plugin-svelte');
158+
const { svelte } = await import_peer('@sveltejs/vite-plugin-svelte');
159159

160160
return [...svelte(vite_plugin_svelte_options), ...(await kit({ svelte_config }))];
161161
}
@@ -181,7 +181,7 @@ let manifest_data;
181181
* @return {Promise<import('vite').Plugin[]>}
182182
*/
183183
async function kit({ svelte_config }) {
184-
const vite = await resolve_peer_dependency('vite');
184+
const vite = await import_peer('vite');
185185

186186
const { kit } = svelte_config;
187187
const out = `${kit.outDir}/output`;

packages/kit/src/utils/import.js

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,52 @@
1-
import * as imr from 'import-meta-resolve';
21
import process from 'node:process';
3-
import { pathToFileURL } from 'node:url';
2+
import fs from 'node:fs';
3+
import path from 'node:path';
4+
5+
/**
6+
* Resolves a peer dependency relative to the current CWD. Duplicated with `packages/adapter-auto`
7+
* @param {string} dependency
8+
*/
9+
function resolve_peer(dependency) {
10+
let [name, ...parts] = dependency.split('/');
11+
if (name[0] === '@') name += `/${parts.shift()}`;
12+
13+
let dir = process.cwd();
14+
15+
while (!fs.existsSync(`${dir}/node_modules/${name}/package.json`)) {
16+
if (dir === (dir = path.dirname(dir))) {
17+
throw new Error(
18+
`Could not resolve peer dependency "${name}" relative to your project — please install it and try again.`
19+
);
20+
}
21+
}
22+
23+
const pkg_dir = `${dir}/node_modules/${name}`;
24+
const pkg = JSON.parse(fs.readFileSync(`${pkg_dir}/package.json`, 'utf-8'));
25+
26+
const subpackage = ['.', ...parts].join('/');
27+
28+
let exported = pkg.exports[subpackage];
29+
30+
while (typeof exported !== 'string') {
31+
if (!exported) {
32+
throw new Error(`Could not find valid "${subpackage}" export in ${name}/package.json`);
33+
}
34+
35+
exported = exported['import'] ?? exported['default'];
36+
}
37+
38+
return path.resolve(pkg_dir, exported);
39+
}
440

541
/**
642
* Resolve a dependency relative to the current working directory,
743
* rather than relative to this package (but falls back to trying that, if necessary)
844
* @param {string} dependency
945
*/
10-
export async function resolve_peer_dependency(dependency) {
46+
export async function import_peer(dependency) {
1147
try {
12-
// @ts-expect-error the types are wrong
13-
const resolved = imr.resolve(dependency, pathToFileURL(process.cwd() + '/dummy.js'));
14-
return await import(resolved);
48+
return await import(resolve_peer(dependency));
1549
} catch {
16-
try {
17-
// both imr.resolve and await import above can throw, which is why we can't just do import(resolved).catch(...) above
18-
return await import(dependency);
19-
} catch {
20-
throw new Error(
21-
`Could not resolve peer dependency "${dependency}" relative to your project — please install it and try again.`
22-
);
23-
}
50+
return await import(dependency);
2451
}
2552
}

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)
0