Skip to content

Commit

Permalink
chore(module): safelist dynamic colors
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincanac committed Feb 14, 2022
1 parent 382e3b8 commit 4a64885
Show file tree
Hide file tree
Showing 5 changed files with 315 additions and 245 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
},
"dependencies": {
"@headlessui/vue": "^1.4.3",
"@iconify/vue": "^3.1.3",
"@nuxt/kit": "npm:@nuxt/kit-edge@latest",
"@popperjs/core": "^2.11.2",
"@vueuse/core": "^7.6.1",
"@nuxtjs/tailwindcss": "^5.0.0-4",
"@popperjs/core": "^2.11.2",
"@tailwindcss/aspect-ratio": "^0.4.0",
"@tailwindcss/forms": "^0.4.0",
"@tailwindcss/line-clamp": "^0.3.1",
"@iconify/vue": "^3.1.3",
"@vueuse/core": "^7.6.1",
"defu": "^5.0.1",
"lodash-es": "^4.17.21",
"nanoid": "^3.2.0",
Expand All @@ -41,8 +41,8 @@
"devDependencies": {
"@nuxt/module-builder": "^0.1.7",
"@nuxtjs/eslint-config-typescript": "^8.0.0",
"@types/tailwindcss": "^3.0.7",
"@types/lodash-es": "^4.17.6",
"@types/tailwindcss": "^3.0.7",
"@vueuse/components": "^7.6.1",
"eslint": "^8.8.0",
"nuxt3": "latest",
Expand Down
11 changes: 10 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import defu from 'defu'
import type { TailwindConfig } from 'tailwindcss/tailwind-config'
import colors from 'tailwindcss/colors.js'
import { name, version } from '../package.json'
import { safeColorsAsRegex } from './utils'

interface ColorsOptions {
/**
Expand Down Expand Up @@ -87,7 +88,15 @@ export default defineNuxtModule<ModuleOptions>({
content: [
resolve(runtimeDir, 'components/**/*.{vue,js,ts}'),
resolve(runtimeDir, 'presets/*.{mjs,js,ts}')
]
],
// Safelist dynamic colors used in preset
safelist: [{
pattern: new RegExp(`bg-(${safeColorsAsRegex})-(100|600|700)`)
}, {
pattern: new RegExp(`text-(${safeColorsAsRegex})-(100|800)`)
}, {
pattern: new RegExp(`ring-(${safeColorsAsRegex})-(500)`)
}]
},
cssPath: resolve(runtimeDir, 'tailwind.css')
})
Expand Down
25 changes: 3 additions & 22 deletions src/runtime/presets/default.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
const colors = [
'primary',
'rose',
'pink',
'fuchsia',
'purple',
'violet',
'indigo',
'blue',
'sky',
'cyan',
'teal',
'emerald',
'green',
'lime',
'yellow',
'amber',
'orange',
'red'
]
import { safeColors } from '../../utils'

const button = {
base: 'font-medium focus:outline-none disabled:cursor-not-allowed disabled:opacity-75 focus:ring-offset-white dark:focus:ring-offset-black',
Expand Down Expand Up @@ -46,7 +27,7 @@ const button = {
xl: 'p-3'
},
variant: {
...colors.reduce((acc: any, color) => {
...safeColors.reduce((acc: any, color) => {
acc[color] = `shadow-sm border border-transparent text-white bg-${color}-600 hover:bg-${color}-700 disabled:bg-${color}-600 focus:ring-2 focus:ring-offset-2 focus:ring-${color}-500`
return acc
}, {}),
Expand Down Expand Up @@ -99,7 +80,7 @@ const badge = {
xl: 'text-sm px-4 py-1'
},
variant: {
...colors.reduce((acc: any, color) => {
...safeColors.reduce((acc: any, color) => {
acc[color] = `bg-${color}-100 dark:bg-${color}-700 text-${color}-800 dark:text-${color}-100`
return acc
}, {})
Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import colors from 'tailwindcss/colors.js'
import { without, kebabCase } from 'lodash-es'

export const safeColors = without(Object.keys(colors).map(kebabCase), 'inherit', 'current', 'transparent', 'black', 'white', 'gray')

export const safeColorsAsRegex = safeColors.join('|')
Loading

1 comment on commit 4a64885

@vercel
Copy link

@vercel vercel bot commented on 4a64885 Feb 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

Please sign in to comment.