Skip to content

Commit

Permalink
fix: await configResolved hooks of worker plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jan 14, 2024
1 parent ed56d96 commit 617cd6a
Showing 1 changed file with 50 additions and 50 deletions.
100 changes: 50 additions & 50 deletions packages/vite/src/node/config.ts
Expand Up @@ -535,8 +535,8 @@ export async function resolveConfig(
colors.yellow(
`\`resolve.browserField\` is set to false, but the option is removed in favour of ` +
`the 'browser' string in \`resolve.mainFields\`. You may want to update \`resolve.mainFields\` ` +
`to remove the 'browser' string and preserve the previous browser behaviour.`,
),
`to remove the 'browser' string and preserve the previous browser behaviour.`
)
)
}

Expand All @@ -560,7 +560,7 @@ export async function resolveConfig(
logger.warn(
`NODE_ENV=${userNodeEnv} is not supported in the .env file. ` +
`Only NODE_ENV=development is supported to create a development build of your project. ` +
`If you need to set process.env.NODE_ENV, you can set it in the Vite config instead.`,
`If you need to set process.env.NODE_ENV, you can set it in the Vite config instead.`
)
}
}
Expand All @@ -583,7 +583,7 @@ export async function resolveConfig(
const resolvedBuildOptions = resolveBuildOptions(
config.build,
logger,
resolvedRoot,
resolvedRoot
)

// resolve cache directory
Expand All @@ -592,8 +592,8 @@ export async function resolveConfig(
config.cacheDir
? path.resolve(resolvedRoot, config.cacheDir)
: pkgDir
? path.join(pkgDir, `node_modules/.vite`)
: path.join(resolvedRoot, `.vite`),
? path.join(pkgDir, `node_modules/.vite`)
: path.join(resolvedRoot, `.vite`)
)

const assetsFilter =
Expand Down Expand Up @@ -654,8 +654,8 @@ export async function resolveConfig(
? normalizePath(
path.resolve(
resolvedRoot,
typeof publicDir === 'string' ? publicDir : 'public',
),
typeof publicDir === 'string' ? publicDir : 'public'
)
)
: ''

Expand All @@ -676,8 +676,8 @@ export async function resolveConfig(
logger.warn(
colors.yellow(
`worker.plugins is now a function that returns an array of plugins. ` +
`Please update your Vite config accordingly.\n`,
),
`Please update your Vite config accordingly.\n`
)
)
}

