Skip to content

Commit

Permalink
fix: allow opt-out of the version info display via displayVersionInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Oct 19, 2021
1 parent 5ab1bcd commit 667dca5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
1 change: 1 addition & 0 deletions packages/nuxt-windicss/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const NAME = 'nuxt-windicss'
export const NUXT_CONFIG_KEY = 'windicss'
export const defaultWindiOptions: Partial<NuxtWindiOptions> = {
analyze: false,
displayVersionInfo: true,
scan: {
dirs: ['./'],
exclude: [
Expand Down
4 changes: 4 additions & 0 deletions packages/nuxt-windicss/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export type NuxtWindiOptions = Options & {
* Starts a server for Windi Analyze.
*/
analyze?: AnalyzeOptions
/**
* Shows nuxt-windicss and windicss versions on build:before hook.
*/
displayVersionInfo?: boolean
}

// pollyfill @todo nuxt/kit export
Expand Down
52 changes: 27 additions & 25 deletions packages/nuxt-windicss/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ import type { NuxtWindiOptions } from './interfaces'
import { NAME, NUXT_CONFIG_KEY, defaultWindiOptions } from './constants'
import { analyze } from '.'

/**
* Export package.json meta into the module
*/
export const defineModuleMeta = (): Record<string, any> => {
// __dirname by itself is CJS which we should avoid
const __dirname = new URL('.', import.meta.url).pathname

let meta = {
configKey: NUXT_CONFIG_KEY,
}
// it shouldn't fail but who knows
const pkg = tryRequireModule(`${__dirname}/../package.json`)
if (pkg) {
meta = {
...meta,
...pkg,
}
}
return meta
}

const defineNuxtWindiCSSModule = defineNuxtModule<NuxtWindiOptions>(nuxt => ({
name: NAME,
configKey: NUXT_CONFIG_KEY,
Expand Down Expand Up @@ -59,16 +80,18 @@ const defineNuxtWindiCSSModule = defineNuxtModule<NuxtWindiOptions>(nuxt => ({
// this hook is ran twice for some reason
if (configFilePath) {
clearRequireCache(configFilePath)
configType = relative(nuxtOptions.rootDir, configFilePath)
configType = `./${relative(nuxtOptions.rootDir, configFilePath)}`
// Restart Nuxt if windi file updates (for modules using windicss:config hook)
if (nuxt.options.dev)
nuxt.options.watch.push(configFilePath)
}

// avoid being too verbose
if (nuxt.options.dev && !nuxt.options.build) {
const { version } = requireModulePkg('windicss')
logger.info(`windicss@${version} running with config: ${configType}.`)
if (nuxtWindiOptions.displayVersionInfo && nuxt.options.dev) {
nuxt.hook('build:before', () => {
const { version } = requireModulePkg('windicss')
logger.info(`\`nuxt-windicss v${defineModuleMeta().version}\` using \`windicss v${version}\` running with config: \`${configType}\`.`)
})
}

passed = true
Expand Down Expand Up @@ -251,27 +274,6 @@ const defineNuxtWindiCSSModule = defineNuxtModule<NuxtWindiOptions>(nuxt => ({
},
}))

/**
* Export package.json meta into the module
*/
export const defineModuleMeta = () => {
// __dirname by itself is CJS which we should avoid
const __dirname = new URL('.', import.meta.url).pathname

let meta = {
configKey: NUXT_CONFIG_KEY,
}
// it shouldn't fail but who knows
const pkg = tryRequireModule(`${__dirname}/../package.json`)
if (pkg) {
meta = {
...meta,
...pkg,
}
}
return meta
}

defineNuxtWindiCSSModule.meta = defineModuleMeta()

export default defineNuxtWindiCSSModule
11 changes: 11 additions & 0 deletions packages/playground/nuxt-bridge/nuxt.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This file is auto generated by `nuxt prepare`
// Please do not manually modify this file.

/// <reference types="nuxt-windicss" />
/// <reference types="@nuxt/bridge-edge" />
/// <reference path=".nuxt/nitro.d.ts" />
/// <reference types="nuxt3" />
/// <reference path=".nuxt/nitro.d.ts" />
/// <reference types="unplugin-vue2-script-setup/types" />
/// <reference path=".nuxt/auto-imports.d.ts" />
export {}

0 comments on commit 667dca5

Please sign in to comment.