Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a page for explaining
import.meta
Documents the existence of `import.meta` and some of the properties that are available. Includes a warning that in nuxt module code there is limited support for `import.meta`.
- Loading branch information
1 parent
2832f01
commit 411e967
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
title: 'Import meta' | ||
description: Understand where your code is running using `import.meta`. | ||
--- | ||
|
||
## The `import.meta` object | ||
|
||
With ES modules you can obtain some metadata from the code that imports or compiles your ES-module. | ||
This is done through `import.meta`, which is an object that provides your code with this information. | ||
Throughout the Nuxt documentation you may see snippets that use this already to figure out whether the | ||
code is currently running on the client or server side. | ||
|
||
:read-more{to="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta"} | ||
|
||
## Properties | ||
|
||
Property | Type | Description | ||
--- | --- | --- | ||
`import.meta.client` | boolean | True when evaluated on the client-side, false otherwise. | ||
`import.meta.dev` | boolean | Equals `nuxt.config.dev` | ||
`import.meta.env` | object | Equals `process.env` | ||
`import.meta.server` | boolean | True when evaluated on the server-side, false otherwise. | ||
`import.meta.url` | string | Resolvable path for the current file. | ||
|
||
::callout{color="amber" icon="i-ph-warning-duotone"} | ||
In nuxt modules, only `import.meta.url` is supported. | ||
:: | ||
|
||
## Examples | ||
|
||
### Using `import.meta.url` to resolve files within your nuxt-module | ||
|
||
```js | ||
// /modules/my-module/index.ts | ||
|
||
// Resolve relative from the current file | ||
const {resolve} = createResolver(import.meta.url) | ||
|
||
export default defineNuxtModule({ | ||
meta: { name: 'myModule' }, | ||
setup() { | ||
addComponent({ | ||
name: 'MyModuleComponent', | ||
// Resolves to '/modules/my-module/components/MyModuleComponent.vue' | ||
filePath: resolve('./components/MyModuleComponent.vue'), | ||
}) | ||
}, | ||
}) | ||
``` |