Skip to content

Latest commit

 

History

History
211 lines (164 loc) · 9.59 KB

7.exports.md

File metadata and controls

211 lines (164 loc) · 9.59 KB
title Exports
description Publish more entry points than the module itself, such as utilities, composables, and components.

The root export of a module is its definition, dist/module.mjs, with dist/types.d.mts for its types. The root also re-exports the named values and types that src/module.ts exports. Anything else that you want users to import by name needs its own subpath in the exports of package.json.

The examples on this page show the published exports, which point at dist. With source exports, put them in publishConfig.exports, and point each subpath of exports at its source. See Source exports.

Bundled entries

A bundled entry, such as ./src/utils in builder({ entries }), builds into dist/<name>.mjs and dist/<name>.d.mts. Export it with both:

{
  "exports": {
    ".": {
      "types": "./dist/types.d.mts",
      "import": "./dist/module.mjs"
    },
    "./utils": {
      "types": "./dist/utils.d.mts",
      "import": "./dist/utils.mjs"
    }
  }
}

Users then import it as my-module/utils. A bundled entry suits code that runs alongside the module, such as helpers for nuxt.config or for other modules:

import { defineMyModuleOptions } from 'my-module/utils'

export default defineNuxtConfig({
  modules: ['my-module'],
  myModule: defineMyModuleOptions({ greeting: 'Hi' }),
})

TypeScript reads the types condition of each export under the bundler and node16 module resolution, which Nuxt apps use. node10 module resolution ignores exports, and these docs don't cover it. If your module keeps a top-level types field or typesVersions for it, see Update the paths in package.json in the upgrade guide.

Runtime files

You can export files in src/runtime/ too, from their output in dist/runtime/. An app can then import them explicitly, as well as through whatever the module registers for auto-import.

To export a set of components, re-export them from an index.ts in the same directory:

export { default as Button } from './Button.vue'
export { default as Card } from './Card.vue'

Then point an export at its output:

{
  "exports": {
    ".": {
      "types": "./dist/types.d.mts",
      "import": "./dist/module.mjs"
    },
    "./components": {
      "types": "./dist/runtime/app/components/index.d.ts",
      "import": "./dist/runtime/app/components/index.js"
    }
  }
}

An app that installs the module can now import each component with its full type:

<script setup lang="ts">
import { Button } from 'my-module/components'
</script>

<template>
  <Button label="Save" />
</template>

To register the same components for auto-import, pass the index to addComponentExports from @nuxt/kit. Each named export becomes a component, prefixed with prefix:

import { addComponentExports, createResolver, defineNuxtModule } from '@nuxt/kit'

export default defineNuxtModule({
  meta: { name: 'my-module', configKey: 'myModule' },
  setup() {
    const resolver = createResolver(import.meta.url)

    addComponentExports({
      filePath: resolver.resolve('./runtime/app/components/index'),
      prefix: 'My',
    })
  },
})

<MyButton> and <MyCard> are then available without an import.

Composables work the same way. Export them from a file in the runtime directory, and point a subpath at its output:

export { useGreeting } from './useGreeting'
{
  "exports": {
    "./composables": {
      "types": "./dist/runtime/app/composables/index.d.ts",
      "import": "./dist/runtime/app/composables/index.js"
    }
  }
}

To register them for auto-import, pass each one to addImports, or pass their directory to addImportsDir:

import { addImports, createResolver, defineNuxtModule } from '@nuxt/kit'

export default defineNuxtModule({
  meta: { name: 'my-module', configKey: 'myModule' },
  setup() {
    const resolver = createResolver(import.meta.url)

    addImports({
      name: 'useGreeting',
      from: resolver.resolve('./runtime/app/composables/useGreeting'),
    })
  },
})

::note With exports that point at dist, these subpaths resolve once the module has been built. Source exports resolve them to your sources while you develop. ::

Source exports

