Skip to content

Repository files navigation

nuxt-remove-before-flight

CI npm version npm downloads Node.js License: MIT

A Nuxt module that removes code marked with // @remove-before-flight during production build only. In development the marked code runs as usual; in build it is stripped from the bundle. Source files on disk are never changed, so git history stays clean.

Use this when you want to keep dev-only code (debug logs, mocks, feature flags) in the same codebase but exclude it from production bundles.


Installation

  1. Install the package (use one of the following):

    npm install nuxt-remove-before-flight
    # or
    pnpm add nuxt-remove-before-flight
    # or
    yarn add nuxt-remove-before-flight
  2. Register the module in nuxt.config.ts:

    export default defineNuxtConfig({
        modules: ['nuxt-remove-before-flight'],
    })

Usage

Single line

Place // @remove-before-flight on one line. That line and the next line are removed at build time.

Example:

function init() {
    // @remove-before-flight
    console.log('Debug: only in dev')
    doRealInit()
}

After build, the result is equivalent to:

function init() {
    doRealInit()
}

Block

Use two // @remove-before-flight lines to mark a block. Everything from the first marker up to and including the second marker is removed at build time.

Example:

function setup() {
    // @remove-before-flight
    if (import.meta.dev) {
        useFakeBackend()
    }
    // @remove-before-flight
    useRealBackend()
}

After build:

function setup() {
    useRealBackend()
}

Rules:

  • Each pair of markers defines one block (first through second, inclusive).
  • An odd, unpaired marker is treated as single-line: that line and the next are removed.

Behavior

Environment Effect on marked code
Dev (nuxt dev) No removal; marked code runs as written.
Build (nuxt build) Marked single lines and blocks are removed before bundling.
Git No file writes; nothing to commit from this module.

Removal is done in memory via a Vite transform that runs only during build. The repository always keeps the original source.


Supported files

  • JS/TS: .ts, .js, .mjs, .cjs — the whole file is processed.
  • Vue: .vue — <script>, <script setup>, and <template> are processed. In templates use the HTML comment <!-- @remove-before-flight --> with the same single-line and block rules.

Configuration

To turn the module off:

// nuxt.config.ts
export default defineNuxtConfig({
    modules: ['nuxt-remove-before-flight'],
    removeBeforeFlight: {
        enabled: false,
    },
})

Development

Commands for contributors:

  • Build the module: npm run build — output is in dist/.
  • Run tests: npm run test — runs Vitest once.
  • Run tests in watch mode: npm run test:watch
  • Run the playground: npm run dev — starts the Nuxt app in playground/ using the local module.

License

MIT

About

Nuxt module that strips code marked with flag only during build; dev and git stay unchanged.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages