Skip to content

Commit 3a8b1fb

Browse files
authored
fix(tw4): use safe module resolution to prevent throws for unresolvable plugins (#519)
1 parent 46d0728 commit 3a8b1fb

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/build/css/providers/tw4.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,14 @@ async function loadTw4Deps() {
4747
*/
4848
export function createModuleLoader(baseDir: string) {
4949
return async (id: string, base: string) => {
50-
const resolved = resolveModulePath(id, { from: base || baseDir })
50+
// Use suffixes + extensions to handle packages without an `exports` map
51+
// (e.g. daisyui/theme resolves to daisyui/theme/index.js)
52+
const resolved = resolveModulePath(id, {
53+
from: base || baseDir,
54+
try: true,
55+
suffixes: ['', '/index'],
56+
extensions: ['.js', '.mjs', '.cjs'],
57+
})
5158
if (resolved) {
5259
const module = await import(pathToFileURL(resolved).href).then(m => m.default || m)
5360
return { path: resolved, base: dirname(resolved), module }
@@ -66,7 +73,7 @@ export function createStylesheetLoader(baseDir: string) {
6673
return async (id: string, base: string) => {
6774
// Handle tailwindcss import
6875
if (id === 'tailwindcss') {
69-
const twPath = resolveModulePath('tailwindcss/index.css', { from: baseDir })
76+
const twPath = resolveModulePath('tailwindcss/index.css', { from: baseDir, try: true })
7077
if (twPath) {
7178
const content = await readFile(twPath, 'utf-8').catch(() => '')
7279
if (content)
@@ -85,7 +92,13 @@ export function createStylesheetLoader(baseDir: string) {
8592
return { path: resolved, base: dirname(resolved), content }
8693
}
8794
// Handle node_modules imports (e.g., @nuxt/ui)
88-
const resolved = resolveModulePath(id, { from: base || baseDir, conditions: ['style'] })
95+
const resolved = resolveModulePath(id, {
96+
from: base || baseDir,
97+
conditions: ['style'],
98+
try: true,
99+
suffixes: ['', '/index'],
100+
extensions: ['.css', '.js', '.mjs'],
101+
})
89102
if (resolved) {
90103
const content = await readFile(resolved, 'utf-8').catch(() => '')
91104
return { path: resolved, base: dirname(resolved), content }

0 commit comments

Comments
 (0)