Skip to content

Commit

Permalink
feat(nuxt3): support extends
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Jul 28, 2022
1 parent afd6c34 commit 257f0b9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/nuxt-windicss/package.json
Expand Up @@ -39,6 +39,7 @@
},
"dependencies": {
"@nuxt/kit": "3.0.0-rc.6",
"@windicss/config": "^1.8.7",
"@windicss/plugin-utils": "^1.8.7",
"consola": "^2.15.3",
"defu": "^6.0.0",
Expand Down
41 changes: 40 additions & 1 deletion packages/nuxt-windicss/src/module.ts
@@ -1,6 +1,6 @@
import { readFileSync, writeFileSync } from 'fs'
import { ensureDirSync } from 'fs-extra'
import { join, relative } from 'pathe'
import { join, relative, resolve } from 'pathe'
import { createUtils } from '@windicss/plugin-utils'
import type { ResolvedOptions, UserOptions, WindiPluginUtils } from '@windicss/plugin-utils'
import type { Config } from 'windicss/types/interfaces'
Expand All @@ -16,6 +16,7 @@ import {
resolveModule,
tryRequireModule,
} from '@nuxt/kit'
import { loadConfiguration } from '@windicss/config'
import VitePluginWindicss from 'vite-plugin-windicss'
import type { NuxtModule } from '@nuxt/schema'
import { defu } from 'defu'
Expand Down Expand Up @@ -108,6 +109,22 @@ export default defineNuxtModule<ModuleOptions>({
return
}

// Add Nuxt 3 Layers paths
if (options.scan && nuxt.options._layers?.length > 1) {
options.scan = options.scan === true ? {} : options.scan
if (typeof options.scan.dirs === 'string')
options.scan.dirs = [options.scan.dirs]
if (typeof options.scan.exclude === 'string')
options.scan.exclude = [options.scan.exclude]
const nonSrcLayers = nuxtOptions._layers
.filter(l => l.cwd !== nuxt.options.srcDir)
options.scan.dirs?.push(...nonSrcLayers.map(l => l.cwd || ''))
const originalExclude = options.scan.exclude as string[]
options.scan.exclude?.push(...nonSrcLayers
.map(l => originalExclude.map(p => resolve(l.cwd || '', p)))
.flat())
}

// allow user to override the options with hooks
const ctxOnOptionsResolved = options.onOptionsResolved
options.onOptionsResolved = async (options: ResolvedOptions) => {
Expand Down Expand Up @@ -136,6 +153,28 @@ export default defineNuxtModule<ModuleOptions>({
nuxt.options.watch.push(configFilePath)
}

// allow layers to have their own windi config
if (nuxt.options._layers?.length > 1) {
nuxtOptions._layers
.filter(l => l.cwd !== nuxt.options.srcDir)
.forEach((l) => {
const { config, filepath } = loadConfiguration({
onConfigurationError: error => console.error(error),
onConfigurationNotFound: () => {},
root: l.cwd,
})
if (!filepath || !config)
return

if (nuxt.options.dev)
nuxt.options.watch.push(filepath)
// fix recursion
// delete config.plugins

windiConfig = defu(windiConfig, config)
})
}

// avoid being too verbose
if (options.displayVersionInfo && nuxt.options.dev) {
nuxt.hook('build:before', () => {
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 257f0b9

Please sign in to comment.