From 4f5845a3182fc950eb9cd76d7161698383113b18 Mon Sep 17 00:00:00 2001 From: pacexy Date: Thu, 16 Jan 2025 16:41:58 +0800 Subject: [PATCH] docs: unify `__dirname` usage (#19176) --- docs/guide/api-environment-frameworks.md | 5 +++++ docs/guide/build.md | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/docs/guide/api-environment-frameworks.md b/docs/guide/api-environment-frameworks.md index 6c16d934486ddd..00493c0c844129 100644 --- a/docs/guide/api-environment-frameworks.md +++ b/docs/guide/api-environment-frameworks.md @@ -46,8 +46,13 @@ The `runner` is evaluated eagerly when it's accessed for the first time. Beware Given a Vite server configured in middleware mode as described by the [SSR setup guide](/guide/ssr#setting-up-the-dev-server), let's implement the SSR middleware using the environment API. Error handling is omitted. ```js +import fs from 'node:fs' +import path from 'node:path' +import { fileURLToPath } from 'node:url' import { createServer } from 'vite' +const __dirname = path.dirname(fileURLToPath(import.meta.url)) + const server = await createServer({ server: { middlewareMode: true }, appType: 'custom', diff --git a/docs/guide/build.md b/docs/guide/build.md index e08ad32b247f72..a91ce95906c721 100644 --- a/docs/guide/build.md +++ b/docs/guide/build.md @@ -106,9 +106,12 @@ During dev, simply navigate or link to `/nested/` - it works as expected, just l During build, all you need to do is to specify multiple `.html` files as entry points: ```js twoslash [vite.config.js] -import { resolve } from 'path' +import { dirname, resolve } from 'node:path' +import { fileURLToPath } from 'node:url' import { defineConfig } from 'vite' +const __dirname = dirname(fileURLToPath(import.meta.url)) + export default defineConfig({ build: { rollupOptions: { @@ -134,9 +137,12 @@ When it is time to bundle your library for distribution, use the [`build.lib` co ::: code-group ```js twoslash [vite.config.js (single entry)] -import { resolve } from 'path' +import { dirname, resolve } from 'node:path' +import { fileURLToPath } from 'node:url' import { defineConfig } from 'vite' +const __dirname = dirname(fileURLToPath(import.meta.url)) + export default defineConfig({ build: { lib: { @@ -162,9 +168,12 @@ export default defineConfig({ ``` ```js twoslash [vite.config.js (multiple entries)] -import { resolve } from 'path' +import { dirname, resolve } from 'node:path' +import { fileURLToPath } from 'node:url' import { defineConfig } from 'vite' +const __dirname = dirname(fileURLToPath(import.meta.url)) + export default defineConfig({ build: { lib: {