From df1677e00ebd6bdb157b68bcea90370988e2b608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ya=C3=ABl=20Guilloux?= Date: Thu, 19 Oct 2023 17:08:26 +0200 Subject: [PATCH] feat(build): improve build step again; clean from build in 1 command --- eslint.config.js | 1 + examples/nuxt/nuxt.config.ts | 3 -- integrations/astro/src/index.ts | 2 +- integrations/language-server/package.json | 4 +- integrations/react/src/index.ts | 1 - .../react/src/utils/runtime-plugin.ts | 23 ++++++------ integrations/react/src/utils/virtual.ts | 1 - .../repl/public/svelte-plugin-proxy.js | 24 ++++++------ .../src/components/monaco/volar.worker.ts | 5 ++- integrations/repl/src/store/index.ts | 2 +- integrations/repl/src/store/pinceau.ts | 6 +-- integrations/repl/vite.config.ts | 1 - integrations/svelte/src/index.ts | 1 - .../svelte/src/utils/runtime-plugin.ts | 23 ++++++------ integrations/svelte/src/utils/virtual.ts | 1 - integrations/vue/src/index.ts | 1 - integrations/vue/src/utils/virtual.ts | 1 - packages/core/src/utils/fs.ts | 2 +- packages/palette/package.json | 2 +- packages/palette/vite.config.base.ts | 37 +++++++++++++++++++ packages/runtime/src/index.ts | 1 - packages/style/src/index.ts | 1 - packages/theme/src/utils.ts | 2 + packages/theme/src/utils/build-dir.ts | 26 +++++++++++++ packages/theme/src/utils/config-file.ts | 6 +-- packages/theme/src/utils/config-layers.ts | 27 ++------------ packages/theme/src/utils/generate.ts | 11 ++---- packages/theme/src/utils/module-path.ts | 25 +++++++++++++ pnpm-lock.yaml | 29 ++++++++++++--- turbo.json | 6 ++- 30 files changed, 172 insertions(+), 103 deletions(-) create mode 100644 packages/theme/src/utils/build-dir.ts create mode 100644 packages/theme/src/utils/module-path.ts diff --git a/eslint.config.js b/eslint.config.js index 0a66d4e0..629c7905 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -30,6 +30,7 @@ export default antfu( '**/tests/fixtures/', '**/tests/tmp/', '.vscode', + '**/grammars/*.tmLanguage.js', ], }, ) diff --git a/examples/nuxt/nuxt.config.ts b/examples/nuxt/nuxt.config.ts index df0d0629..79674d07 100644 --- a/examples/nuxt/nuxt.config.ts +++ b/examples/nuxt/nuxt.config.ts @@ -18,9 +18,6 @@ export default defineNuxtConfig({ excludes: [ resolve('../../packages'), ], - }, - theme: { - buildDir: resolve('./.nuxt/pinceau/') } }, typescript: { diff --git a/integrations/astro/src/index.ts b/integrations/astro/src/index.ts index 7b6ba872..ee0e726b 100644 --- a/integrations/astro/src/index.ts +++ b/integrations/astro/src/index.ts @@ -1,4 +1,4 @@ -import { PinceauPluginsOptions } from '@pinceau/core' +import type { PinceauPluginsOptions } from '@pinceau/core' import type { PinceauAstroOptions } from './types' declare module '@pinceau/core' { diff --git a/integrations/language-server/package.json b/integrations/language-server/package.json index 4e3bde6b..1673804a 100644 --- a/integrations/language-server/package.json +++ b/integrations/language-server/package.json @@ -27,7 +27,9 @@ "stub": "unbuild --stub" }, "dependencies": { - "@pinceau/integration": "workspace:*", + "@pinceau/theme": "workspace:*", + "@pinceau/core": "workspace:*", + "@pinceau/style": "workspace:*", "@vue/compiler-sfc": "^3.3.4", "fast-glob": "^3.3.1", "line-column": "^1.0.2", diff --git a/integrations/react/src/index.ts b/integrations/react/src/index.ts index 030ea5b7..b09dd4c8 100644 --- a/integrations/react/src/index.ts +++ b/integrations/react/src/index.ts @@ -1,4 +1,3 @@ -import { PinceauPluginsOptions } from '@pinceau/core' import type { PinceauReactOptions } from './types' declare module '@pinceau/core' { diff --git a/integrations/react/src/utils/runtime-plugin.ts b/integrations/react/src/utils/runtime-plugin.ts index 0ac80a1b..dbeb3bcb 100644 --- a/integrations/react/src/utils/runtime-plugin.ts +++ b/integrations/react/src/utils/runtime-plugin.ts @@ -13,22 +13,21 @@ export function createReactPlugin(ctx: PinceauContext) { if (ctx.options.runtime) { imports.push('useRuntimeSheet') } return `import React, { createContext, useContext } from 'react' - import { ${imports.join(', ')} } from '@pinceau/runtime' +import { ${imports.join(', ')} } from '@pinceau/runtime' - export const PinceauReactOptions = ${JSON.stringify(runtimeOptions || {})} +export const PinceauReactOptions = ${JSON.stringify(runtimeOptions || {})} - const PinceauContext = createContext() +const PinceauContext = createContext() - export const usePinceauContext = () => useContext(PinceauContext) +export const usePinceauContext = () => useContext(PinceauContext) - export const PinceauProvider = ({ options, children }) => { - const userOptions = { ...PinceauReactOptions, ...options } +export const PinceauProvider = ({ options, children }) => { + const userOptions = { ...PinceauReactOptions, ...options } - ${ctx.options.theme ? 'const themeSheet = useThemeSheet(userOptions)' : ''} - ${ctx.options.runtime ? `const runtimeSheet = useRuntimeSheet(${ctx.options.theme ? '{ themeSheet, ...userOptions }' : 'userOptions'})` : ''} - ${ctx.options.runtime && !!runtimeOptions?.ssr ? 'const ssr = { toString: () => runtimeSheet.toString() }' : ''} + ${ctx.options.theme ? 'const themeSheet = useThemeSheet(userOptions)' : ''} + ${ctx.options.runtime ? `const runtimeSheet = useRuntimeSheet(${ctx.options.theme ? '{ themeSheet, ...userOptions }' : 'userOptions'})` : ''} + ${ctx.options.runtime && !!runtimeOptions?.ssr ? 'const ssr = { toString: () => runtimeSheet.toString() }' : ''} - return React.createElement(PinceauContext.Provider, { value: { themeSheet, runtimeSheet, ssr }, children }) - } -` + return React.createElement(PinceauContext.Provider, { value: { themeSheet, runtimeSheet, ssr }, children }) +}` } diff --git a/integrations/react/src/utils/virtual.ts b/integrations/react/src/utils/virtual.ts index 89f641b2..82480518 100644 --- a/integrations/react/src/utils/virtual.ts +++ b/integrations/react/src/utils/virtual.ts @@ -1,4 +1,3 @@ -import fs from 'node:fs' import { join } from 'pathe' import type { PinceauContext } from '@pinceau/core' import { writeOutput } from '@pinceau/core/utils' diff --git a/integrations/repl/public/svelte-plugin-proxy.js b/integrations/repl/public/svelte-plugin-proxy.js index 21f189b5..f5501d20 100644 --- a/integrations/repl/public/svelte-plugin-proxy.js +++ b/integrations/repl/public/svelte-plugin-proxy.js @@ -1,20 +1,20 @@ -import { useThemeSheet, useRuntimeSheet } from '@pinceau/runtime' +import { useRuntimeSheet, useThemeSheet } from '@pinceau/runtime' - export const PinceauSvelteOptions = {"dev":false,"colorSchemeMode":"media","computedStyles":true,"variants":true,"ssr":{"theme":true,"runtime":true},"appId":false} +export const PinceauSvelteOptions = { dev: false, colorSchemeMode: 'media', computedStyles: true, variants: true, ssr: { theme: true, runtime: true }, appId: false } - let userOptions +let userOptions - let themeSheet +let themeSheet export const getRuntimeSheet = () => runtimeSheet - let runtimeSheet +let runtimeSheet export const getThemeSheet = () => themeSheet - export const pinceauPlugin = (options) => { - userOptions = { ...PinceauSvelteOptions, ...options } - - themeSheet = useThemeSheet(userOptions) - runtimeSheet = useRuntimeSheet({ themeSheet, ...userOptions }) - } +export function pinceauPlugin(options) { + userOptions = { ...PinceauSvelteOptions, ...options } - export const ssr = { toString: () => runtimeSheet.toString() } + themeSheet = useThemeSheet(userOptions) + runtimeSheet = useRuntimeSheet({ themeSheet, ...userOptions }) +} + +export const ssr = { toString: () => runtimeSheet.toString() } diff --git a/integrations/repl/src/components/monaco/volar.worker.ts b/integrations/repl/src/components/monaco/volar.worker.ts index 25e3ca10..1b348dbc 100644 --- a/integrations/repl/src/components/monaco/volar.worker.ts +++ b/integrations/repl/src/components/monaco/volar.worker.ts @@ -87,7 +87,7 @@ self.onmessage = async (msg: MessageEvent) => { 'pug': () => ({}), 'emmet': () => ({}), 'pug-beautify': () => ({}), - ...(language !== 'vue' ? noopVue : {}) + ...(language !== 'vue' ? noopVue : {}), }, }, compilerOptions, @@ -98,7 +98,8 @@ self.onmessage = async (msg: MessageEvent) => { if (language === 'svelte') { // @ts-ignore serviceConfig.languages[0] = svelteLanguage - } else if (language !== 'vue') { + } + else if (language !== 'vue') { // @ts-ignore serviceConfig.languages[0] = typescriptLanguage } diff --git a/integrations/repl/src/store/index.ts b/integrations/repl/src/store/index.ts index 225a9d80..34c06da2 100644 --- a/integrations/repl/src/store/index.ts +++ b/integrations/repl/src/store/index.ts @@ -1,4 +1,4 @@ -import { nextTick, reactive, ref, watch, watchEffect } from 'vue' +import { reactive, ref, watch, watchEffect } from 'vue' import type { Ref, WatchStopHandle } from 'vue' import type { editor } from 'monaco-editor-core' import palette from '@pinceau/palette/theme.config?raw' diff --git a/integrations/repl/src/store/pinceau.ts b/integrations/repl/src/store/pinceau.ts index 7f8ed77c..571bfefa 100644 --- a/integrations/repl/src/store/pinceau.ts +++ b/integrations/repl/src/store/pinceau.ts @@ -15,12 +15,10 @@ import { import { normalizeTokens, } from '@pinceau/theme/runtime' -import { transformAddPinceauClass, transformAddRuntimeScriptTag, transformWriteScriptFeatures, suite as vueSuite } from '@pinceau/vue/transforms' +import { transformAddPinceauClass, transformAddRuntimeScriptTag, transformWriteScriptFeatures } from '@pinceau/vue/transforms' import { PinceauVueTransformer } from '@pinceau/vue/utils' import { PinceauSvelteTransformer } from '@pinceau/svelte/utils' -import { suite as svelteSuite } from '@pinceau/svelte/transforms' -import { suite as reactSuite } from '@pinceau/react/transforms' -import { findDefaultExport, parseAst, parsePinceauQuery, printAst, transform, usePinceauContext, usePinceauTransformContext, visitAst } from '@pinceau/core/utils' +import { findDefaultExport, parseAst, parsePinceauQuery, printAst, usePinceauContext, usePinceauTransformContext, visitAst } from '@pinceau/core/utils' import { suite as styleSuite } from '@pinceau/style/transforms' import { suite as themeSuite } from '@pinceau/theme/transforms' import type { PinceauContext } from '@pinceau/core' diff --git a/integrations/repl/vite.config.ts b/integrations/repl/vite.config.ts index 91c9ee4e..56ccfdbf 100644 --- a/integrations/repl/vite.config.ts +++ b/integrations/repl/vite.config.ts @@ -6,7 +6,6 @@ import vue from '@vitejs/plugin-vue' import replace from '@rollup/plugin-replace' import { nodePolyfills } from 'vite-plugin-node-polyfills' import Icons from 'unplugin-icons/vite' -import { env, node, nodeless } from 'unenv' const resolve = (p: string) => createResolver(import.meta.url).resolve(p) diff --git a/integrations/svelte/src/index.ts b/integrations/svelte/src/index.ts index 72e97e5c..dd73fb9b 100644 --- a/integrations/svelte/src/index.ts +++ b/integrations/svelte/src/index.ts @@ -1,4 +1,3 @@ -import { PinceauPluginsOptions } from '@pinceau/core' import type { PinceauSvelteOptions } from './types' declare module '@pinceau/core' { diff --git a/integrations/svelte/src/utils/runtime-plugin.ts b/integrations/svelte/src/utils/runtime-plugin.ts index 9449ecd8..bf064ca5 100644 --- a/integrations/svelte/src/utils/runtime-plugin.ts +++ b/integrations/svelte/src/utils/runtime-plugin.ts @@ -14,21 +14,20 @@ export function createSveltePlugin(ctx: PinceauContext) { return `import { ${imports.join(', ')} } from '@pinceau/runtime' - export const PinceauSvelteOptions = ${JSON.stringify(runtimeOptions || {})} +export const PinceauSvelteOptions = ${JSON.stringify(runtimeOptions || {})} - let userOptions +let userOptions - ${ctx.options.theme ? 'let themeSheet\nexport const getRuntimeSheet = () => runtimeSheet' : ''} +${ctx.options.theme ? 'let themeSheet\nexport const getThemeSheet = () => themeSheet' : ''} - ${ctx.options.runtime ? 'let runtimeSheet\nexport const getThemeSheet = () => themeSheet' : ''} +${ctx.options.runtime ? 'let runtimeSheet\nexport const getRuntimeSheet = () => runtimeSheet' : ''} - export const pinceauPlugin = (options) => { - userOptions = { ...PinceauSvelteOptions, ...options } - - ${ctx.options.theme ? 'themeSheet = useThemeSheet(userOptions)' : ''} - ${ctx.options.runtime ? `runtimeSheet = useRuntimeSheet(${ctx.options.theme ? '{ themeSheet, ...userOptions }' : 'userOptions'})` : ''} - } +export const pinceauPlugin = (options) => { + userOptions = { ...PinceauSvelteOptions, ...options } + + ${ctx.options.theme ? 'themeSheet = useThemeSheet(userOptions)' : ''} + ${ctx.options.runtime ? `runtimeSheet = useRuntimeSheet(${ctx.options.theme ? '{ themeSheet, ...userOptions }' : 'userOptions'})` : ''} +} - ${ctx.options.runtime && !!runtimeOptions?.ssr ? 'export const ssr = { toString: () => runtimeSheet.toString() }' : ''} -` +${ctx.options.runtime && !!runtimeOptions?.ssr ? 'export const ssr = { toString: () => runtimeSheet.toString() }' : ''}` } diff --git a/integrations/svelte/src/utils/virtual.ts b/integrations/svelte/src/utils/virtual.ts index a2f7a8a1..b2cf99e8 100644 --- a/integrations/svelte/src/utils/virtual.ts +++ b/integrations/svelte/src/utils/virtual.ts @@ -1,4 +1,3 @@ -import fs from 'node:fs' import { join } from 'pathe' import type { PinceauContext } from '@pinceau/core' import { writeOutput } from '@pinceau/core/utils' diff --git a/integrations/vue/src/index.ts b/integrations/vue/src/index.ts index 44359602..87b775f5 100644 --- a/integrations/vue/src/index.ts +++ b/integrations/vue/src/index.ts @@ -1,4 +1,3 @@ -import { PinceauPluginsOptions } from '@pinceau/core' import type { PinceauVueOptions } from './types' declare module '@pinceau/core' { diff --git a/integrations/vue/src/utils/virtual.ts b/integrations/vue/src/utils/virtual.ts index 8293358c..e6476f87 100644 --- a/integrations/vue/src/utils/virtual.ts +++ b/integrations/vue/src/utils/virtual.ts @@ -1,4 +1,3 @@ -import fs from 'node:fs' import { join } from 'pathe' import type { PinceauContext } from '@pinceau/core' import { writeOutput } from '@pinceau/core/utils' diff --git a/packages/core/src/utils/fs.ts b/packages/core/src/utils/fs.ts index cfb9e424..93d37a21 100644 --- a/packages/core/src/utils/fs.ts +++ b/packages/core/src/utils/fs.ts @@ -1,5 +1,5 @@ import { dirname } from 'pathe' -import type { PinceauContext, VirtualOutputs } from '../types' +import type { PinceauContext } from '../types' /** * Write a virtual output. diff --git a/packages/palette/package.json b/packages/palette/package.json index ec091dff..0aae0b79 100644 --- a/packages/palette/package.json +++ b/packages/palette/package.json @@ -29,7 +29,7 @@ "theme.config.ts" ], "scripts": { - "stub": "pnpm run build:base", + "build:pre": "pnpm run build:base", "build:base": "vite build -c vite.config.base.ts && rm -rf dist", "build:react": "vite build -c vite.config.react.ts && rm -rf dist", "build:svelte": "vite build -c vite.config.svelte.ts && rm -rf dist", diff --git a/packages/palette/vite.config.base.ts b/packages/palette/vite.config.base.ts index faf034fd..81f872ac 100644 --- a/packages/palette/vite.config.base.ts +++ b/packages/palette/vite.config.base.ts @@ -1,10 +1,19 @@ +import fs from 'node:fs' import path from 'node:path' import { createResolver } from '@nuxt/kit' import { defineConfig } from 'vite' +import type { ResolvedConfig } from 'vite' import Pinceau from 'pinceau/plugin' +import { getPinceauContext } from '@pinceau/core/utils' +import { createSveltePlugin } from '@pinceau/svelte/utils' +import { createReactPlugin } from '@pinceau/react/utils' +import { createVuePlugin } from '@pinceau/vue/utils' +import { configSourceFromModulePath } from '@pinceau/theme/utils' const resolve = createResolver(import.meta.url).resolve +let config: ResolvedConfig + export default defineConfig({ resolve: { alias: { @@ -23,5 +32,33 @@ export default defineConfig({ buildDir: path.join(__dirname, '../outputs/'), }, }), + { + name: '@pinceau/palette-end', + configResolved(_config) { + config = _config + }, + buildEnd() { + const ctx = getPinceauContext(config) + + const plugins = { + svelte: createSveltePlugin, + react: createReactPlugin, + vue: createVuePlugin, + } + + const outputsPath = configSourceFromModulePath('@pinceau/outputs', ctx)?.path + + Object.entries(plugins).forEach(([key, plugin]) => { + const pluginContent = plugin(ctx) + + if (outputsPath) { + fs.writeFileSync( + path.join(outputsPath, `${key}-plugin.js`), + pluginContent, + ) + } + }) + }, + }, ], }) diff --git a/packages/runtime/src/index.ts b/packages/runtime/src/index.ts index f8bfac57..a50009e0 100644 --- a/packages/runtime/src/index.ts +++ b/packages/runtime/src/index.ts @@ -1,4 +1,3 @@ -import { PinceauPluginsOptions } from '@pinceau/core' import type { PinceauRuntimeOptions } from './types' export * from './utils' diff --git a/packages/style/src/index.ts b/packages/style/src/index.ts index f1256f13..fc6795e0 100644 --- a/packages/style/src/index.ts +++ b/packages/style/src/index.ts @@ -1,4 +1,3 @@ -import { PinceauPluginsOptions } from '@pinceau/core' import type { PinceauStyleOptions } from './types' export * from './types' diff --git a/packages/theme/src/utils.ts b/packages/theme/src/utils.ts index 676f0f45..b978470e 100644 --- a/packages/theme/src/utils.ts +++ b/packages/theme/src/utils.ts @@ -13,3 +13,5 @@ export * from './formats' export * from './utils/tokens-transformers' export * from './utils/media-queries' export * from './utils/config-content' +export * from './utils/build-dir' +export * from './utils/module-path' diff --git a/packages/theme/src/utils/build-dir.ts b/packages/theme/src/utils/build-dir.ts new file mode 100644 index 00000000..2ce24065 --- /dev/null +++ b/packages/theme/src/utils/build-dir.ts @@ -0,0 +1,26 @@ +import { join } from 'pathe' +import type { PinceauContext } from '@pinceau/core' +import { configSourceFromModulePath } from './module-path' + +export function resolveBuildDir(ctx: PinceauContext) { + const { buildDir: optionsBuildDir } = ctx.options.theme + + let buildDir = optionsBuildDir + + // Enforce ending slash for buildDir + if (buildDir && !buildDir.endsWith('/')) { + buildDir += '/' + } + + // Try to resolve using common module path resolve + if (buildDir === undefined && ctx.options.cwd && ctx.options.resolve) { + buildDir = configSourceFromModulePath('@pinceau/outputs', ctx)?.path + } + + // Try to resolve using path join + if (buildDir === undefined && ctx.options.cwd) { + buildDir = join(ctx.options.cwd, 'node_modules/@pinceau/outputs/') + } + + return buildDir +} diff --git a/packages/theme/src/utils/config-file.ts b/packages/theme/src/utils/config-file.ts index 180287e2..5a7f6034 100644 --- a/packages/theme/src/utils/config-file.ts +++ b/packages/theme/src/utils/config-file.ts @@ -1,14 +1,10 @@ import fs from 'node:fs' import type { PinceauOptions } from '@pinceau/core' -import { message, parseAst } from '@pinceau/core/utils' +import { message } from '@pinceau/core/utils' import createJITI from 'jiti' import { resolve } from 'pathe' import type { ConfigFileImport, ConfigLayer, ResolvedConfigLayer } from '../types' -import { resolveMediaQueriesKeys } from './media-queries' import { getConfigLayer } from './config-layers' -import { resolveConfigDefinitions } from './config-definitions' -import { resolveConfigImports } from './config-imports' -import { resolveConfigUtils } from './config-utils' import { resolveConfigContent } from './config-content' /** diff --git a/packages/theme/src/utils/config-layers.ts b/packages/theme/src/utils/config-layers.ts index c26a5725..c61ca4c6 100644 --- a/packages/theme/src/utils/config-layers.ts +++ b/packages/theme/src/utils/config-layers.ts @@ -1,4 +1,3 @@ -import { dirname, extname } from 'node:path' import type { PinceauContext, PinceauOptions } from '@pinceau/core' import { merger } from '@pinceau/core/utils' import { resolveSchema as resolveUntypedSchema } from 'untyped' @@ -7,6 +6,7 @@ import type { ConfigLayer, ResolvedConfigLayer, Theme, ThemeLoadingOutput } from import { resolveFileLayer } from './config-file' import { normalizeTokens } from './tokens' import { resolveMediaQueriesKeys } from './media-queries' +import { configSourceFromModulePath } from './module-path' // Gives an empty layer for a given path or nothing. export function getConfigLayer(path?: string): ResolvedConfigLayer { @@ -87,30 +87,9 @@ export function resolveConfigSources( options: PinceauOptions, ctx: PinceauContext, ) { - const configSourceFromModulePath = (path: string): ConfigLayer | undefined => { - if (!ctx.resolve) { return } - - let pkgPath - try { - pkgPath = ctx.resolve(path) - } - catch (e) { - // TODO: Debug messages - } - - if (pkgPath) { - const dir = dirname(pkgPath) - const filename = pkgPath.replace(`${dir}/`, '') - const ext = extname(filename) - if (dir && filename && ext) { return { path: `${dir}/`, configFileName: filename.replace(ext, '') } } - } - - return undefined - } - // Inject palette if options set to true if (options.theme.palette) { - const paletteSource = configSourceFromModulePath('@pinceau/palette') + const paletteSource = configSourceFromModulePath('@pinceau/palette', ctx) if (paletteSource) { options.theme.layers.push(paletteSource) } } @@ -128,7 +107,7 @@ export function resolveConfigSources( if (typeof layerOrPath === 'string') { // Supports passing a package like `@pinceau/palette` if (!layerOrPath.startsWith('/')) { - const resolvedModuleSource = configSourceFromModulePath(layerOrPath) + const resolvedModuleSource = configSourceFromModulePath(layerOrPath, ctx) if (resolvedModuleSource) { configLayer = resolvedModuleSource } } diff --git a/packages/theme/src/utils/generate.ts b/packages/theme/src/utils/generate.ts index 4c454af4..78a7232b 100644 --- a/packages/theme/src/utils/generate.ts +++ b/packages/theme/src/utils/generate.ts @@ -1,4 +1,3 @@ -import { join } from 'node:path' import type { File, Core as Instance, Named, Transform } from 'style-dictionary-esm' import StyleDictionary from 'style-dictionary-esm' import { REFERENCES_REGEX, message } from '@pinceau/core/utils' @@ -6,6 +5,7 @@ import type { PinceauContext } from '@pinceau/core' import type { PinceauTheme } from '@pinceau/outputs' import type { DesignTokens, PinceauThemeFormat, Theme, ThemeGenerationOutput, ThemeLoadingOutput } from '../types' import { flattenTokens } from './tokens' +import { resolveBuildDir } from './build-dir' export async function generateTheme( loadedTheme: ThemeLoadingOutput, @@ -16,14 +16,9 @@ export async function generateTheme( // Get context const { theme } = loadedTheme - const { options } = ctx - let { buildDir } = options.theme - // Enforce ending slash for buildDir - if (buildDir && !buildDir.endsWith('/')) { buildDir += '/' } - - // Enforce buildDir when not set; `false` disables write - if (buildDir === undefined && ctx.options.cwd) { buildDir = join(ctx.options.cwd, 'node_modules/@pinceau/outputs/') } + // Resolve build dir path + const buildDir = resolveBuildDir(ctx) // Transforms used const usedTransforms = ['size/px', 'color/hex'] diff --git a/packages/theme/src/utils/module-path.ts b/packages/theme/src/utils/module-path.ts new file mode 100644 index 00000000..b9782fd8 --- /dev/null +++ b/packages/theme/src/utils/module-path.ts @@ -0,0 +1,25 @@ +import { dirname, extname } from 'pathe' +import type { PinceauContext } from '@pinceau/core' +import type { ConfigLayer } from '../types/config' + +export function configSourceFromModulePath(path: string, ctx: PinceauContext): ConfigLayer | undefined { + if (!ctx.resolve) { return } + + let pkgPath + try { + pkgPath = ctx.resolve(path) + } + catch (e) { + // TODO: Debug messages + // console.log({ e }) + } + + if (pkgPath) { + const dir = dirname(pkgPath) + const filename = pkgPath.replace(`${dir}/`, '') + const ext = extname(filename) + if (dir && filename && ext) { return { path: `${dir}/`, configFileName: filename.replace(ext, '') } } + } + + return undefined +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3ff65582..359efbf6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -533,9 +533,15 @@ importers: integrations/language-server: dependencies: - '@pinceau/integration': + '@pinceau/core': specifier: workspace:* - version: link:../../packages/integration + version: link:../../packages/core + '@pinceau/style': + specifier: workspace:* + version: link:../../packages/style + '@pinceau/theme': + specifier: workspace:* + version: link:../../packages/theme '@vue/compiler-sfc': specifier: ^3.3.4 version: 3.3.4 @@ -945,23 +951,34 @@ importers: packages/outputs: {} packages/palette: - dependencies: - '@pinceau/core': - specifier: workspace:* - version: link:../core devDependencies: '@pinceau/configs': specifier: workspace:* version: link:../configs + '@pinceau/core': + specifier: workspace:* + version: link:../core '@pinceau/outputs': specifier: workspace:* version: link:../outputs + '@pinceau/react': + specifier: workspace:* + version: link:../../integrations/react + '@pinceau/runtime': + specifier: workspace:* + version: link:../runtime '@pinceau/style': specifier: workspace:* version: link:../style + '@pinceau/svelte': + specifier: workspace:* + version: link:../../integrations/svelte '@pinceau/theme': specifier: workspace:* version: link:../theme + '@pinceau/vue': + specifier: workspace:* + version: link:../../integrations/vue pinceau: specifier: workspace:* version: link:../../integrations/pinceau diff --git a/turbo.json b/turbo.json index 457b5c1c..f056ad1f 100644 --- a/turbo.json +++ b/turbo.json @@ -4,8 +4,12 @@ "stub": { "outputs": ["dist/**"] }, + "build:pre": { + "dependsOn": ["^stub"], + "outputs": ["dist/**"] + }, "build": { - "dependsOn": ["^build"], + "dependsOn": ["^build:pre", "^stub", "^build"], "outputs": ["dist/**"] }, "clean": {