chore: add prefix option to init

This commit is contained in:
zernonia 2024-03-05 11:58:03 +08:00
parent b0952e5212
commit 62e6dc978a
6 changed files with 26 additions and 8 deletions

View File

@ -28,6 +28,7 @@ import {
resolveConfigPaths, resolveConfigPaths,
} from '../utils/get-config' } from '../utils/get-config'
import { transformCJSToESM } from '../utils/transformers/transform-cjs-to-esm' import { transformCJSToESM } from '../utils/transformers/transform-cjs-to-esm'
import { applyPrefixesCss } from '../utils/transformers/transform-tw-prefix'
const PROJECT_DEPENDENCIES = { const PROJECT_DEPENDENCIES = {
base: [ base: [
@ -150,6 +151,14 @@ export async function promptForConfig(
active: 'yes', active: 'yes',
inactive: 'no', inactive: 'no',
}, },
{
type: 'text',
name: 'tailwindPrefix',
message: `Are you using a custom ${highlight(
'tailwind prefix eg. tw-',
)}? (Leave blank if not)`,
initial: '',
},
{ {
type: 'text', type: 'text',
name: 'tailwindConfig', name: 'tailwindConfig',
@ -186,6 +195,7 @@ export async function promptForConfig(
css: options.tailwindCss, css: options.tailwindCss,
baseColor: options.tailwindBaseColor, baseColor: options.tailwindBaseColor,
cssVariables: options.tailwindCssVariables, cssVariables: options.tailwindCssVariables,
prefix: options.tailwindPrefix,
}, },
aliases: { aliases: {
utils: options.utils, utils: options.utils,
@ -246,8 +256,8 @@ export async function runInit(cwd: string, config: Config) {
transformCJSToESM( transformCJSToESM(
config.resolvedPaths.tailwindConfig, config.resolvedPaths.tailwindConfig,
config.tailwind.cssVariables config.tailwind.cssVariables
? template(templates.TAILWIND_CONFIG_WITH_VARIABLES)({ extension, framework: config.framework }) ? template(templates.TAILWIND_CONFIG_WITH_VARIABLES)({ extension, framework: config.framework, prefix: config.tailwind.prefix })
: template(templates.TAILWIND_CONFIG)({ extension, framework: config.framework }), : template(templates.TAILWIND_CONFIG)({ extension, framework: config.framework, prefix: config.tailwind.prefix }),
), ),
'utf8', 'utf8',
) )
@ -258,7 +268,9 @@ export async function runInit(cwd: string, config: Config) {
await fs.writeFile( await fs.writeFile(
config.resolvedPaths.tailwindCss, config.resolvedPaths.tailwindCss,
config.tailwind.cssVariables config.tailwind.cssVariables
? baseColor.cssVarsTemplate ? config.tailwind.prefix
? applyPrefixesCss(baseColor.cssVarsTemplate, config.tailwind.prefix)
: baseColor.cssVarsTemplate
: baseColor.inlineColorsTemplate, : baseColor.inlineColorsTemplate,
'utf8', 'utf8',
) )

View File

@ -35,7 +35,7 @@ export const rawConfigSchema = z
aliases: z.object({ aliases: z.object({
components: z.string(), components: z.string(),
utils: z.string(), utils: z.string(),
ui: z.string().optional(), ui: z.string().default('').optional(),
}), }),
}) })
.strict() .strict()

View File

@ -17,6 +17,7 @@ module.exports = {
'./app/**/*.{<%- extension %>,<%- extension %>x,vue}', './app/**/*.{<%- extension %>,<%- extension %>x,vue}',
'./src/**/*.{<%- extension %>,<%- extension %>x,vue}', './src/**/*.{<%- extension %>,<%- extension %>x,vue}',
], ],
prefix: "<%- prefix %>",
theme: { theme: {
container: { container: {
center: true, center: true,
@ -51,6 +52,7 @@ export const TAILWIND_CONFIG_WITH_VARIABLES = `const animate = require("tailwind
module.exports = { module.exports = {
darkMode: ["class"], darkMode: ["class"],
safelist: ["dark"], safelist: ["dark"],
prefix: "<%- prefix %>",
<% if (framework === 'vite') { %> <% if (framework === 'vite') { %>
content: [ content: [
'./pages/**/*.{<%- extension %>,<%- extension %>x,vue}', './pages/**/*.{<%- extension %>,<%- extension %>x,vue}',

View File

@ -4,7 +4,8 @@
"config": "tailwind.config.ts", "config": "tailwind.config.ts",
"css": "src/app/globals.css", "css": "src/app/globals.css",
"baseColor": "zinc", "baseColor": "zinc",
"cssVariables": true "cssVariables": true,
"prefix": "tw-"
}, },
"aliases": { "aliases": {
"utils": "~/lib/utils", "utils": "~/lib/utils",

View File

@ -12,6 +12,7 @@ export default {
'./app/**/*.{<%- extension %>,<%- extension %>x,vue}', './app/**/*.{<%- extension %>,<%- extension %>x,vue}',
'./src/**/*.{<%- extension %>,<%- extension %>x,vue}', './src/**/*.{<%- extension %>,<%- extension %>x,vue}',
], ],
prefix: \\"<%- prefix %>\\",
theme: { theme: {
container: { container: {
center: true, center: true,
@ -48,6 +49,7 @@ exports[`handle tailwind config template correctly 2`] = `
export default { export default {
darkMode: [\\"class\\"], darkMode: [\\"class\\"],
safelist: [\\"dark\\"], safelist: [\\"dark\\"],
prefix: \\"<%- prefix %>\\",
<% if (framework === 'vite') { %> <% if (framework === 'vite') { %>
content: [ content: [
'./pages/**/*.{<%- extension %>,<%- extension %>x,vue}', './pages/**/*.{<%- extension %>,<%- extension %>x,vue}',

View File

@ -1,9 +1,9 @@
import path from 'pathe' import path from 'pathe'
import { expect, test } from 'vitest' import { expect, it } from 'vitest'
import { getConfig, getRawConfig } from '../../src/utils/get-config' import { getConfig, getRawConfig } from '../../src/utils/get-config'
test('get raw config', async () => { it('get raw config', async () => {
expect( expect(
await getRawConfig(path.resolve(__dirname, '../fixtures/config-none')), await getRawConfig(path.resolve(__dirname, '../fixtures/config-none')),
).toEqual(null) ).toEqual(null)
@ -31,7 +31,7 @@ test('get raw config', async () => {
).rejects.toThrowError() ).rejects.toThrowError()
}) })
test('get config', async () => { it('get config', async () => {
expect( expect(
await getConfig(path.resolve(__dirname, '../fixtures/config-none')), await getConfig(path.resolve(__dirname, '../fixtures/config-none')),
).toEqual(null) ).toEqual(null)
@ -94,6 +94,7 @@ test('get config', async () => {
baseColor: 'zinc', baseColor: 'zinc',
css: 'src/app/globals.css', css: 'src/app/globals.css',
cssVariables: true, cssVariables: true,
prefix: 'tw-',
}, },
aliases: { aliases: {
components: '~/components', components: '~/components',