Skip to content

Latest commit

 

History

History
160 lines (113 loc) · 8.58 KB

6.output.md

File metadata and controls

160 lines (113 loc) · 8.58 KB
title Build Output
description The files that the build writes to `dist`, and the size report it prints.

This page describes each file that the build writes to dist, and the size report that it prints. A module with src/module.ts, src/runtime/, and a ./src/utils entry builds into this layout:

dist/
├── module.mjs       # src/module.ts, bundled
├── module.d.mts     # its declarations
├── module.json      # the module's meta
├── types.d.mts      # the types that `exports["."].types` points at
├── utils.mjs        # src/utils.ts, bundled
├── utils.d.mts
└── runtime/         # src/runtime/, file by file

The output is ES modules only. Set sourcemap in tsdown.config.ts, or pass --sourcemap, to emit a source map next to every script.

module.mjs

module.mjs is src/module.ts, bundled with tsdown. It's compiled for the lowest Node version in your module's engines.node field. Without that field, its syntax is kept as written.

Imports of the runtime directory, and of other directory entries, point at their output in dist. The build doesn't bundle them. See Configuration for the packages that stay external.

module.json

module.json holds the module's meta. It contains the meta of the module definition, and the versions of @nuxt/module-builder and tsdown that built it. When the definition doesn't set name or version, they come from package.json. The build loads dist/module.mjs to read the meta.

{
  "name": "my-module",
  "configKey": "myModule",
  "compatibility": {
    "nuxt": ">=4.0.0"
  },
  "version": "1.0.0",
  "builder": {
    "@nuxt/module-builder": "2.0.0",
    "tsdown": "0.23.7"
  }
}

types.d.mts

types.d.mts holds the types of the package's root export. It re-exports the value exports of module.d.mts, including its default export. It re-exports the type-only exports of module.d.mts as types. It also adds the type exports of src/module.ts to Nuxt's own types:

import type { ModuleHooks, ModuleRuntimeHooks, ModuleRuntimeConfig, ModulePublicRuntimeConfig } from './module.mjs'

declare module '#app' {
  interface RuntimeNuxtHooks extends ModuleRuntimeHooks {}
}

declare module '@nuxt/schema' {
  interface NuxtHooks extends ModuleHooks {}
  interface RuntimeConfig extends ModuleRuntimeConfig {}
  interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
}

export { type ModuleHooks, type ModuleOptions, type ModulePublicRuntimeConfig, type ModuleRuntimeConfig, type ModuleRuntimeHooks, default } from './module.mjs'

When src/module.ts doesn't export ModuleOptions, types.d.mts infers it from the default export.

nuxt-module-build prepare writes the same augmentations for your sources to .nuxt/module-builder/types.d.mts. That file re-exports src/module.ts. With source exports, the root export's types condition points at it while you develop.

Runtime files

The build emits every file under src/runtime/ under dist/runtime/. It keeps each file's name, apart from the extension:

Source Output
Scripts: .ts, .mts, .cts, .js, .mjs, .cjs .js, with a .d.ts declaration
.tsx, .jsx .jsx, with types stripped and the JSX kept, and a .d.ts declaration. See JSX.
.vue .vue, with TypeScript stripped, and .d.vue.ts and .vue.d.ts declarations
.css .css, with nesting flattened, vendor prefixes added for the project's browserslist targets, and minified
.scss, .sass .css, compiled with your module's sass or sass-embedded. Partials (_name.scss) aren't emitted.
Anything else, including .d.ts Copied as it is

Test and story files (*.spec.ts, *.test.ts, *.stories.ts, and their other script extensions) are left out.

Scripts are transpiled with oxc, one file at a time. Each keeps its own imports and exports:

  • Relative imports get the emitted extension: ../utils/merge becomes ../utils/merge.js, and ./styles.scss becomes ./styles.css.
  • Non-relative imports (#imports, #build/*, and packages) are kept as written.
  • Imports used only as types are removed.
  • TypeScript directive comments, such as // @ts-expect-error and // @ts-ignore, are removed. Other comments, such as JSDoc, are kept.

A relative import of a file outside src/runtime/ emits that file too, where the import points. For example, src/shared/format.ts becomes dist/shared/format.js. When that path would leave dist, or take the name of another output, the file goes under dist/_external/ instead. A file that is only imported for its types gets a declaration and no script.

.css @import statements are kept as written. The build doesn't apply postcss configuration to runtime stylesheets. Process them in the Nuxt app, or before the build.

Other directory entries are emitted in the same way, under their own outDir. Their scripts get .mjs and .d.mts, unless their ext is js. In every directory, a file whose JSX is kept is emitted as .jsx with a .d.ts.

JSX

.tsx and .jsx files keep their JSX, so the app that installs the module compiles it with its own Vue JSX plugin. Refer to these files without an extension:

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

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

    addComponent({
      name: 'MyBanner',
      filePath: resolver.resolve('./runtime/app/components/Banner'),
    })
  },
})

The path then resolves to the .tsx source when package.json points at your sources, and to the .jsx output in dist.

Nitro and Node don't transform files in node_modules. JSX that runs there is compiled to vue/jsx-runtime calls instead, and the file is emitted as .js. This applies to:

  • Scripts under a server/ directory.
  • Runtime files that these scripts or a bundled entry, such as src/module.ts, import values from through relative paths.

An import through an alias or through the module's own package name isn't followed.

Runtime declarations

The build rewrites the declarations of runtime files, so they resolve in the app that installs the module:

  • Relative imports get the extension of the file they resolve to, for node16 and nodenext module resolution.
  • #app imports become nuxt/app.
  • Other Nuxt aliases, such as #build/* and #imports, are kept. They resolve against the .nuxt of the app that installs the module.

See TypeScript for how declarations are generated, and the errors that fail the build.

Size report

Every build prints the size of its output with tsdown's report. When src/runtime/ or another directory entry exists, the report is a summary, followed by one line for each bundled entry and transpiled directory, with the size of its code and of its declarations:

ℹ 22 files, total: 5.11 kB
ℹ dist/utils.mjs   74 B · types 47 B
ℹ dist/module.mjs  436 B · types 589 B
ℹ dist/runtime/    1.18 kB · types 2.78 kB · 18 files

Otherwise, it has a line for each file. module.json and types.d.mts are written after the report, so they aren't in it. Under tsdown --watch, the summary of a rebuild only counts the files it wrote, while the lines for each entry and directory cover the whole build.

Set report in tsdown.config.ts to change it: { summary: true } or { summary: false }, { gzip: false } to skip compressing each file, { brotli: true }, or false to turn it off. The lines for each entry and directory follow a summary only.

To check the published size in CI, pack the module with the package manager that publishes it, into a temporary directory. Then list the tarball with npm pack --dry-run --json, which gives each file's size, plus the gzipped tarball's size and the unpackedSize:

pack_dir=$(mktemp -d)
pnpm pack --pack-destination "$pack_dir"
npm pack --dry-run --json "$pack_dir"/*.tgz | jq '.[0] | { size, unpackedSize, files: [.files[] | { path, size }] }'

With package.json pointing at dist, npm pack --pack-destination "$pack_dir" packs it the same way.