Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable c12 for loading config and provide defineConfig #913

Merged
merged 16 commits into from
Nov 27, 2024
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@
},
"exports": {
".": {
"types": "./dist/types.d.ts",
"types": "./dist/module.d.ts",
"require": "./dist/module.cjs",
"import": "./dist/module.mjs"
},
"./config": {
"types": "./dist/config.d.ts",
"require": "./dist/config.cjs",
"import": "./dist/config.mjs"
}
},
"main": "./dist/module.cjs",
"types": "./dist/types.d.ts",
"files": [
"dist"
],
"build": {
"entries": ["./src/config"],
"rollup": { "emitCJS": true }
},
"scripts": {
"play": "pnpm dev",
"prepare": "nuxt-module-build prepare",
Expand All @@ -46,6 +55,7 @@
"dependencies": {
"@nuxt/kit": "^3.14.1592",
"autoprefixer": "^10.4.20",
"c12": "^2.0.1",
"consola": "^3.2.3",
"defu": "^6.1.4",
"h3": "^1.13.0",
Expand Down
2 changes: 1 addition & 1 deletion playground/modules/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default defineNuxtModule((_, nuxt) => {

nuxt.options.tailwindcss = nuxt.options.tailwindcss ?? {}
if (!Array.isArray(nuxt.options.tailwindcss.configPath)) {
nuxt.options.tailwindcss.configPath = nuxt.options.tailwindcss.configPath ? [nuxt.options.tailwindcss.configPath] : ['tailwind.config']
nuxt.options.tailwindcss.configPath = nuxt.options.tailwindcss.configPath ? [nuxt.options.tailwindcss.configPath] : []
}
nuxt.options.tailwindcss.configPath.push(template.dst)
})
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createDefineConfig } from 'c12'
import type { Config } from 'tailwindcss'

export const defineConfig = createDefineConfig<Partial<Config>>()
export default defineConfig
235 changes: 0 additions & 235 deletions src/context.ts

This file was deleted.

12 changes: 8 additions & 4 deletions src/expose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ import { dirname, join } from 'pathe'
import { useNuxt, addTemplate, addTypeTemplate } from '@nuxt/kit'
import type { ResolvedNuxtTemplate } from 'nuxt/schema'
import type { ExposeConfig } from './types'
import { twCtx } from './context'
import { twCtx } from './internal-context/context'

const NON_ALPHANUMERIC_RE = /^[0-9a-z]+$/i
const isJSObject = (value: any) => typeof value === 'object' && !Array.isArray(value)

export const createExposeTemplates = (config: ExposeConfig, nuxt = useNuxt()) => {
const templates: ResolvedNuxtTemplate<any>[] = []
const getTWConfig = (objPath: string[] = []) => objPath.reduce((prev, curr) => prev?.[curr], twCtx.tryUse() as any)

const populateMap = (obj: any, path: string[] = [], level = 1) => {
const getTWConfig = (
objPath: string[] = [],
twConfig = twCtx.use().config,
ineshbose marked this conversation as resolved.
Show resolved Hide resolved
) => objPath.reduce((prev, curr) => prev?.[curr], twConfig as Record<string, any>)

const populateMap = (obj: any = twCtx.use().config, path: string[] = [], level = 1) => {
Object.entries(obj).forEach(([key, value = {} as any]) => {
const subpathComponents = path.concat(key)
const subpath = subpathComponents.join('/')
Expand Down Expand Up @@ -63,7 +67,7 @@ export const createExposeTemplates = (config: ExposeConfig, nuxt = useNuxt()) =>
})
}

populateMap(twCtx.tryUse())
populateMap()

const entryTemplate = addTemplate({
filename: 'tailwind.config/index.mjs',
Expand Down
20 changes: 20 additions & 0 deletions src/internal-context/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { defu } from 'defu'
import { getContext } from 'unctx'
import type { TWConfig } from '../types'

type Context = {
config: Partial<TWConfig>
dst: string
meta: {
disableHMR?: boolean
}
}

const twCtx = getContext<Partial<Context>>('twcss')
const { set } = twCtx

twCtx.set = (instance, replace = true) => {
set(defu(instance, twCtx.tryUse()), replace)
}

export { twCtx }
Loading