Skip to content

Commit

Permalink
fix(exposeConfig): maximize nested exports (#629)
Browse files Browse the repository at this point in the history
* feat: maximize nested exports

* fix: updating tests
  • Loading branch information
ineshbose authored Mar 14, 2023
1 parent 7c4561f commit e2ca518
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
22 changes: 17 additions & 5 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,23 @@ export default defineNuxtModule<ModuleOptions>({
Array.isArray(value) || // arrays are objects in JS, but we can't break it down
Object.keys(value).find(k => !k.match(/^[0-9a-z]+$/i)) // object has non-alphanumeric property (unsafe var name)
) {
addTemplate({
filename: `tailwind.config/${subpath}.mjs`,
getContents: () => `export default ${JSON.stringify(value, null, 2)}`
})
dtsContent.push(`declare module "#tailwind-config/${subpath}" { const defaultExport: ${JSON.stringify(value)}; export default defaultExport; }`)
if (typeof value === 'object' && !Array.isArray(value)) {
const validKeys: string[] = []
const invalidKeys: string[] = []
Object.keys(value).forEach(i => (/^[0-9a-z]+$/i.test(i) ? validKeys : invalidKeys).push(i))

addTemplate({
filename: `tailwind.config/${subpath}.mjs`,
getContents: () => `${validKeys.map(i => `const _${i} = ${JSON.stringify(value[i])}`).join('\n')}\nconst config = { ${validKeys.map(i => `"${i}": _${i}, `).join('')}${invalidKeys.map(i => `"${i}": ${JSON.stringify(value[i])}, `).join('')} }\nexport { config as default${validKeys.length > 0 ? ', _' : ''}${validKeys.join(', _')} }`
})
dtsContent.push(`declare module "#tailwind-config/${subpath}" { ${validKeys.map(i => `export const _${i}: ${JSON.stringify(value[i])};`).join('')} const defaultExport: { ${validKeys.map(i => `"${i}": typeof _${i}, `).join('')}${invalidKeys.map(i => `"${i}": ${JSON.stringify(value[i])}, `).join('')} }; export default defaultExport; }`)
} else {
addTemplate({
filename: `tailwind.config/${subpath}.mjs`,
getContents: () => `export default ${JSON.stringify(value, null, 2)}`
})
dtsContent.push(`declare module "#tailwind-config/${subpath}" { const defaultExport: ${JSON.stringify(value)}; export default defaultExport; }`)
}
} else {
// recurse through nested objects
populateMap(value, path.concat(key), level + 1, maxLevel)
Expand Down
11 changes: 9 additions & 2 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,15 @@ describe('tailwindcss module', async () => {

test('expose config', () => {
const nuxt = useTestContext().nuxt
const vfsKey = Object.keys(nuxt.vfs).find(k => k.includes('tailwind.config/theme/animation.'))
const vfsKey = Object.keys(nuxt.vfs).find(k => k.includes('tailwind.config/theme/flexBasis.'))
// check default tailwind default animation exists
expect(nuxt.vfs[vfsKey]).contains('"pulse": "pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite"')
expect(nuxt.vfs[vfsKey]).contains('"full": _full, "0.5": "0.125rem"')
expect(nuxt.vfs[vfsKey]).contains('export { config as default')
})

test('expose config variable', () => {
const nuxt = useTestContext().nuxt
const vfsKey = Object.keys(nuxt.vfs).find(k => k.includes('tailwind.config/theme/animation.'))
expect(nuxt.vfs[vfsKey]).contains('const _pulse = "pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite"')
})
})

0 comments on commit e2ca518

Please sign in to comment.