Expand All @@ -703,7 +703,7 @@ export async function resolveConfig(
workerConfig = await runConfigHook(
workerConfig,
workerUserPlugins,
configEnv,
configEnv
)

const workerResolved: ResolvedConfig = {
Expand All @@ -716,13 +716,15 @@ export async function resolveConfig(
workerResolved,
workerPrePlugins,
workerNormalPlugins,
workerPostPlugins,
workerPostPlugins
)

// run configResolved hooks
createPluginHookUtils(resolvedWorkerPlugins)
.getSortedPluginHooks('configResolved')
.map((hook) => hook(workerResolved))
await Promise.all(
createPluginHookUtils(resolvedWorkerPlugins)
.getSortedPluginHooks('configResolved')
.map((hook) => hook(workerResolved))
)

return resolvedWorkerPlugins
}
Expand All @@ -736,7 +738,7 @@ export async function resolveConfig(
resolved = {
configFile: configFile ? normalizePath(configFile) : undefined,
configFileDependencies: configFileDependencies.map((name) =>
normalizePath(path.resolve(name)),
normalizePath(path.resolve(name))
),
inlineConfig,
root: resolvedRoot,
Expand Down Expand Up @@ -803,15 +805,15 @@ export async function resolveConfig(
resolved,
prePlugins,
normalPlugins,
postPlugins,
postPlugins
)
Object.assign(resolved, createPluginHookUtils(resolved.plugins))

// call configResolved hooks
await Promise.all(
resolved
.getSortedPluginHooks('configResolved')
.map((hook) => hook(resolved)),
.map((hook) => hook(resolved))
)

debug?.(`using resolved config: %O`, {
Expand All @@ -834,8 +836,8 @@ export async function resolveConfig(
colors.yellow(
`build.terserOptions is specified but build.minify is not set to use Terser. ` +
`Note Vite now defaults to use esbuild for minification. If you still ` +
`prefer Terser, set build.minify to "terser".`,
),
`prefer Terser, set build.minify to "terser".`
)
)
}

Expand All @@ -845,18 +847,18 @@ export async function resolveConfig(
// Use isArray to narrow its type to array
if (Array.isArray(outputOption)) {
const assetFileNamesList = outputOption.map(
(output) => output.assetFileNames,
(output) => output.assetFileNames
)
if (assetFileNamesList.length > 1) {
const firstAssetFileNames = assetFileNamesList[0]
const hasDifferentReference = assetFileNamesList.some(
(assetFileNames) => assetFileNames !== firstAssetFileNames,
(assetFileNames) => assetFileNames !== firstAssetFileNames
)
if (hasDifferentReference) {
resolved.logger.warn(
colors.yellow(`
assetFileNames isn't equal for every build.rollupOptions.output. A single pattern across all outputs is supported by Vite.
`),
`)
)
}
}
Expand All @@ -873,7 +875,7 @@ assetFileNames isn't equal for every build.rollupOptions.output. A single patter
colors.yellow(`
(!) Experimental legacy.buildSsrCjsExternalHeuristics and ssr.format were be removed in Vite 5.
The only SSR Output format is ESM. Find more information at https://github.com/vitejs/vite/discussions/13816.
`),
`)
)
}

Expand All @@ -887,16 +889,16 @@ assetFileNames isn't equal for every build.rollupOptions.output. A single patter
export function resolveBaseUrl(
base: UserConfig['base'] = '/',
isBuild: boolean,
logger: Logger,
logger: Logger
): string {
if (base[0] === '.') {
logger.warn(
colors.yellow(
colors.bold(
`(!) invalid "base" option: "${base}". The value can only be an absolute ` +
`URL, "./", or an empty string.`,
),
),
`URL, "./", or an empty string.`
)
)
)
return '/'
}
Expand All @@ -906,9 +908,7 @@ export function resolveBaseUrl(
// no leading slash warn
if (!isExternal && base[0] !== '/') {
logger.warn(
colors.yellow(
colors.bold(`(!) "base" option should start with a slash.`),
),
colors.yellow(colors.bold(`(!) "base" option should start with a slash.`))
)
}

Expand All @@ -925,7 +925,7 @@ export function resolveBaseUrl(
}

export function sortUserPlugins(
plugins: (Plugin | Plugin[])[] | undefined,
plugins: (Plugin | Plugin[])[] | undefined
): [Plugin[], Plugin[], Plugin[]] {
const prePlugins: Plugin[] = []
const postPlugins: Plugin[] = []
Expand All @@ -946,7 +946,7 @@ export async function loadConfigFromFile(
configEnv: ConfigEnv,
configFile?: string,
configRoot: string = process.cwd(),
logLevel?: LogLevel,
logLevel?: LogLevel
): Promise<{
path: string
config: UserConfig
Expand Down Expand Up @@ -984,7 +984,7 @@ export async function loadConfigFromFile(
const userConfig = await loadConfigFromBundledFile(
resolvedPath,
bundled.code,
isESM,
isESM
)
debug?.(`bundled config file loaded in ${getTime()}`)

Expand All @@ -1002,15 +1002,15 @@ export async function loadConfigFromFile(
} catch (e) {
createLogger(logLevel).error(
colors.red(`failed to load config from ${resolvedPath}`),
{ error: e },
{ error: e }
)
throw e
}
}

async function bundleConfigFile(
fileName: string,
isESM: boolean,
isESM: boolean
): Promise<{ code: string; dependencies: string[] }> {
const dirnameVarName = '__vite_injected_original_dirname'
const filenameVarName = '__vite_injected_original_filename'
Expand Down Expand Up @@ -1039,7 +1039,7 @@ async function bundleConfigFile(
const resolveByViteResolver = (
id: string,
importer: string,
isRequire: boolean,
isRequire: boolean
) => {
return tryNodeResolve(
id,
Expand All @@ -1059,7 +1059,7 @@ async function bundleConfigFile(
packageCache,
isRequire,
},
false,
false
)?.id
}

Expand Down Expand Up @@ -1093,14 +1093,14 @@ async function bundleConfigFile(
canResolveWithImport = !!resolveByViteResolver(
id,
importer,
false,
false
)
} catch {}
if (canResolveWithImport) {
throw new Error(
`Failed to resolve ${JSON.stringify(
id,
)}. This package is ESM only but it was tried to load by \`require\`. See https://vitejs.dev/guide/troubleshooting.html#this-package-is-esm-only for more details.`,
id
)}. This package is ESM only but it was tried to load by \`require\`. See https://vitejs.dev/guide/troubleshooting.html#this-package-is-esm-only for more details.`
)
}
}
Expand All @@ -1116,15 +1116,15 @@ async function bundleConfigFile(
) {
throw new Error(
`${JSON.stringify(
id,
)} resolved to an ESM file. ESM file cannot be loaded by \`require\`. See https://vitejs.dev/guide/troubleshooting.html#this-package-is-esm-only for more details.`,
id
)} resolved to an ESM file. ESM file cannot be loaded by \`require\`. See https://vitejs.dev/guide/troubleshooting.html#this-package-is-esm-only for more details.`
)
}
return {
path: idFsPath,
external: true,
}
},
}
)
},
},
Expand All @@ -1135,11 +1135,11 @@ async function bundleConfigFile(
const contents = await fsp.readFile(args.path, 'utf8')
const injectValues =
`const ${dirnameVarName} = ${JSON.stringify(
path.dirname(args.path),
path.dirname(args.path)
)};` +
`const ${filenameVarName} = ${JSON.stringify(args.path)};` +
`const ${importMetaUrlVarName} = ${JSON.stringify(
pathToFileURL(args.path).href,
pathToFileURL(args.path).href
)};`

return {
Expand All @@ -1166,7 +1166,7 @@ const _require = createRequire(import.meta.url)
async function loadConfigFromBundledFile(
fileName: string,
bundledCode: string,
isESM: boolean,
isESM: boolean
): Promise<UserConfigExport> {
// for esm, before we can register loaders without requiring users to run node
// with --experimental-loader themselves, we have to do a hack here:
Expand Down Expand Up @@ -1212,7 +1212,7 @@ async function loadConfigFromBundledFile(
async function runConfigHook(
config: InlineConfig,
plugins: Plugin[],
configEnv: ConfigEnv,
configEnv: ConfigEnv
): Promise<InlineConfig> {
let conf = config

Expand All @@ -1232,13 +1232,13 @@ async function runConfigHook(

export function getDepOptimizationConfig(
config: ResolvedConfig,
ssr: boolean,
ssr: boolean
): DepOptimizationConfig {
return ssr ? config.ssr.optimizeDeps : config.optimizeDeps
}
export function isDepsOptimizerEnabled(
config: ResolvedConfig,
ssr: boolean,
ssr: boolean
): boolean {
const { command } = config
const { disabled } = getDepOptimizationConfig(config, ssr)
Expand Down

0 comments on commit 617cd6a

Please sign in to comment.