(moduleOptions, nuxt)
| 38 | export default defineNuxtModule<ModuleOptions>({ |
| 39 | meta: { name, version, configKey, compatibility }, defaults, |
| 40 | async setup(moduleOptions, nuxt) { |
| 41 | if (moduleOptions.quiet) logger.level = LogLevels.silent |
| 42 | |
| 43 | // install postcss8 module on nuxt < 2.16 |
| 44 | if (Number.parseFloat(getNuxtVersion()) < 2.16) { |
| 45 | await installModule('@nuxt/postcss8').catch((e) => { |
| 46 | logger.error(`Error occurred while loading \`@nuxt/postcss8\` required for Nuxt ${getNuxtVersion()}, is it installed?`) |
| 47 | throw e |
| 48 | }) |
| 49 | } |
| 50 | |
| 51 | const isTailwind4 = await readPackageJSON('tailwindcss', { parent: import.meta.url }).then(m => Number.parseFloat(m.version!) >= 4) |
| 52 | |
| 53 | if (isTailwind4 && !moduleOptions.experimental?.tailwindcss4) { |
| 54 | logger.warn('Tailwind CSS v4 detected. The current version of `@nuxtjs/tailwindcss` supports Tailwind CSS 3 officially and support for v4 is experimental. To suppress this warning, set `tailwindcss.experimental.tailwindcss4` to `true` in your `nuxt.config`.') |
| 55 | } |
| 56 | |
| 57 | const ctx = await createInternalContext(moduleOptions, nuxt) |
| 58 | |
| 59 | if (moduleOptions.editorSupport) { |
| 60 | const editorSupportConfig = resolvers.resolveEditorSupportConfig(moduleOptions.editorSupport) |
| 61 | |
| 62 | if ((editorSupportConfig.autocompleteUtil) && !isNuxtMajorVersion(2, nuxt)) { |
| 63 | addImports({ |
| 64 | name: 'autocompleteUtil', |
| 65 | from: createResolver(import.meta.url).resolve('./runtime/utils'), |
| 66 | as: 'tw', |
| 67 | ...(typeof editorSupportConfig.autocompleteUtil === 'object' ? editorSupportConfig.autocompleteUtil : {}), |
| 68 | }) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // css file handling |
| 73 | const [cssPath, cssPathConfig] = Array.isArray(moduleOptions.cssPath) ? moduleOptions.cssPath : [moduleOptions.cssPath] |
| 74 | const [resolvedCss, loggerInfo] = await resolvers.resolveCSSPath(cssPath, nuxt).catch((e) => { |
| 75 | if (isTailwind4) { |
| 76 | return [addTemplate({ filename: 'tailwind.css', getContents: () => `@import 'tailwindcss';`, write: true }).dst, 'Generating default CSS file for Tailwind CSS 4...'] |
| 77 | } |
| 78 | throw e |
| 79 | }) |
| 80 | logger.info(loggerInfo) |
| 81 | |
| 82 | nuxt.options.css = nuxt.options.css ?? [] |
| 83 | const resolvedNuxtCss = (resolvedCss && await Promise.all(nuxt.options.css.map((p: any) => resolvePath(p.src ?? p)))) || [] |
| 84 | |
| 85 | // inject only if this file isn't listed already by user |
| 86 | if (resolvedCss && !resolvedNuxtCss.includes(resolvedCss)) { |
| 87 | const injectPosition = await resolvers.resolveInjectPosition(resolvedNuxtCss, cssPathConfig?.injectPosition) |
| 88 | nuxt.options.css.splice(injectPosition, 0, resolvedCss) |
| 89 | } |
| 90 | |
| 91 | const shouldInstallTWVitePlugin = isTailwind4 && nuxt.options.builder === '@nuxt/vite-builder' |
| 92 | if (shouldInstallTWVitePlugin) { |
| 93 | // @ts-expect-error may not be installed |
| 94 | await import('@tailwindcss/vite').then(r => addVitePlugin(r.default())) |
| 95 | } |
| 96 | |
| 97 | // workaround for nuxt2 middleware race condition |
nothing calls this directly
no test coverage detected