Skip to content

Rollup v4 Example

This example demonstrates usage with Rollup 4.x (latest version).

Features Tested

  • Default shebang preservation
  • chmod option
  • include/exclude patterns
  • skipBackslash option
  • warnOnMultiple option
  • Plugin API

Configuration

js
import replaceShebang from 'rollup-plugin-replace-shebang'

const plugin = replaceShebang({
  // Use default shebang from source
  chmod: true,
  // Skip backslash processing (useful for Windows paths)
  skipBackslash: true,
  // Include only source files
  include: ['src/**/*.js'],
  // Exclude test files
  exclude: ['src/**/*.test.js'],
  // Warn when multiple files have shebangs
  warnOnMultiple: true
})

export default {
  input: 'src/cli.js',
  output: {
    file: 'dist/cli.js',
    format: 'es'
  },
  plugins: [plugin]
}

Source Code

src/cli.js
js
#!/usr/bin/env node

import { greet, getVersion, getPluginInfo, getWindowsPath } from './utils.js'
import { formatOutput } from './formatter.js'

const args = process.argv.slice(2)
const command = args[0]

switch (command) {
  case 'greet':
    console.log(greet(args[1]))
    break
  case 'version':
    console.log(getVersion())
    break
  case 'info':
    console.log(formatOutput(getPluginInfo()))
    break
  case 'path':
    console.log('Windows path:', getWindowsPath())
    break
  default:
    console.log(`
Usage: cli <command> [args]

Commands:
  greet <name>    Greet someone
  version         Show version
  info            Show plugin info
  path            Show Windows path (skipBackslash demo)
`)
}

console.log('\nThis CLI was built with rollup v4.x')
src/utils.js
js
export function greet(name) {
  return `Hello, ${name || 'World'}!`
}

export function getVersion() {
  return '1.0.0'
}

export function getPluginInfo() {
  return {
    name: 'rollup-plugin-replace-shebang',
    version: '2.0.0',
    features: [
      'shebang replacement',
      'include/exclude patterns',
      'chmod support',
      'skipBackslash',
      'template variables',
      'plugin API'
    ]
  }
}

// Demo skipBackslash feature: Windows path with backslash
export function getWindowsPath() {
  return 'C:\\Users\\test\\file.txt'
}
src/formatter.js
js
export function formatOutput(data) {
  if (typeof data === 'object') {
    return JSON.stringify(data, null, 2)
  }
  return String(data)
}

Try It

bash
cd examples/rollup-v4
pnpm install
pnpm run build
./dist/cli.js info
./dist/cli.js path    # Demo skipBackslash

Output

{
  "name": "rollup-plugin-replace-shebang",
  "version": "2.0.0",
  "features": [
    "shebang replacement",
    "include/exclude patterns",
    "chmod support",
    "skipBackslash",
    "template variables",
    "plugin API"
  ]
}

This CLI was built with rollup v4.x

Try Online

Open in StackBlitz

Released under the MIT License.