With source exports, exports point at your sources, and publishConfig.exports holds the paths in dist. The playground, and anything else that installs the module from the workspace, then loads every export from src/ without a build:

{
  "exports": {
    ".": {
      "types": "./.nuxt/module-builder/types.d.mts",
      "import": "./src/module.ts"
    },
    "./utils": "./src/utils.ts",
    "./components": "./src/runtime/app/components/index.ts"
  },
  "publishConfig": {
    "exports": {
      ".": {
        "types": "./dist/types.d.mts",
        "import": "./dist/module.mjs"
      },
      "./utils": {
        "types": "./dist/utils.d.mts",
        "import": "./dist/utils.mjs"
      },
      "./components": {
        "types": "./dist/runtime/app/components/index.d.ts",
        "import": "./dist/runtime/app/components/index.js"
      }
    }
  }
}
  • Each subpath of exports points at the source file that the build turns into the matching path of publishConfig.exports.
  • publishConfig.exports replaces exports as a whole, so it repeats every subpath.
  • The root export's types condition points at the declarations that nuxt-module-build prepare writes. The type augmentations of src/module.ts then apply in the playground.
  • A subpath has no augmentations, so it points at its source alone.
  • A subpath that code outside Nuxt imports, such as a plugin for a plain Vite app, can keep pointing at dist in exports. That code then loads the built file. Build the module before you use it.

The build checks the paths in publishConfig.exports, not the source paths in exports. See Checks on published paths.

Which paths point at sources

A subpath points at its source when only Nuxt loads it: an app, through the module, its nuxt.config, or a runtime file. It points at dist when Node or a tool that can't read TypeScript loads it, such as a plain Vite app's vite.config.ts, an ESLint config, or a CLI. Build the module before that code runs.

Each path of exports mirrors its path in publishConfig.exports:

  • A wildcard subpath with an extension in its dist target gets the source's extension: "./runtime/*": "./dist/runtime/*.js" becomes "./src/runtime/*.ts", for its types condition too. TypeScript substitutes the extension after it resolves the target, so a *.js target also finds the .ts files, but Node loads the target as written and finds no .js file there.
  • A wildcard subpath without an extension in its dist target, such as "./dist/runtime/components/*", takes the import's own extension. It points at the same path under src when every file it reaches keeps its name in dist, such as .vue and .css files, for its types condition too. When it also reaches scripts, which dist names .js, such as "./*": "./dist/*" or a "./runtime/*" for components and composables, it stays at dist.
  • A subpath written as a string stays a string: "./registry": "./dist/registry.mjs" becomes "./registry": "./src/registry.ts".
  • A subpath with only a types condition that points at a file outside dist, such as ./client.d.ts, stays as it is, in both fields.
  • A subpath for the module entry, such as "./module", points where the root export does.
  • An entry of imports points at the source of its target. ./dist/types.d.mts has none: point it at ./.nuxt/module-builder/types.d.mts, as the root export's types condition does.
  • main and module point at ./src/module.ts, with the dist paths in publishConfig.
  • Other fields, such as style, stay at dist. pnpm and Yarn 4 don't apply publishConfig to them. A style condition in exports follows exports, and points at the source stylesheet.

Subpaths that Node loads from source

prepare and the build check each bundled entry that exports or imports points at source, and the files it imports, because Node loads them. A subpath that points at a runtime file is left to the build, which transpiles it.

When Node loads a subpath from source, as a vite.config.ts or a script does, the file it reaches and the files that file imports follow the module code rules, such as JSON imports with an import attribute. To check a runtime file, import its subpath with Node from the module root:

node --input-type=module -e "await import('my-module/<subpath>')"

Importing your own exports

Runtime code and module code can import the module's own package name, such as my-module/utils. The build never bundles these imports, so they resolve to the published files in the app that installs the module.

While the build generates declarations, these imports resolve to your sources instead, so they type-check before dist exists. With source exports, the build reads the subpaths and their dist paths from publishConfig.exports.