From 49ab4f231c23b34891c3ee86f4b92bf8d6d267a3 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Sat, 7 Jan 2023 01:49:18 +0800 Subject: [PATCH] Remove proload for config loading (#5778) * Refactor resolve and load config * Add changeset * Update name * Remove unnecessary node_env handling * Fix test * Update comment --- .changeset/calm-peas-doubt.md | 5 ++ packages/astro/package.json | 2 - packages/astro/src/content/utils.ts | 4 - packages/astro/src/core/config/config.ts | 68 +++++++-------- packages/astro/src/core/config/vite-load.ts | 92 ++++----------------- packages/astro/test/test-utils.js | 8 +- pnpm-lock.yaml | 4 - scripts/memory/index.js | 10 ++- 8 files changed, 65 insertions(+), 128 deletions(-) create mode 100644 .changeset/calm-peas-doubt.md diff --git a/.changeset/calm-peas-doubt.md b/.changeset/calm-peas-doubt.md new file mode 100644 index 000000000000..68de7b3a674b --- /dev/null +++ b/.changeset/calm-peas-doubt.md @@ -0,0 +1,5 @@ +--- +'astro': major +--- + +Remove proload to load the Astro config. It will now use NodeJS and Vite to load the config only. diff --git a/packages/astro/package.json b/packages/astro/package.json index 8f30610828e0..f7ede273a9ec 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -120,8 +120,6 @@ "@babel/plugin-transform-react-jsx": "^7.17.12", "@babel/traverse": "^7.18.2", "@babel/types": "^7.18.4", - "@proload/core": "^0.3.3", - "@proload/plugin-tsm": "^0.2.1", "@types/babel__core": "^7.1.19", "@types/html-escaper": "^3.0.0", "@types/yargs-parser": "^21.0.0", diff --git a/packages/astro/src/content/utils.ts b/packages/astro/src/content/utils.ts index 07f19faf0497..8d52fd01ef31 100644 --- a/packages/astro/src/content/utils.ts +++ b/packages/astro/src/content/utils.ts @@ -190,7 +190,6 @@ export async function loadContentConfig({ settings: AstroSettings; }): Promise { const contentPaths = getContentPaths({ srcDir: settings.config.srcDir }); - const nodeEnv = process.env.NODE_ENV; const tempConfigServer: ViteDevServer = await createServer({ root: fileURLToPath(settings.config.root), server: { middlewareMode: true, hmr: false }, @@ -207,9 +206,6 @@ export async function loadContentConfig({ return new NotFoundError('Failed to resolve content config.'); } finally { await tempConfigServer.close(); - // Reset NODE_ENV to initial value - // Vite's `createServer()` sets NODE_ENV to 'development'! - process.env.NODE_ENV = nodeEnv; } const config = contentConfigParser.safeParse(unparsedConfig); if (config.success) { diff --git a/packages/astro/src/core/config/config.ts b/packages/astro/src/core/config/config.ts index 248ea91170c7..5be492b404c3 100644 --- a/packages/astro/src/core/config/config.ts +++ b/packages/astro/src/core/config/config.ts @@ -115,7 +115,7 @@ export function resolveRoot(cwd?: string | URL): string { } /** Merge CLI flags & user config object (CLI flags take priority) */ -function mergeCLIFlags(astroConfig: AstroUserConfig, flags: CLIFlags, cmd: string) { +function mergeCLIFlags(astroConfig: AstroUserConfig, flags: CLIFlags) { astroConfig.server = astroConfig.server || {}; astroConfig.markdown = astroConfig.markdown || {}; astroConfig.experimental = astroConfig.experimental || {}; @@ -136,6 +136,23 @@ function mergeCLIFlags(astroConfig: AstroUserConfig, flags: CLIFlags, cmd: strin return astroConfig; } +async function search(fsMod: typeof fs, root: string) { + const paths = [ + 'astro.config.mjs', + 'astro.config.js', + 'astro.config.ts', + 'astro.config.mts', + 'astro.config.cjs', + 'astro.config.cts', + ].map((p) => path.join(root, p)); + + for (const file of paths) { + if (fsMod.existsSync(file)) { + return file; + } + } +} + interface LoadConfigOptions { cwd?: string; flags?: Flags; @@ -147,41 +164,36 @@ interface LoadConfigOptions { fsMod?: typeof fs; } +interface ResolveConfigPathOptions { + cwd?: string; + flags?: Flags; + fs: typeof fs; +} + /** * Resolve the file URL of the user's `astro.config.js|cjs|mjs|ts` file - * Note: currently the same as loadConfig but only returns the `filePath` - * instead of the resolved config */ export async function resolveConfigPath( - configOptions: Pick & { fs: typeof fs } + configOptions: ResolveConfigPathOptions ): Promise { const root = resolveRoot(configOptions.cwd); const flags = resolveFlags(configOptions.flags || {}); - let userConfigPath: string | undefined; + let userConfigPath: string | undefined; if (flags?.config) { userConfigPath = /^\.*\//.test(flags.config) ? flags.config : `./${flags.config}`; userConfigPath = fileURLToPath(new URL(userConfigPath, `file://${root}/`)); - } - - // Resolve config file path using Proload - // If `userConfigPath` is `undefined`, Proload will search for `astro.config.[cm]?[jt]s` - try { - const config = await loadConfigWithVite({ - configPath: userConfigPath, - root, - fs: configOptions.fs, - }); - return config.filePath; - } catch (e) { - if (flags.config) { + if (!configOptions.fs.existsSync(userConfigPath)) { throw new AstroError({ ...AstroErrorData.ConfigNotFound, message: AstroErrorData.ConfigNotFound.message(flags.config), }); } - throw e; + } else { + userConfigPath = await search(configOptions.fs, root); } + + return userConfigPath; } interface OpenConfigResult { @@ -261,22 +273,6 @@ async function tryLoadConfig( } } -/** - * Attempt to load an `astro.config.mjs` file - * @deprecated - */ -export async function loadConfig(configOptions: LoadConfigOptions): Promise { - const root = resolveRoot(configOptions.cwd); - const flags = resolveFlags(configOptions.flags || {}); - let userConfig: AstroUserConfig = {}; - - const config = await tryLoadConfig(configOptions, root); - if (config) { - userConfig = config.value; - } - return resolveConfig(userConfig, root, flags, configOptions.cmd); -} - /** Attempt to resolve an Astro configuration object. Normalize, validate, and return. */ export async function resolveConfig( userConfig: AstroUserConfig, @@ -284,7 +280,7 @@ export async function resolveConfig( flags: CLIFlags = {}, cmd: string ): Promise { - const mergedConfig = mergeCLIFlags(userConfig, flags, cmd); + const mergedConfig = mergeCLIFlags(userConfig, flags); const validatedConfig = await validateConfig(mergedConfig, root, cmd); return validatedConfig; diff --git a/packages/astro/src/core/config/vite-load.ts b/packages/astro/src/core/config/vite-load.ts index b512d7fabeec..1a620ab90af5 100644 --- a/packages/astro/src/core/config/vite-load.ts +++ b/packages/astro/src/core/config/vite-load.ts @@ -1,15 +1,7 @@ import type fsType from 'fs'; -import npath from 'path'; import { pathToFileURL } from 'url'; import * as vite from 'vite'; import loadFallbackPlugin from '../../vite-plugin-load-fallback/index.js'; -import { AstroError, AstroErrorData } from '../errors/index.js'; - -// Fallback for legacy -import load from '@proload/core'; -import loadTypeScript from '@proload/plugin-tsm'; - -load.use([loadTypeScript]); export interface ViteLoader { root: string; @@ -24,8 +16,8 @@ async function createViteLoader(root: string, fs: typeof fsType): Promise { - try { - await fs.promises.stat(configPath); - return true; - } catch { - if (mustExist) { - throw new AstroError({ - ...AstroErrorData.ConfigNotFound, - message: AstroErrorData.ConfigNotFound.message(configPath), - }); - } - return false; - } -} - -async function search(fs: typeof fsType, root: string) { - const paths = [ - 'astro.config.mjs', - 'astro.config.js', - 'astro.config.ts', - 'astro.config.mts', - 'astro.config.cjs', - 'astro.config.cjs', - ].map((path) => npath.join(root, path)); - - for (const file of paths) { - // First verify the file event exists - const exists = await stat(fs, file, false); - if (exists) { - return file; - } - } -} - interface LoadConfigWithViteOptions { root: string; configPath: string | undefined; @@ -91,35 +49,25 @@ export async function loadConfigWithVite({ value: Record; filePath?: string; }> { - let file: string; - if (configPath) { - // Go ahead and check if the file exists and throw if not. - await stat(fs, configPath, true); - file = configPath; - } else { - const found = await search(fs, root); - if (!found) { - // No config file found, return an empty config that will be populated with defaults - return { - value: {}, - filePath: undefined, - }; - } else { - file = found; - } + // No config file found, return an empty config that will be populated with defaults + if (!configPath) { + return { + value: {}, + filePath: undefined, + }; } // Try loading with Node import() - if (/\.[cm]?js$/.test(file)) { + if (/\.[cm]?js$/.test(configPath)) { try { - const config = await import(pathToFileURL(file).toString()); + const config = await import(pathToFileURL(configPath).toString()); return { value: config.default ?? {}, - filePath: file, + filePath: configPath, }; } catch { // We do not need to keep the error here because with fallback the error will be rethrown - // when/if it fails in Proload. + // when/if it fails in Vite. } } @@ -127,22 +75,10 @@ export async function loadConfigWithVite({ let loader: ViteLoader | undefined; try { loader = await createViteLoader(root, fs); - const mod = await loader.viteServer.ssrLoadModule(file); + const mod = await loader.viteServer.ssrLoadModule(configPath); return { value: mod.default ?? {}, - filePath: file, - }; - } catch { - // Try loading with Proload - // TODO deprecate - this is only for legacy compatibility - const res = await load('astro', { - mustExist: true, - cwd: root, - filePath: file, - }); - return { - value: res?.value ?? {}, - filePath: file, + filePath: configPath, }; } finally { if (loader) { diff --git a/packages/astro/test/test-utils.js b/packages/astro/test/test-utils.js index 8c12799f15a1..63b79d7635b4 100644 --- a/packages/astro/test/test-utils.js +++ b/packages/astro/test/test-utils.js @@ -7,7 +7,7 @@ import stripAnsi from 'strip-ansi'; import { fileURLToPath } from 'url'; import { sync } from '../dist/cli/sync/index.js'; import build from '../dist/core/build/index.js'; -import { loadConfig } from '../dist/core/config/config.js'; +import { openConfig } from '../dist/core/config/config.js'; import { createSettings } from '../dist/core/config/index.js'; import dev from '../dist/core/dev/index.js'; import { nodeLogDestination } from '../dist/core/logger/node.js'; @@ -86,7 +86,11 @@ export async function loadFixture(inlineConfig) { const logging = defaultLogging; // Load the config. - let config = await loadConfig({ cwd: fileURLToPath(cwd), logging }); + let { astroConfig: config } = await openConfig({ + cwd: fileURLToPath(cwd), + logging, + cmd: 'dev', + }); config = merge(config, { ...inlineConfig, root: cwd }); // HACK: the inline config doesn't run through config validation where these normalizations usually occur diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 503231a93f3e..6227a240edfb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -397,8 +397,6 @@ importers: '@babel/traverse': ^7.18.2 '@babel/types': ^7.18.4 '@playwright/test': ^1.22.2 - '@proload/core': ^0.3.3 - '@proload/plugin-tsm': ^0.2.1 '@types/babel__core': ^7.1.19 '@types/babel__generator': ^7.6.4 '@types/babel__traverse': ^7.17.1 @@ -496,8 +494,6 @@ importers: '@babel/plugin-transform-react-jsx': 7.20.7_@babel+core@7.20.12 '@babel/traverse': 7.20.12 '@babel/types': 7.20.7 - '@proload/core': 0.3.3 - '@proload/plugin-tsm': 0.2.1_@proload+core@0.3.3 '@types/babel__core': 7.1.20 '@types/html-escaper': 3.0.0 '@types/yargs-parser': 21.0.0 diff --git a/scripts/memory/index.js b/scripts/memory/index.js index 30cc1c0f2a8b..8eaffb56b581 100644 --- a/scripts/memory/index.js +++ b/scripts/memory/index.js @@ -1,7 +1,8 @@ import { fileURLToPath } from 'url'; import v8 from 'v8'; import dev from '../../packages/astro/dist/core/dev/index.js'; -import { loadConfig } from '../../packages/astro/dist/core/config.js'; +import { openConfig } from '../../packages/astro/dist/core/config.js'; +import { nodeLogDestination } from '../../packages/astro/dist/core/logger/node.js'; import prettyBytes from 'pretty-bytes'; if (!global.gc) { @@ -14,8 +15,13 @@ const isCI = process.argv.includes('--ci'); /** URL directory containing the entire project. */ const projDir = new URL('./project/', import.meta.url); -let config = await loadConfig({ +let { astroConfig: config } = await openConfig({ cwd: fileURLToPath(projDir), + logging: { + dest: nodeLogDestination, + level: 'error', + }, + cmd: 'dev', }); const telemetry = {