fix(core,kit): ensure module transpilation paths are dirs (#20396)

This commit is contained in:
Harlan Wilton 2023-04-20 01:05:46 +07:00 committed by GitHub
parent 526852c1f1
commit 5febd46d23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -1,4 +1,6 @@
import { lstatSync } from 'node:fs'
import type { Nuxt, NuxtModule } from '@nuxt/schema'
import { dirname, isAbsolute } from 'pathe'
import { isNuxt2 } from '../compatibility'
import { useNuxt } from '../context'
import { requireModule, resolveModule } from '../internal/cjs'
@ -22,7 +24,7 @@ export async function installModule (moduleToInstall: string | NuxtModule, _inli
}
if (typeof moduleToInstall === 'string') {
nuxt.options.build.transpile.push(moduleToInstall)
nuxt.options.build.transpile.push(normalizeModuleTranspilePath(moduleToInstall))
}
nuxt.options._installedModules = nuxt.options._installedModules || []
@ -35,6 +37,17 @@ export async function installModule (moduleToInstall: string | NuxtModule, _inli
// --- Internal ---
export const normalizeModuleTranspilePath = (p: string) => {
try {
// we need to target directories instead of module file paths themselves
// /home/user/project/node_modules/module/index.js -> /home/user/project/node_modules/module
p = isAbsolute(p) && lstatSync(p).isFile() ? dirname(p) : p
} catch (e) {
// maybe the path is absolute but does not exist, allow this to bubble up
}
return p.split('node_modules/').pop() as string
}
async function normalizeModule (nuxtModule: string | NuxtModule, inlineOptions?: any) {
const nuxt = useNuxt()

View File

@ -1,4 +1,4 @@
import { useNuxt } from '@nuxt/kit'
import { normalizeModuleTranspilePath, useNuxt } from '@nuxt/kit'
export interface AddModuleTranspilesOptions {
additionalModules?: string[]
@ -14,7 +14,7 @@ export const addModuleTranspiles = (opts: AddModuleTranspilesOptions = {}) => {
]
.map(m => typeof m === 'string' ? m : Array.isArray(m) ? m[0] : m.src)
.filter(m => typeof m === 'string')
.map(m => m.split('node_modules/').pop())
.map(normalizeModuleTranspilePath)
// Try to sanitize modules to better match imports
nuxt.options.build.transpile =