docs: remove __dirname and __filename from example code (#4381)

* docs: remove `__dirname` and `__filename` from example code

* fix: add `fileURLToPath`

* Update packages/schema/src/config/server.ts

Co-authored-by: pooya parsa <pyapar@gmail.com>

* Update docs/content/2.guide/6.going-further/7.testing.md

Co-authored-by: pooya parsa <pyapar@gmail.com>

Co-authored-by: pooya parsa <pyapar@gmail.com>
This commit is contained in:
Daniel Roe 2022-04-19 15:46:14 +01:00 committed by GitHub
parent 90da56a03b
commit 74ce29b329
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 20 additions and 23 deletions

View File

@ -193,15 +193,15 @@ Imagine a directory structure like this:
Then in `awesome-ui/nuxt.js` you can use the `components:dirs` hook: Then in `awesome-ui/nuxt.js` you can use the `components:dirs` hook:
```js ```js
import { join } from 'node:path'
import { defineNuxtModule } from '@nuxt/kit' import { defineNuxtModule } from '@nuxt/kit'
import { fileURLToPath } from 'node:url'
export default defineNuxtModule({ export default defineNuxtModule({
hooks: { hooks: {
'components:dirs'(dirs) { 'components:dirs'(dirs) {
// Add ./components dir to the list // Add ./components dir to the list
dirs.push({ dirs.push({
path: join(__dirname, 'components'), path: fileURLToPath(new URL('./components', import.meta.url)),
prefix: 'awesome' prefix: 'awesome'
}) })
} }

View File

@ -45,11 +45,11 @@ export default myPreset
Then in your `nuxt.config` you can specify that Nitro should use your custom preset: Then in your `nuxt.config` you can specify that Nitro should use your custom preset:
```ts [nuxt.config.js|ts] ```ts [nuxt.config.js|ts]
import { resolve } from 'node:path' import { fileURLToPath } from 'node:url'
export default { export default {
nitro: { nitro: {
preset: resolve(__dirname, 'my-preset') preset: fileURLToPath(new URL('./my-preset', import.meta.url))
} }
} }
``` ```

View File

@ -253,23 +253,18 @@ const myLib = await import('my-lib').then(lib => lib.default || lib)
In ESM Modules, unlike CJS, `require`, `require.resolve`, `__filename` and `__dirname` globals are not available In ESM Modules, unlike CJS, `require`, `require.resolve`, `__filename` and `__dirname` globals are not available
and should be replaced with `import()` and `import.meta.filename`. and should be replaced with `import()` and `import.meta.filename`.
You can use [createCommonJS](https://github.com/unjs/mlly#createcommonjs) from `unjs/mlly` to create a CJS compatible context in ESM (or use an inline shim):
::code-group ::code-group
```js [mlly] ```js [Before]
import { createCommonJS } from 'mlly' import { join } from 'path'
const { __dirname, __filename, require } = createCommonJS(import.meta.url) const newDir = join(__dirname, 'new-dir')
``` ```
```js [manual] ```js [After]
import { fileURLToPath } from 'node:url' import { fileURLToPath } from 'node:url'
import { dirname } from 'node:path'
import { createRequire } from 'node:module'
const __filename = fileURLToPath(import.meta.url) const newDir = fileURLToPath(new URL('./new-dir', import.meta.url))
const __dirname = dirname(__filename)
const require = createRequire(import.meta.url)
``` ```
:: ::

View File

@ -165,6 +165,7 @@ We can create a test file and use the `rootDir` to test the fixture.
```ts ```ts
// basic.test.js // basic.test.js
import { describe, it } from 'vitest' import { describe, it } from 'vitest'
import { fileURLToPath } from 'node:url'
import { setup, $fetch } from '@nuxt/test-utils-edge' import { setup, $fetch } from '@nuxt/test-utils-edge'
describe('ssr', async () => { describe('ssr', async () => {

View File

@ -346,7 +346,7 @@ export default {
* // Will register file from project server-middleware directory to handle /server-middleware/* requires * // Will register file from project server-middleware directory to handle /server-middleware/* requires
* { path: '/server-middleware', handler: '~/server-middleware/index.js' }, * { path: '/server-middleware', handler: '~/server-middleware/index.js' },
* // We can create custom instances too, but only in development mode, they are ignored for the production bundle. * // We can create custom instances too, but only in development mode, they are ignored for the production bundle.
* { path: '/static2', handler: serveStatic(__dirname + '/static2') } * { path: '/static2', handler: serveStatic(fileURLToPath(new URL('./static2', import.meta.url))) }
* ] * ]
* ``` * ```
* *
@ -516,12 +516,11 @@ export default {
* *
* @example * @example
* ```js * ```js
* import { resolve } from 'pathe'
* export default { * export default {
* alias: { * alias: {
* 'images': resolve(__dirname, './assets/images'), * 'images': fileURLToPath(new URL('./assets/images', import.meta.url),
* 'style': resolve(__dirname, './assets/style'), * 'style': fileURLToPath(new URL('./assets/style', import.meta.url),
* 'data': resolve(__dirname, './assets/other/data') * 'data': fileURLToPath(new URL('./assets/other/data', import.meta.url)
* } * }
* } * }
* ``` * ```

View File

@ -85,13 +85,14 @@ export default {
* *
* @example * @example
* ```js * ```js
* import { fileURLToPath } from 'url'
* export default { * export default {
* router: { * router: {
* extendRoutes(routes, resolve) { * extendRoutes(routes, resolve) {
* routes.push({ * routes.push({
* name: 'custom', * name: 'custom',
* path: '*', * path: '*',
* component: resolve(__dirname, 'pages/404.vue') * component: fileURLToPath(new URL('./pages/404.vue', import.meta.url))
* }) * })
* } * }
* } * }

View File

@ -4,12 +4,13 @@ export default {
* Whether to enable HTTPS. * Whether to enable HTTPS.
* *
* @example * @example
* ```js * ```
* import { fileURLToPath } from 'node:url'
* export default { * export default {
* server: { * server: {
* https: { * https: {
* key: fs.readFileSync(path.resolve(__dirname, 'server.key')), * key: fs.readFileSync(fileURLToPath(new URL('./server.key', import.meta.url))),
* cert: fs.readFileSync(path.resolve(__dirname, 'server.crt')) * cert: fs.readFileSync(fileURLToPath(new URL('./server.crt', import.meta.url)))
* } * }
* } * }
* } * }