Skip to content

Commit

Permalink
fix(vite): search for config from vite root
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Apr 21, 2022
1 parent e46a7f9 commit 98317c8
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 51 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export async function resolveOptions(options: CliOptions) {

export async function build(_options: CliOptions) {
const options = await resolveOptions(_options)
const loadConfig = createConfigLoader()
const loadConfig = createConfigLoader(process.cwd())
const { config, sources: configSources } = await loadConfig()

uno = createGenerator(
Expand Down
81 changes: 41 additions & 40 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,53 @@ import { createConfigLoader as createLoader } from 'unconfig'

export type { LoadConfigResult, LoadConfigSource }

export function createConfigLoader<U extends UserConfig>(configOrPath: string | U = process.cwd(), extraConfigSources: LoadConfigSource[] = []): () => Promise<LoadConfigResult<U>> {
export function createConfigLoader<U extends UserConfig>(_configOrPath: string | U, extraConfigSources: LoadConfigSource[] = []) {
let inlineConfig = {} as U

if (typeof configOrPath !== 'string') {
inlineConfig = configOrPath
if (inlineConfig.configFile === false) {
return async() => ({
config: inlineConfig as U,
sources: [],
})
return async(configOrPath = _configOrPath): Promise<LoadConfigResult<U>> => {
if (typeof configOrPath !== 'string') {
inlineConfig = configOrPath
if (inlineConfig.configFile === false) {
return {
config: inlineConfig as U,
sources: [],
}
}
else {
configOrPath = inlineConfig.configFile || process.cwd()
}
}
else {
configOrPath = inlineConfig.configFile || process.cwd()
}
}

const resolved = resolve(configOrPath)
let cwd = resolved
const resolved = resolve(configOrPath)
let cwd = resolved

let isFile = false
if (fs.existsSync(resolved) && fs.statSync(resolved).isFile()) {
isFile = true
cwd = dirname(resolved)
}
let isFile = false
if (fs.existsSync(resolved) && fs.statSync(resolved).isFile()) {
isFile = true
cwd = dirname(resolved)
}

const loader = createLoader<U>({
sources: isFile
? [
{
files: resolved,
extensions: [],
},
]
: [
{
files: [
'unocss.config',
'uno.config',
],
},
...extraConfigSources,
],
cwd,
defaults: inlineConfig,
})
const loader = createLoader<U>({
sources: isFile
? [
{
files: resolved,
extensions: [],
},
]
: [
{
files: [
'unocss.config',
'uno.config',
],
},
...extraConfigSources,
],
cwd,
defaults: inlineConfig,
})

return async() => {
const result = await loader.load()
result.config = result.config || inlineConfig
if (result.config.configDeps) {
Expand All @@ -61,6 +61,7 @@ export function createConfigLoader<U extends UserConfig>(configOrPath: string |
...result.config.configDeps.map(i => resolve(cwd, i)),
]
}

return result
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ export interface ConfigBase<Theme extends {} = {}> {
extendTheme?: Arrayable<ThemeExtender<Theme>>

/**
* Addtional options for auto complete
* Additional options for auto complete
*/
autocomplete?: {
/**
Expand Down Expand Up @@ -440,6 +440,9 @@ export interface UnocssPluginContext<Config extends UserConfig = UserConfig> {

invalidate: () => void
onInvalidate: (fn: () => void) => void

root: string
updateRoot: (root: string) => Promise<LoadConfigResult<Config>>
}

export interface SourceMap {
Expand Down
21 changes: 17 additions & 4 deletions packages/plugins-common/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export function createContext<Config extends UserConfig = UserConfig>(
extraConfigSources: LoadConfigSource[] = [],
resolveConfigResult: (config: LoadConfigResult<Config>) => void = () => {},
): UnocssPluginContext<Config> {
const loadConfig = createConfigLoader(configOrPath, extraConfigSources)
let root = process.cwd()
const loadConfig = createConfigLoader(configOrPath || root, extraConfigSources)

let rawConfig = {} as Config
const uno = createGenerator(rawConfig, defaults)
Expand All @@ -23,10 +24,10 @@ export function createContext<Config extends UserConfig = UserConfig>(
const modules = new BetterMap<string, string>()
const tokens = new Set<string>()

const ready = reloadConfig()
let ready = reloadConfig()

async function reloadConfig() {
const result = await loadConfig()
const result = await loadConfig(configOrPath || root)
resolveConfigResult(result)

rawConfig = result.config
Expand All @@ -43,6 +44,14 @@ export function createContext<Config extends UserConfig = UserConfig>(
return result
}

async function updateRoot(newRoot: string) {
if (newRoot !== root) {
root = newRoot
ready = reloadConfig()
}
return await ready
}

function invalidate() {
invalidations.forEach(cb => cb())
}
Expand All @@ -66,7 +75,9 @@ export function createContext<Config extends UserConfig = UserConfig>(
}

return {
ready,
get ready() {
return ready
},
tokens,
modules,
invalidate,
Expand All @@ -78,5 +89,7 @@ export function createContext<Config extends UserConfig = UserConfig>(
uno,
extract,
getConfig,
root,
updateRoot,
}
}
4 changes: 2 additions & 2 deletions packages/vite/src/config-hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export function ConfigHMRPlugin(ctx: UnocssPluginContext): Plugin | undefined {
const { ready, uno } = ctx
return {
name: 'unocss:config',
async configResolved() {
await ready
async configResolved(config) {

This comment has been minimized.

Copy link
@jeffrey-bin

jeffrey-bin Apr 22, 2022

this commit changed the detection root of vscode package, which yield a 'Make sure you have unocss.config.js in your workspace root,' warnig message that cause unocss vscode extention disabled.

This comment has been minimized.

Copy link
@antfu

antfu Apr 22, 2022

Author Member

Can you share a repro of your project structure? And let's discuss in an issue.

await ctx.updateRoot(config.root)
},
async configureServer(server) {
uno.config.envMode = 'dev'
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export default function UnocssPlugin(
const mode = inlineConfig.mode ?? 'global'

const plugins = [
ConfigHMRPlugin(ctx),
...initTransformerPlugins(ctx),
...createDevtoolsPlugin(ctx),
ConfigHMRPlugin(ctx),
]

if (inlineConfig.inspector !== false)
Expand Down
2 changes: 2 additions & 0 deletions playground/src/auto-imports.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ declare global {
const createApp: typeof import('vue')['createApp']
const createEventHook: typeof import('@vueuse/core')['createEventHook']
const createGlobalState: typeof import('@vueuse/core')['createGlobalState']
const createInjectionState: typeof import('@vueuse/core')['createInjectionState']
const createReactiveFn: typeof import('@vueuse/core')['createReactiveFn']
const createSharedComposable: typeof import('@vueuse/core')['createSharedComposable']
const createUnrefFn: typeof import('@vueuse/core')['createUnrefFn']
Expand Down Expand Up @@ -85,6 +86,7 @@ declare global {
const toRef: typeof import('vue')['toRef']
const toRefs: typeof import('vue')['toRefs']
const triggerRef: typeof import('vue')['triggerRef']
const tryOnBeforeMount: typeof import('@vueuse/core')['tryOnBeforeMount']
const tryOnBeforeUnmount: typeof import('@vueuse/core')['tryOnBeforeUnmount']
const tryOnMounted: typeof import('@vueuse/core')['tryOnMounted']
const tryOnScopeDispose: typeof import('@vueuse/core')['tryOnScopeDispose']
Expand Down
7 changes: 5 additions & 2 deletions playground/src/components.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// generated by unplugin-vue-components
// We suggest you to commit this file into source control
// Read more: https://github.com/vuejs/vue-next/pull/3399
import '@vue/runtime-core'

declare module 'vue' {
declare module '@vue/runtime-core' {
export interface GlobalComponents {
CodeMirror: typeof import('./../../packages/inspector/client/components/CodeMirror.vue')['default']
Editor: typeof import('./components/Editor.vue')['default']
Expand All @@ -15,10 +16,12 @@ declare module 'vue' {
Playground: typeof import('./components/Playground.vue')['default']
Preview: typeof import('./components/Preview.vue')['default']
ReplPlayground: typeof import('./../../packages/inspector/client/components/ReplPlayground.vue')['default']
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
Sidebar: typeof import('./../../packages/inspector/client/components/Sidebar.vue')['default']
StatusBar: typeof import('./../../packages/inspector/client/components/StatusBar.vue')['default']
TitleBar: typeof import('./../../packages/inspector/client/components/TitleBar.vue')['default']
}
}

export { }
export {}

0 comments on commit 98317c8

Please sign in to comment.