fix: resolve @nuxt/kit module import error in browser
All checks were successful
deploy-analiticaNucleo / deploy (push) Successful in 36s
All checks were successful
deploy-analiticaNucleo / deploy (push) Successful in 36s
- Replace external module references with empty stub file - Add .nuxt-stubs/empty.mjs to provide browser-compatible module - Update workaround to resolve server-only modules to stub file instead of marking as external
This commit is contained in:
1
nuxt4-app/.nuxt-stubs/empty.mjs
Normal file
1
nuxt4-app/.nuxt-stubs/empty.mjs
Normal file
@@ -0,0 +1 @@
|
||||
export default {}
|
||||
@@ -2,29 +2,43 @@
|
||||
// This is a temporary fix until @nuxt/ui properly handles ssr: false
|
||||
|
||||
import type { Plugin } from 'vite'
|
||||
import { fileURLToPath } from 'url'
|
||||
import { dirname, resolve } from 'path'
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url)
|
||||
const __dirname = dirname(__filename)
|
||||
const emptyModulePath = resolve(__dirname, '.nuxt-stubs/empty.mjs')
|
||||
|
||||
export function disableImportProtection(): Plugin {
|
||||
return {
|
||||
name: 'disable-import-protection-for-nuxt-ui',
|
||||
enforce: 'pre',
|
||||
resolveId(id, importer) {
|
||||
// External node binary files
|
||||
if (id.endsWith('.node')) {
|
||||
return { id, external: true }
|
||||
}
|
||||
// Server-only packages that should be stubbed for client bundle
|
||||
const serverPackages = [
|
||||
'@nuxt/kit',
|
||||
'@tailwindcss/node',
|
||||
'@tailwindcss/oxide',
|
||||
'jiti'
|
||||
]
|
||||
|
||||
// External Node.js built-in modules
|
||||
const nodeModules = ['fs', 'path', 'url', 'fs/promises', 'node:url', 'node:module', 'node:fs', 'node:path']
|
||||
if (nodeModules.includes(id) || id.startsWith('node:')) {
|
||||
return { id, external: true }
|
||||
}
|
||||
|
||||
// External server-only packages from @nuxt/ui imports
|
||||
const serverPackages = ['@nuxt/kit', '@tailwindcss/node', '@tailwindcss/oxide', 'jiti']
|
||||
for (const pkg of serverPackages) {
|
||||
if (id === pkg || id.startsWith(pkg + '/')) {
|
||||
return { id, external: true }
|
||||
}
|
||||
return emptyModulePath
|
||||
}
|
||||
}
|
||||
|
||||
// Stub .node binary files
|
||||
if (id.endsWith('.node')) {
|
||||
return emptyModulePath
|
||||
}
|
||||
|
||||
// Stub Node.js built-in modules for client bundle
|
||||
const nodeModules = ['fs', 'path', 'url', 'fs/promises']
|
||||
const nodePrefix = ['node:url', 'node:module', 'node:fs', 'node:path']
|
||||
|
||||
if (nodeModules.includes(id) || nodePrefix.includes(id) || id.startsWith('node:')) {
|
||||
return emptyModulePath
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user