-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
100 additions
and
0 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
51 changes: 51 additions & 0 deletions
51
packages/vite-plugin-drupal/src/plugins/tailwind-config.ts
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,51 @@ | ||
import process from 'node:process' | ||
import type { Plugin, ResolvedConfig } from 'vite' | ||
import fse from 'fs-extra' | ||
import YAML from 'yaml' | ||
import resolveConfig from 'tailwindcss/resolveConfig.js' | ||
import type { Context } from './context' | ||
|
||
export default (ctx: Context): Plugin => { | ||
let config: ResolvedConfig | ||
return { | ||
name: 'vite-plugin-uebertool-tailwind-config', | ||
configResolved(resolvedConfig) { | ||
config = resolvedConfig | ||
}, | ||
async handleHotUpdate({ file }) { | ||
if (file.match(/tailwind.config/)) { | ||
await fse.outputFile(`${config.build.outDir}/${ctx.distThemeName}.tailwind.yml`, await buildTailwindConfig()) | ||
} | ||
}, | ||
async buildStart() { | ||
if (ctx.prod) | ||
return | ||
|
||
await fse.outputFile(`${config.build.outDir}/${ctx.distThemeName}.tailwind.yml`, await buildTailwindConfig()) | ||
}, | ||
async generateBundle(_, bundle, isWrite) { | ||
const tailwindConfig = await buildTailwindConfig() | ||
if (!isWrite || ctx.dev) | ||
return | ||
|
||
this.emitFile({ | ||
type: 'asset', | ||
name: `${ctx.distThemeName}.tailwind.yml`, | ||
fileName: `${ctx.distThemeName}.tailwind.yml`, | ||
source: tailwindConfig, | ||
}) | ||
}, | ||
} | ||
} | ||
|
||
async function buildTailwindConfig() { | ||
try { | ||
const tailwindConfigFile = await import(`${process.cwd()}/tailwind.config.js`) | ||
const tailwindConfig = resolveConfig(tailwindConfigFile.default) | ||
return YAML.stringify(tailwindConfig.theme) | ||
} | ||
catch (e) { | ||
console.warn(`No tailwind.config.js file found in ${process.cwd()}!\n${e}`) | ||
return '' | ||
} | ||
} |