fix(kit,vite,webpack): resolve postcss paths from each modules dir (#29096)

This commit is contained in:
Daniel Roe 2024-09-20 18:39:17 +01:00
parent 097105fb2b
commit 73accb5ac1
No known key found for this signature in database
GPG Key ID: CBC814C393D93268
3 changed files with 27 additions and 17 deletions

View File

@ -79,7 +79,6 @@ export async function loadNuxtModuleInstance (nuxtModule: string | NuxtModule, n
// Import if input is string // Import if input is string
if (typeof nuxtModule === 'string') { if (typeof nuxtModule === 'string') {
let error: unknown
const paths = [join(nuxtModule, 'nuxt'), join(nuxtModule, 'module'), nuxtModule, join(nuxt.options.rootDir, nuxtModule)] const paths = [join(nuxtModule, 'nuxt'), join(nuxtModule, 'module'), nuxtModule, join(nuxt.options.rootDir, nuxtModule)]
for (const parentURL of nuxt.options.modulesDir) { for (const parentURL of nuxt.options.modulesDir) {
@ -94,16 +93,17 @@ export async function loadNuxtModuleInstance (nuxtModule: string | NuxtModule, n
buildTimeModuleMeta = JSON.parse(await fsp.readFile(moduleMetadataPath, 'utf-8')) buildTimeModuleMeta = JSON.parse(await fsp.readFile(moduleMetadataPath, 'utf-8'))
} }
break break
} catch (_err: unknown) { } catch (error: unknown) {
error = _err 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 !== '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 // Throw error if input is not a function

View File

@ -27,11 +27,16 @@ export async function resolveCSSOptions (nuxt: Nuxt): Promise<ViteConfig['css']>
const pluginOptions = postcssOptions.plugins[pluginName] const pluginOptions = postcssOptions.plugins[pluginName]
if (!pluginOptions) { continue } if (!pluginOptions) { continue }
const path = jiti.esmResolve(pluginName) let pluginFn: ((opts: Record<string, any>) => Plugin) | undefined
const pluginFn = (await jiti.import(path)) as (opts: Record<string, any>) => Plugin for (const parentURL of nuxt.options.modulesDir) {
if (typeof pluginFn === 'function') { pluginFn = await jiti.import(pluginName, { parentURL, try: true }) as (opts: Record<string, any>) => Plugin
css.postcss.plugins.push(pluginFn(pluginOptions)) if (typeof pluginFn === 'function') {
} else { 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.`) console.warn(`[nuxt] could not import postcss plugin \`${pluginName}\`. Please report this as a bug.`)
} }
} }

View File

@ -49,11 +49,16 @@ export async function getPostcssConfig (nuxt: Nuxt) {
const pluginOptions = postcssOptions.plugins[pluginName] const pluginOptions = postcssOptions.plugins[pluginName]
if (!pluginOptions) { continue } if (!pluginOptions) { continue }
const path = jiti.esmResolve(pluginName) let pluginFn: ((opts: Record<string, any>) => Plugin) | undefined
const pluginFn = (await jiti.import(path)) as (opts: Record<string, any>) => Plugin for (const parentURL of nuxt.options.modulesDir) {
if (typeof pluginFn === 'function') { pluginFn = await jiti.import(pluginName, { parentURL, try: true }) as (opts: Record<string, any>) => Plugin
plugins.push(pluginFn(pluginOptions)) if (typeof pluginFn === 'function') {
} else { plugins.push(pluginFn(pluginOptions))
break
}
}
if (typeof pluginFn !== 'function') {
console.warn(`[nuxt] could not import postcss plugin \`${pluginName}\`. Please report this as a bug.`) console.warn(`[nuxt] could not import postcss plugin \`${pluginName}\`. Please report this as a bug.`)
} }
} }