Skip to content

Commit 49ab4f2

Browse files
authored
Remove proload for config loading (#5778)
* Refactor resolve and load config * Add changeset * Update name * Remove unnecessary node_env handling * Fix test * Update comment
1 parent 54d87af commit 49ab4f2

8 files changed

Lines changed: 65 additions & 128 deletions

File tree

.changeset/calm-peas-doubt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': major
3+
---
4+
5+
Remove proload to load the Astro config. It will now use NodeJS and Vite to load the config only.

packages/astro/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@
120120
"@babel/plugin-transform-react-jsx": "^7.17.12",
121121
"@babel/traverse": "^7.18.2",
122122
"@babel/types": "^7.18.4",
123-
"@proload/core": "^0.3.3",
124-
"@proload/plugin-tsm": "^0.2.1",
125123
"@types/babel__core": "^7.1.19",
126124
"@types/html-escaper": "^3.0.0",
127125
"@types/yargs-parser": "^21.0.0",

packages/astro/src/content/utils.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ export async function loadContentConfig({
190190
settings: AstroSettings;
191191
}): Promise<ContentConfig | Error> {
192192
const contentPaths = getContentPaths({ srcDir: settings.config.srcDir });
193-
const nodeEnv = process.env.NODE_ENV;
194193
const tempConfigServer: ViteDevServer = await createServer({
195194
root: fileURLToPath(settings.config.root),
196195
server: { middlewareMode: true, hmr: false },
@@ -207,9 +206,6 @@ export async function loadContentConfig({
207206
return new NotFoundError('Failed to resolve content config.');
208207
} finally {
209208
await tempConfigServer.close();
210-
// Reset NODE_ENV to initial value
211-
// Vite's `createServer()` sets NODE_ENV to 'development'!
212-
process.env.NODE_ENV = nodeEnv;
213209
}
214210
const config = contentConfigParser.safeParse(unparsedConfig);
215211
if (config.success) {

packages/astro/src/core/config/config.ts

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export function resolveRoot(cwd?: string | URL): string {
115115
}
116116

117117
/** Merge CLI flags & user config object (CLI flags take priority) */
118-
function mergeCLIFlags(astroConfig: AstroUserConfig, flags: CLIFlags, cmd: string) {
118+
function mergeCLIFlags(astroConfig: AstroUserConfig, flags: CLIFlags) {
119119
astroConfig.server = astroConfig.server || {};
120120
astroConfig.markdown = astroConfig.markdown || {};
121121
astroConfig.experimental = astroConfig.experimental || {};
@@ -136,6 +136,23 @@ function mergeCLIFlags(astroConfig: AstroUserConfig, flags: CLIFlags, cmd: strin
136136
return astroConfig;
137137
}
138138

139+
async function search(fsMod: typeof fs, root: string) {
140+
const paths = [
141+
'astro.config.mjs',
142+
'astro.config.js',
143+
'astro.config.ts',
144+
'astro.config.mts',
145+
'astro.config.cjs',
146+
'astro.config.cts',
147+
].map((p) => path.join(root, p));
148+
149+
for (const file of paths) {
150+
if (fsMod.existsSync(file)) {
151+
return file;
152+
}
153+
}
154+
}
155+
139156
interface LoadConfigOptions {
140157
cwd?: string;
141158
flags?: Flags;
@@ -147,41 +164,36 @@ interface LoadConfigOptions {
147164
fsMod?: typeof fs;
148165
}
149166

167+
interface ResolveConfigPathOptions {
168+
cwd?: string;
169+
flags?: Flags;
170+
fs: typeof fs;
171+
}
172+
150173
/**
151174
* Resolve the file URL of the user's `astro.config.js|cjs|mjs|ts` file
152-
* Note: currently the same as loadConfig but only returns the `filePath`
153-
* instead of the resolved config
154175
*/
155176
export async function resolveConfigPath(
156-
configOptions: Pick<LoadConfigOptions, 'cwd' | 'flags'> & { fs: typeof fs }
177+
configOptions: ResolveConfigPathOptions
157178
): Promise<string | undefined> {
158179
const root = resolveRoot(configOptions.cwd);
159180
const flags = resolveFlags(configOptions.flags || {});
160-
let userConfigPath: string | undefined;
161181

182+
let userConfigPath: string | undefined;
162183
if (flags?.config) {
163184
userConfigPath = /^\.*\//.test(flags.config) ? flags.config : `./${flags.config}`;
164185
userConfigPath = fileURLToPath(new URL(userConfigPath, `file://${root}/`));
165-
}
166-
167-
// Resolve config file path using Proload
168-
// If `userConfigPath` is `undefined`, Proload will search for `astro.config.[cm]?[jt]s`
169-
try {
170-
const config = await loadConfigWithVite({
171-
configPath: userConfigPath,
172-
root,
173-
fs: configOptions.fs,
174-
});
175-
return config.filePath;
176-
} catch (e) {
177-
if (flags.config) {
186+
if (!configOptions.fs.existsSync(userConfigPath)) {
178187
throw new AstroError({
179188
...AstroErrorData.ConfigNotFound,
180189
message: AstroErrorData.ConfigNotFound.message(flags.config),
181190
});
182191
}
183-
throw e;
192+
} else {
193+
userConfigPath = await search(configOptions.fs, root);
184194
}
195+
196+
return userConfigPath;
185197
}
186198

187199
interface OpenConfigResult {
@@ -261,30 +273,14 @@ async function tryLoadConfig(
261273
}
262274
}
263275

264-
/**
265-
* Attempt to load an `astro.config.mjs` file
266-
* @deprecated
267-
*/
268-
export async function loadConfig(configOptions: LoadConfigOptions): Promise<AstroConfig> {
269-
const root = resolveRoot(configOptions.cwd);
270-
const flags = resolveFlags(configOptions.flags || {});
271-
let userConfig: AstroUserConfig = {};
272-
273-
const config = await tryLoadConfig(configOptions, root);
274-
if (config) {
275-
userConfig = config.value;
276-
}
277-
return resolveConfig(userConfig, root, flags, configOptions.cmd);
278-
}
279-
280276
/** Attempt to resolve an Astro configuration object. Normalize, validate, and return. */
281277
export async function resolveConfig(
282278
userConfig: AstroUserConfig,
283279
root: string,
284280
flags: CLIFlags = {},
285281
cmd: string
286282
): Promise<AstroConfig> {
287-
const mergedConfig = mergeCLIFlags(userConfig, flags, cmd);
283+
const mergedConfig = mergeCLIFlags(userConfig, flags);
288284
const validatedConfig = await validateConfig(mergedConfig, root, cmd);
289285

290286
return validatedConfig;

packages/astro/src/core/config/vite-load.ts

Lines changed: 14 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
import type fsType from 'fs';
2-
import npath from 'path';
32
import { pathToFileURL } from 'url';
43
import * as vite from 'vite';
54
import loadFallbackPlugin from '../../vite-plugin-load-fallback/index.js';
6-
import { AstroError, AstroErrorData } from '../errors/index.js';
7-
8-
// Fallback for legacy
9-
import load from '@proload/core';
10-
import loadTypeScript from '@proload/plugin-tsm';
11-
12-
load.use([loadTypeScript]);
135

146
export interface ViteLoader {
157
root: string;
@@ -24,8 +16,8 @@ async function createViteLoader(root: string, fs: typeof fsType): Promise<ViteLo
2416
appType: 'custom',
2517
ssr: {
2618
// NOTE: Vite doesn't externalize linked packages by default. During testing locally,
27-
// these dependencies trip up Vite's dev SSR transform. In the future, we should
28-
// avoid `vite.createServer` and use `loadConfigFromFile` instead.
19+
// these dependencies trip up Vite's dev SSR transform. Awaiting upstream feature:
20+
// https://github.com/vitejs/vite/pull/10939
2921
external: [
3022
'@astrojs/tailwind',
3123
'@astrojs/mdx',
@@ -43,40 +35,6 @@ async function createViteLoader(root: string, fs: typeof fsType): Promise<ViteLo
4335
};
4436
}
4537

46-
async function stat(fs: typeof fsType, configPath: string, mustExist: boolean): Promise<boolean> {
47-
try {
48-
await fs.promises.stat(configPath);
49-
return true;
50-
} catch {
51-
if (mustExist) {
52-
throw new AstroError({
53-
...AstroErrorData.ConfigNotFound,
54-
message: AstroErrorData.ConfigNotFound.message(configPath),
55-
});
56-
}
57-
return false;
58-
}
59-
}
60-
61-
async function search(fs: typeof fsType, root: string) {
62-
const paths = [
63-
'astro.config.mjs',
64-
'astro.config.js',
65-
'astro.config.ts',
66-
'astro.config.mts',
67-
'astro.config.cjs',
68-
'astro.config.cjs',
69-
].map((path) => npath.join(root, path));
70-
71-
for (const file of paths) {
72-
// First verify the file event exists
73-
const exists = await stat(fs, file, false);
74-
if (exists) {
75-
return file;
76-
}
77-
}
78-
}
79-
8038
interface LoadConfigWithViteOptions {
8139
root: string;
8240
configPath: string | undefined;
@@ -91,58 +49,36 @@ export async function loadConfigWithVite({
9149
value: Record<string, any>;
9250
filePath?: string;
9351
}> {
94-
let file: string;
95-
if (configPath) {
96-
// Go ahead and check if the file exists and throw if not.
97-
await stat(fs, configPath, true);
98-
file = configPath;
99-
} else {
100-
const found = await search(fs, root);
101-
if (!found) {
102-
// No config file found, return an empty config that will be populated with defaults
103-
return {
104-
value: {},
105-
filePath: undefined,
106-
};
107-
} else {
108-
file = found;
109-
}
52+
// No config file found, return an empty config that will be populated with defaults
53+
if (!configPath) {
54+
return {
55+
value: {},
56+
filePath: undefined,
57+
};
11058
}
11159

11260
// Try loading with Node import()
113-
if (/\.[cm]?js$/.test(file)) {
61+
if (/\.[cm]?js$/.test(configPath)) {
11462
try {
115-
const config = await import(pathToFileURL(file).toString());
63+
const config = await import(pathToFileURL(configPath).toString());
11664
return {
11765
value: config.default ?? {},
118-
filePath: file,
66+
filePath: configPath,
11967
};
12068
} catch {
12169
// We do not need to keep the error here because with fallback the error will be rethrown
122-
// when/if it fails in Proload.
70+
// when/if it fails in Vite.
12371
}
12472
}
12573

12674
// Try Loading with Vite
12775
let loader: ViteLoader | undefined;
12876
try {
12977
loader = await createViteLoader(root, fs);
130-
const mod = await loader.viteServer.ssrLoadModule(file);
78+
const mod = await loader.viteServer.ssrLoadModule(configPath);
13179
return {
13280
value: mod.default ?? {},
133-
filePath: file,
134-
};
135-
} catch {
136-
// Try loading with Proload
137-
// TODO deprecate - this is only for legacy compatibility
138-
const res = await load('astro', {
139-
mustExist: true,
140-
cwd: root,
141-
filePath: file,
142-
});
143-
return {
144-
value: res?.value ?? {},
145-
filePath: file,
81+
filePath: configPath,
14682
};
14783
} finally {
14884
if (loader) {

packages/astro/test/test-utils.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import stripAnsi from 'strip-ansi';
77
import { fileURLToPath } from 'url';
88
import { sync } from '../dist/cli/sync/index.js';
99
import build from '../dist/core/build/index.js';
10-
import { loadConfig } from '../dist/core/config/config.js';
10+
import { openConfig } from '../dist/core/config/config.js';
1111
import { createSettings } from '../dist/core/config/index.js';
1212
import dev from '../dist/core/dev/index.js';
1313
import { nodeLogDestination } from '../dist/core/logger/node.js';
@@ -86,7 +86,11 @@ export async function loadFixture(inlineConfig) {
8686
const logging = defaultLogging;
8787

8888
// Load the config.
89-
let config = await loadConfig({ cwd: fileURLToPath(cwd), logging });
89+
let { astroConfig: config } = await openConfig({
90+
cwd: fileURLToPath(cwd),
91+
logging,
92+
cmd: 'dev',
93+
});
9094
config = merge(config, { ...inlineConfig, root: cwd });
9195

9296
// HACK: the inline config doesn't run through config validation where these normalizations usually occur

pnpm-lock.yaml

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

scripts/memory/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { fileURLToPath } from 'url';
22
import v8 from 'v8';
33
import dev from '../../packages/astro/dist/core/dev/index.js';
4-
import { loadConfig } from '../../packages/astro/dist/core/config.js';
4+
import { openConfig } from '../../packages/astro/dist/core/config.js';
5+
import { nodeLogDestination } from '../../packages/astro/dist/core/logger/node.js';
56
import prettyBytes from 'pretty-bytes';
67

78
if (!global.gc) {
@@ -14,8 +15,13 @@ const isCI = process.argv.includes('--ci');
1415
/** URL directory containing the entire project. */
1516
const projDir = new URL('./project/', import.meta.url);
1617

17-
let config = await loadConfig({
18+
let { astroConfig: config } = await openConfig({
1819
cwd: fileURLToPath(projDir),
20+
logging: {
21+
dest: nodeLogDestination,
22+
level: 'error',
23+
},
24+
cmd: 'dev',
1925
});
2026

2127
const telemetry = {

0 commit comments

Comments
 (0)