-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(module): HMR support with
@nuxtjs/tailwindcss
(#1665)
Co-authored-by: Benjamin Canac <[email protected]>
- Loading branch information
1 parent
bd3fa86
commit 821e15b
Showing
8 changed files
with
10,053 additions
and
6,968 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { join } from 'pathe' | ||
import { defu } from 'defu' | ||
import { addTemplate, createResolver, installModule, useNuxt } from '@nuxt/kit' | ||
|
||
import { setGlobalColors } from './runtime/utils/colors' | ||
import type { ModuleOptions } from './module' | ||
|
||
export default async function installTailwind ( | ||
moduleOptions: ModuleOptions, | ||
nuxt = useNuxt(), | ||
resolve = createResolver(import.meta.url).resolve | ||
) { | ||
const runtimeDir = resolve('./runtime') | ||
|
||
// 1. register hook | ||
// @ts-ignore | ||
nuxt.hook('tailwindcss:config', function (tailwindConfig) { | ||
tailwindConfig.theme = tailwindConfig.theme || {} | ||
tailwindConfig.theme.extend = tailwindConfig.theme.extend || {} | ||
tailwindConfig.theme.extend.colors = | ||
tailwindConfig.theme.extend.colors || {} | ||
|
||
const colors = setGlobalColors(tailwindConfig.theme) | ||
|
||
// @ts-ignore | ||
nuxt.options.appConfig.ui = { | ||
primary: 'green', | ||
gray: 'cool', | ||
colors, | ||
strategy: 'merge' | ||
} | ||
}) | ||
|
||
// 2. add config template | ||
const configTemplate = addTemplate({ | ||
filename: 'nuxtui-tailwind.config.cjs', | ||
write: true, | ||
getContents: ({ nuxt }) => ` | ||
const { defaultExtractor: createDefaultExtractor } = require('tailwindcss/lib/lib/defaultExtractor.js') | ||
const { customSafelistExtractor, generateSafelist } = require(${JSON.stringify(resolve(runtimeDir, 'utils', 'colors'))}) | ||
const { iconsPlugin, getIconCollections } = require('@egoist/tailwindcss-icons') | ||
const defaultExtractor = createDefaultExtractor({ tailwindConfig: { separator: ':' } }) | ||
module.exports = { | ||
plugins: [ | ||
require('@tailwindcss/forms')({ strategy: 'class' }), | ||
require('@tailwindcss/aspect-ratio'), | ||
require('@tailwindcss/typography'), | ||
require('@tailwindcss/container-queries'), | ||
require('@headlessui/tailwindcss'), | ||
iconsPlugin(${Array.isArray(moduleOptions.icons) || moduleOptions.icons === 'all' ? `{ collections: getIconCollections(${JSON.stringify(moduleOptions.icons)}) }` : typeof moduleOptions.icons === 'object' ? JSON.stringify(moduleOptions.icons) : {}}) | ||
], | ||
content: { | ||
files: [ | ||
${JSON.stringify(resolve(runtimeDir, 'components/**/*.{vue,mjs,ts}'))}, | ||
${JSON.stringify(resolve(runtimeDir, 'ui.config/**/*.{mjs,js,ts}'))} | ||
], | ||
transform: { | ||
vue: (content) => { | ||
return content.replaceAll(/(?:\\r\\n|\\r|\\n)/g, ' ') | ||
} | ||
}, | ||
extract: { | ||
vue: (content) => { | ||
return [ | ||
...defaultExtractor(content), | ||
...customSafelistExtractor(${JSON.stringify(moduleOptions.prefix)}, content, ${JSON.stringify(nuxt.options.appConfig.ui.colors)}, ${JSON.stringify(moduleOptions.safelistColors)}) | ||
] | ||
} | ||
} | ||
}, | ||
safelist: generateSafelist(${JSON.stringify(moduleOptions.safelistColors || [])}, ${JSON.stringify(nuxt.options.appConfig.ui.colors)}), | ||
} | ||
` | ||
}) | ||
|
||
// 3. install module | ||
await installModule('@nuxtjs/tailwindcss', defu({ | ||
exposeConfig: true, | ||
config: { darkMode: 'class' }, | ||
configPath: [ | ||
configTemplate.dst, | ||
join(nuxt.options.rootDir, 'tailwind.config') | ||
] | ||
}, nuxt.options.tailwindcss)) | ||
} |
Oops, something went wrong.