-
Notifications
You must be signed in to change notification settings - Fork 360
feat(compiler): add tailwind css variables generation from vuestic.co… #4560
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
Open
m0ksem
wants to merge
5
commits into
epicmaxco:develop
Choose a base branch
from
m0ksem:feat/compiler_tailwind
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ec6d802
feat(compiler): add tailwind css variables generation from vuestic.co…
m0ksem aa55e3e
chore(compiler/tailwind): minor fixes
m0ksem e01ba82
fix: re-transform tailwind when vuestic-config is updated
m0ksem 7b4f6b7
fix(docs): tailwind upgrade
m0ksem bdfe6ce
chore: typo
m0ksem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 @@ | ||
@import "tailwindcss"; |
This file contains hidden or 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { createApp } from 'vue' | ||
import App from './App.vue' | ||
import './main.css' | ||
|
||
createApp(App) | ||
.mount('#app') |
This file contains hidden or 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 hidden or 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,26 @@ | ||
import { existsSync, watch } from 'fs' | ||
import { dirname } from 'path' | ||
|
||
export const watchFileChangeOnce = (path: string, cb: () => void) => { | ||
if (!existsSync(path)) { | ||
const watcher = watch(dirname(path), { recursive: true }, (eventType, filename) => { | ||
if (filename === path) { | ||
cb() | ||
} | ||
|
||
watcher.close() | ||
}) | ||
|
||
return | ||
} | ||
|
||
const watcher = watch( | ||
path, | ||
(eventType, filename) => { | ||
if (filename) { | ||
cb() | ||
watcher.close() | ||
} | ||
} | ||
) | ||
} |
This file contains hidden or 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,48 @@ | ||
import ts from 'typescript' | ||
import { basename, dirname, resolve } from "path"; | ||
import { writeFile, mkdtemp, rm } from 'fs/promises' | ||
import { randomUUID } from 'crypto' | ||
import { logger } from '../logger'; | ||
|
||
export const executeModule = async <T>(scriptCode: string, filePath: string) => { | ||
if (!filePath) { | ||
filePath = randomUUID() | ||
} | ||
|
||
const fileName = basename(filePath) | ||
const dirName = dirname(filePath) | ||
|
||
const tempFileName = resolve(dirName, fileName + `${randomUUID()}-vc.mjs`) | ||
|
||
try { | ||
await writeFile(tempFileName, scriptCode) | ||
|
||
const module = await import(tempFileName) | ||
|
||
return module as T | ||
} | ||
catch (e) { | ||
logger.error(typeof e === 'string' ? e : e instanceof Error ? e.message : 'Unknown error', { | ||
timestamp: true | ||
}) | ||
} | ||
finally { | ||
await rm(tempFileName, { recursive: true, force: true }) | ||
} | ||
}; | ||
|
||
export const executeTsModule = async <T>(scriptCode: string, filePath: string) => { | ||
const transpiled = transpileTs(scriptCode) | ||
|
||
return executeModule<T>(transpiled.outputText, filePath) | ||
} | ||
|
||
export const transpileTs = (code: string) => { | ||
return ts.transpileModule(code, { | ||
compilerOptions: { | ||
module: ts.ModuleKind.ESNext, | ||
target: ts.ScriptTarget.ESNext, | ||
strict: false, | ||
}, | ||
}) | ||
} |
This file contains hidden or 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,49 @@ | ||
import { existsSync } from 'node:fs' | ||
import { readFile } from 'node:fs/promises' | ||
import { resolve } from 'node:path' | ||
import { executeTsModule } from './ts' | ||
import { type GlobalConfig } from 'vuestic-ui' | ||
import { watchFileChangeOnce } from './fs' | ||
|
||
const POSSIBLE_CONFIG_FILES = [ | ||
'./vuestic.config.ts', | ||
'./vuestic.config.js', | ||
'./vuestic.config.mjs', | ||
'./vuestic.config.cjs', | ||
'./vuestic.config.mts', | ||
] | ||
|
||
export const resolveVuesticConfigPath = () => { | ||
for (const file of POSSIBLE_CONFIG_FILES) { | ||
const absolutePath = resolve(file) | ||
if (existsSync(absolutePath)) { | ||
return absolutePath | ||
} | ||
} | ||
} | ||
|
||
export const tryToReadConfig = async (path: string | undefined = resolveVuesticConfigPath()) => { | ||
if (path && existsSync(path)) { | ||
const absolutePath = resolve(path) | ||
const source = await readFile(absolutePath) | ||
const { default: config } = await executeTsModule<{ default: GlobalConfig }>(source.toString(), absolutePath) ?? {} | ||
|
||
return config | ||
} | ||
|
||
return null | ||
} | ||
|
||
export const watchVuesticConfigOnce = async (onChange: () => void) => { | ||
let config = await tryToReadConfig() | ||
|
||
for (const file of POSSIBLE_CONFIG_FILES) { | ||
const absolutePath = resolve(file) | ||
|
||
watchFileChangeOnce(absolutePath, () => { | ||
onChange() | ||
}) | ||
} | ||
|
||
return config | ||
} |
This file contains hidden or 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,79 @@ | ||
import { readFile, writeFile } from 'fs/promises'; | ||
import { MagicString } from '@vue/compiler-sfc'; | ||
import { Plugin } from 'vite'; | ||
import { watchVuesticConfigOnce } from '../shared/vuestic-config'; | ||
import { colorsPreset } from 'vuestic-ui' | ||
import { logger } from '../logger'; | ||
|
||
const kebabCase = (str: string) => { | ||
return str.replace(/([a-z])([A-Z])/g, '$1-$2') | ||
.replace(/([A-Z])([A-Z][a-z])/g, '$1-$2') | ||
.toLowerCase(); | ||
} | ||
|
||
const defaultColors = Object.keys(colorsPreset.light) | ||
|
||
function uniqueArray<T>(array: T[]): T[] { | ||
return Array.from(new Set(array)); | ||
} | ||
|
||
function addCssVariables(colors: Record<string, string>): string { | ||
const colorVariables = uniqueArray([...defaultColors, ...Object.keys(colors)]).map((key) => `--color-${kebabCase(key)}: var(--va-${kebabCase(key)});`).join('\n '); | ||
|
||
return ` | ||
@theme { | ||
${colorVariables} | ||
} | ||
` | ||
} | ||
|
||
const TAILWIND_CSS_IMPORT_TEMPLATE = `@import "tailwindcss";` | ||
|
||
export const vuesticTailwind = () => { | ||
let server: import('vite').ViteDevServer | ||
|
||
return { | ||
name: 'vuestic:tailwindcss', | ||
|
||
enforce: 'pre', | ||
|
||
configResolved(config) { | ||
const tailwindPluginIndex = config.plugins.findIndex((p) => p.name === '@tailwindcss/vite:scan') | ||
const vuesticTailwindPluginIndex = config.plugins.findIndex((p) => p.name === 'vuestic:tailwindcss') | ||
|
||
if (tailwindPluginIndex !== -1 && vuesticTailwindPluginIndex !== -1 && tailwindPluginIndex < vuesticTailwindPluginIndex) { | ||
logger.warn('[Vuestic] vuestic plugin should be placed before tailwindcss plugin in the Vite config plugins array.', { timestamp: true }); | ||
} | ||
}, | ||
|
||
configureServer(_s) { | ||
server = _s; | ||
}, | ||
|
||
async transform(code, id, options) { | ||
if (!id.endsWith('css')) { | ||
return | ||
} | ||
|
||
if (!code.includes(TAILWIND_CSS_IMPORT_TEMPLATE)) { | ||
return | ||
} | ||
|
||
const ms = new MagicString(code); | ||
|
||
const config = (await watchVuesticConfigOnce(async () => { | ||
// trigger full re-transform of id | ||
await writeFile(id, ((await readFile(id)).toString())) | ||
|
||
server?.ws.send({ type: 'full-reload', path: '*' }); | ||
})) ?? { colors: { variables: {} } }; | ||
|
||
ms.appendRight(code.indexOf(TAILWIND_CSS_IMPORT_TEMPLATE) + TAILWIND_CSS_IMPORT_TEMPLATE.length, addCssVariables(config.colors.variables)); | ||
|
||
return { | ||
code: ms.toString(), | ||
map: ms.generateMap() | ||
} | ||
}, | ||
} as Plugin | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.