Skip to content

Commit

Permalink
fix: resolve tailwindcss/tailwind.css with pnpm and no shamefully hoi…
Browse files Browse the repository at this point in the history
…st (#740)
  • Loading branch information
ineshbose authored Oct 25, 2023
1 parent 5d64568 commit 52356f9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default defineNuxtModule<ModuleOptions>({

/** CSS file handling */
const cssPath = typeof moduleOptions.cssPath === 'string' ? await resolvePath(moduleOptions.cssPath, { extensions: ['.css', '.sass', '.scss', '.less', '.styl'] }) : false
const [resolvedCss, loggerInfo] = resolveCSSPath(cssPath, nuxt)
const [resolvedCss, loggerInfo] = await resolveCSSPath(cssPath, nuxt)
logger.info(loggerInfo)

nuxt.options.css = nuxt.options.css ?? []
Expand Down
17 changes: 14 additions & 3 deletions src/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync } from 'fs'
import { join, relative } from 'pathe'
import { addTemplate, createResolver, findPath, useNuxt } from '@nuxt/kit'
import { addTemplate, createResolver, findPath, useNuxt, tryResolveModule } from '@nuxt/kit'
import type { Arrayable, InjectPosition, ModuleOptions } from './types'

/**
Expand Down Expand Up @@ -60,11 +60,22 @@ export const resolveModulePaths = async (configPath: ModuleOptions['configPath']
* @param nuxt
* @returns [resolvedCss, loggerMessage]
*/
export function resolveCSSPath (cssPath: ModuleOptions['cssPath'], nuxt = useNuxt()): [string, string] {
export async function resolveCSSPath (cssPath: ModuleOptions['cssPath'], nuxt = useNuxt()): Promise<[string, string]> {
if (typeof cssPath === 'string') {
return existsSync(cssPath)
? [cssPath, `Using Tailwind CSS from ~/${relative(nuxt.options.srcDir, cssPath)}`]
: ['tailwindcss/tailwind.css', 'Using default Tailwind CSS file']
: await tryResolveModule('tailwindcss/package.json')
.then(twLocation => twLocation ? [join(twLocation, '../tailwind.css'), 'Using default Tailwind CSS file'] : Promise.reject('tailwindcss not resolved'))
.catch(e => [
createResolver(import.meta.url).resolve(
addTemplate({
filename: '_tailwind.css',
write: true,
getContents: () => '@tailwind base;\n@tailwind components;\n@tailwind utilities;'
}).dst
),
`Faced error while trying to use default Tailwind CSS file: ${e?.name || ''} ${e?.message || e}\nCreated default Tailwind CSS file`
]) as [string, string]
} else {
return [
createResolver(import.meta.url).resolve(
Expand Down

0 comments on commit 52356f9

Please sign in to comment.