From 9d77fdc8020861a29e8257b5a84d259454362316 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 20 Sep 2024 18:39:17 +0100 Subject: [PATCH] fix(kit,vite,webpack): resolve postcss paths from each modules dir (#29096) --- packages/kit/src/module/install.ts | 14 +++++++------- packages/vite/src/css.ts | 15 ++++++++++----- packages/webpack/src/utils/postcss.ts | 15 ++++++++++----- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/packages/kit/src/module/install.ts b/packages/kit/src/module/install.ts index e44140ee57..81a389dee2 100644 --- a/packages/kit/src/module/install.ts +++ b/packages/kit/src/module/install.ts @@ -85,7 +85,6 @@ export async function loadNuxtModuleInstance (nuxtModule: string | NuxtModule, n // Import if input is string if (typeof nuxtModule === 'string') { - let error: unknown const paths = [join(nuxtModule, 'nuxt'), join(nuxtModule, 'module'), nuxtModule, join(nuxt.options.rootDir, nuxtModule)] for (const parentURL of nuxt.options.modulesDir) { @@ -100,16 +99,17 @@ export async function loadNuxtModuleInstance (nuxtModule: string | NuxtModule, n buildTimeModuleMeta = JSON.parse(await fsp.readFile(moduleMetadataPath, 'utf-8')) } break - } catch (_err: unknown) { - error = _err + } catch (error: unknown) { + const code = (error as Error & { code?: string }).code + if (code === 'MODULE_NOT_FOUND' || code === 'ERR_PACKAGE_PATH_NOT_EXPORTED' || code === 'ERR_MODULE_NOT_FOUND' || code === 'ERR_UNSUPPORTED_DIR_IMPORT') { + continue + } + logger.error(`Error while importing module \`${nuxtModule}\`: ${error}`) + throw error } } if (typeof nuxtModule !== 'string') { break } } - if (typeof nuxtModule !== 'function' && error) { - logger.error(`Error while importing module \`${nuxtModule}\`: ${error}`) - throw error - } } // Throw error if input is not a function diff --git a/packages/vite/src/css.ts b/packages/vite/src/css.ts index 6457c78e43..4a26447584 100644 --- a/packages/vite/src/css.ts +++ b/packages/vite/src/css.ts @@ -27,11 +27,16 @@ export async function resolveCSSOptions (nuxt: Nuxt): Promise const pluginOptions = postcssOptions.plugins[pluginName] if (!pluginOptions) { continue } - const path = jiti.esmResolve(pluginName) - const pluginFn = (await jiti.import(path)) as (opts: Record) => Plugin - if (typeof pluginFn === 'function') { - css.postcss.plugins.push(pluginFn(pluginOptions)) - } else { + let pluginFn: ((opts: Record) => Plugin) | undefined + for (const parentURL of nuxt.options.modulesDir) { + pluginFn = await jiti.import(pluginName, { parentURL, try: true }) as (opts: Record) => Plugin + if (typeof pluginFn === 'function') { + css.postcss.plugins.push(pluginFn(pluginOptions)) + break + } + } + + if (typeof pluginFn !== 'function') { console.warn(`[nuxt] could not import postcss plugin \`${pluginName}\`. Please report this as a bug.`) } } diff --git a/packages/webpack/src/utils/postcss.ts b/packages/webpack/src/utils/postcss.ts index 0d48972bb6..1548355eb0 100644 --- a/packages/webpack/src/utils/postcss.ts +++ b/packages/webpack/src/utils/postcss.ts @@ -49,11 +49,16 @@ export async function getPostcssConfig (nuxt: Nuxt) { const pluginOptions = postcssOptions.plugins[pluginName] if (!pluginOptions) { continue } - const path = jiti.esmResolve(pluginName) - const pluginFn = (await jiti.import(path)) as (opts: Record) => Plugin - if (typeof pluginFn === 'function') { - plugins.push(pluginFn(pluginOptions)) - } else { + let pluginFn: ((opts: Record) => Plugin) | undefined + for (const parentURL of nuxt.options.modulesDir) { + pluginFn = await jiti.import(pluginName, { parentURL, try: true }) as (opts: Record) => Plugin + if (typeof pluginFn === 'function') { + plugins.push(pluginFn(pluginOptions)) + break + } + } + + if (typeof pluginFn !== 'function') { console.warn(`[nuxt] could not import postcss plugin \`${pluginName}\`. Please report this as a bug.`) } }