Skip to content

Commit

Permalink
refactor: dynamic import vite helpers for using esm build
Browse files Browse the repository at this point in the history
Close #21
  • Loading branch information
Julien-R44 committed Feb 11, 2024
1 parent 434afcb commit 2b373a4
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path'
import { cwd } from 'node:process'
import { createConfigLoader as createLoader } from 'unconfig'
import { type ConfigEnv, type Plugin, type UserConfig, loadEnv, normalizePath } from 'vite'
import { type ConfigEnv, type Plugin, type UserConfig } from 'vite'

import { initUi, type UI } from './ui.js'
import { zodValidation } from './validators/zod/index.js'
Expand Down Expand Up @@ -77,6 +77,12 @@ async function validateEnv(
envConfig: ConfigEnv,
inlineOptions?: PluginOptions,
) {
/**
* Dynamic import of Vite helpers to using the ESM build of Vite and
* avoiding CJS since it will be deprecated
* See : https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated
*/
const { normalizePath, loadEnv } = await import('vite')
const rootDir = userConfig.root || cwd()

const resolvedRoot = normalizePath(
Expand All @@ -103,7 +109,7 @@ async function validateEnv(
}
}

async function validateAndLog(ui: UI, env: ReturnType<typeof loadEnv>, options: PluginOptions) {
async function validateAndLog(ui: UI, env: Record<string, string>, options: PluginOptions) {
const { schema, validator } = getNormalizedOptions(options)
const showDebug = shouldLogVariables(options)
const validate = { zod: zodValidation, builtin: builtinValidation }[validator]
Expand Down

0 comments on commit 2b373a4

Please sign in to comment.