All Projects → chengpeiquan → vite-plugin-banner

chengpeiquan / vite-plugin-banner

Licence: MIT license
A banner plugin for Vite. It can adds a banner to the top of each generated chunk.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to vite-plugin-banner

vite-plugin-compress
Compress your bundle + assets from Vite
Stars: ✭ 103 (+164.1%)
Mutual labels:  vite, vite-plugin
vite-plugin-md
Markdown with Vue for Vite
Stars: ✭ 289 (+641.03%)
Mutual labels:  vite, vite-plugin
vite-plugin-inspect
Inspect the intermediate state of Vite plugins
Stars: ✭ 491 (+1158.97%)
Mutual labels:  vite, vite-plugin
vite-plugin-webfont-dl
⚡ Webfont Download Vite Plugin - Make your Vite site load faster
Stars: ✭ 69 (+76.92%)
Mutual labels:  vite, vite-plugin
vite-plugin-dts
A vite plugin for generating `.d.ts` files.
Stars: ✭ 539 (+1282.05%)
Mutual labels:  vite, vite-plugin
vite-plugin-environment
Easily expose environment variables in Vite.js
Stars: ✭ 57 (+46.15%)
Mutual labels:  vite, vite-plugin
vite-plugin-relay
A vite plugin for Relay
Stars: ✭ 44 (+12.82%)
Mutual labels:  vite, vite-plugin
vite-plugin-sleep
a vite plugin you never need. slow devServer and slow HMR.
Stars: ✭ 77 (+97.44%)
Mutual labels:  vite, vite-plugin
vite-plugin-restart
Custom files/globs to restart Vite server
Stars: ✭ 92 (+135.9%)
Mutual labels:  vite, vite-plugin
vite-plugin-html-template
html template for vite, like html-webpack-plugin for webpack.
Stars: ✭ 97 (+148.72%)
Mutual labels:  vite, vite-plugin
vite-plugin-checker
💬 Vite plugin that provide checks of TypeScript, ESLint, vue-tsc, and more.
Stars: ✭ 338 (+766.67%)
Mutual labels:  vite, vite-plugin
vite-plugins
🌱 为社区尽一份绵薄之力
Stars: ✭ 63 (+61.54%)
Mutual labels:  vite, vite-plugin
vite-plugin-env-compatible
Environment Variables Compatible for vite(with vue-cli, create-react-app and so on)
Stars: ✭ 35 (-10.26%)
Mutual labels:  vite, vite-plugin
vite-plugin-radar
All in one analytics loader for vite
Stars: ✭ 64 (+64.1%)
Mutual labels:  vite, vite-plugin
vite-plugin-ssr
Like Next.js / Nuxt but as do-one-thing-do-it-well Vite plugin.
Stars: ✭ 1,703 (+4266.67%)
Mutual labels:  vite, vite-plugin
electron-vite-quick-start
⚡ Full stack uses Vite to run Electron application, including main process.
Stars: ✭ 45 (+15.38%)
Mutual labels:  vite
vue-components-lib-seed
🌱 a vue3.0 components library template. Vue3.0 组件库的次佳实践.
Stars: ✭ 153 (+292.31%)
Mutual labels:  vite
Dashboard
Custom your personal browser start page from some configurable components.
Stars: ✭ 619 (+1487.18%)
Mutual labels:  vite
django-vite
Integration of ViteJS in a Django project.
Stars: ✭ 201 (+415.38%)
Mutual labels:  vite
image-optimizer
A free and open source tool for optimizing images and vector graphics.
Stars: ✭ 740 (+1797.44%)
Mutual labels:  vite

vite-plugin-banner

GitHub stars



English | 简体中文

Features

Adds a banner to the top of each generated chunk.

Installation

Install the package from npm (or yarn, or pnpm).

npm install -D vite-plugin-banner

Options

Plugin Options Type Description Example
string The banner content Basic usage
ContentCallback See the type declarations below Add different banners
BannerPluginOptions See the type declarations below Optional parameter format

· Type Declarations:

/**
 * Some options from `vite.config.[ts|js]`
 * @since 0.2.0
 */
export interface BannerPluginOptions {
  /**
   * The comment content of the banner
   * @since ^0.6.0 support for `ContentCallback` types
   */
  content: string | ContentCallback

  /**
   * The output directory from the configuration of Vite.js
   * @default 'dist'
   */
  outDir?: string

  /**
   * Whether to print error messages to the console
   * @since 0.4.0
   * @default false
   */
  debug?: boolean

  /**
   * By default, the validity of the content will be verified.
   *
   * If set to `false`, no verification will be performed.
   * @see https://github.com/chengpeiquan/vite-plugin-banner/issues/13
   * @since 0.5.0
   * @default true
   */
  verify?: boolean
}

/**
 * Callback function to get the contents to be injected.(or not inject)
 * @since 0.6.0
 *
 * @param fileName - The name of the file currently being processed
 * @returns
 *  1. When a valid string is returned, it will become the banner content
 *  2. Returning a Falsy value will skip processing(e.g. `''`, `null`, `undefined`)
 *
 * @example
 * ```ts
 *  content: (fileName: string) => {
 *    // Or use switch statement
 *    return fileName.endsWith('.js')
 *      ? 'This message will inject into `js` files.'
 *      : 'This message will inject into other files.'
 *  }
 * ```
 */
export type ContentCallback = (fileName: string) => string | null | undefined

Usage

In most cases, just use the string format as a plugin option.

Basic usage

Add it to vite.config.ts

// vite.config.ts
import banner from 'vite-plugin-banner'
// Other dependencies...

export default defineConfig({
  plugins: [banner('This is the banner content.')],
})

When you run npm run build on your project, In the dist folder(Or the build.outDir in vite.config.ts you configured), Except for vendor files, all js and css files will add a banner to the top.

e.g. in app.b3a7772e.js:

/* This is the banner content. */
var e=Object.assign;import{M as t,d as a,u as r,c......

Advanced usage

Of course, the most ideal banner is related to your package information.

First, You need to update your package.json like this, For example, it contains such field content:

{
  "name": "chengpeiquan.com",
  "version": "0.1.0",
  "description": "My personal website, technology stack based on Vue.js 3.0, and Vite 2.0, use Server Side Generation.",
  "author": "chengpeiquan",
  "homepage": "https://chengpeiquan.com/"
}

Then, import the package.json into vite.config.ts, update the banner like this:

// vite.config.ts
import pkg from './package.json'
// Other dependencies...

export default defineConfig({
  plugins: [
    banner(
      `/**\n * name: ${pkg.name}\n * version: v${pkg.version}\n * description: ${pkg.description}\n * author: ${pkg.author}\n * homepage: ${pkg.homepage}\n */`
    ),
  ],
})

Run npm run build, you can see the banner become more detailed.

e.g. in app.6936be52.js:

/**
 * name: chengpeiquan.com
 * version: v0.1.0
 * description: My personal website, technology stack based on Vue.js 3.0, and Vite 2.0, use Server Side Generation.
 * author: chengpeiquan
 * homepage: https://chengpeiquan.com/
 */
var e=Object.assign;import{M as t,d as a,u as r,c......

Fun usage

If you want to make some banners that show your personality, you can make some interesting content from some online generators.

Such as:

// vite.config.ts
export default defineConfig({
  plugins: [
    banner(`
    ██   ██         ███████   ██      ██ ████████   ██    ██   ███████   ██     ██
    ░██  ░██        ██░░░░░██ ░██     ░██░██░░░░░   ░░██  ██   ██░░░░░██ ░██    ░██
    ░██  ░██       ██     ░░██░██     ░██░██         ░░████   ██     ░░██░██    ░██
    ░██  ░██      ░██      ░██░░██    ██ ░███████     ░░██   ░██      ░██░██    ░██
    ░██  ░██      ░██      ░██ ░░██  ██  ░██░░░░       ░██   ░██      ░██░██    ░██
    ░██  ░██      ░░██     ██   ░░████   ░██           ░██   ░░██     ██ ░██    ░██
    ░██  ░████████ ░░███████     ░░██    ░████████     ░██    ░░███████  ░░███████ 
    ░░   ░░░░░░░░   ░░░░░░░       ░░     ░░░░░░░░      ░░      ░░░░░░░    ░░░░░░░  
    `),
  ],
})

Run npm run build, e.g. in app.d9a287b8.js:

/*
    ██   ██         ███████   ██      ██ ████████   ██    ██   ███████   ██     ██
    ░██  ░██        ██░░░░░██ ░██     ░██░██░░░░░   ░░██  ██   ██░░░░░██ ░██    ░██
    ░██  ░██       ██     ░░██░██     ░██░██         ░░████   ██     ░░██░██    ░██
    ░██  ░██      ░██      ░██░░██    ██ ░███████     ░░██   ░██      ░██░██    ░██
    ░██  ░██      ░██      ░██ ░░██  ██  ░██░░░░       ░██   ░██      ░██░██    ░██
    ░██  ░██      ░░██     ██   ░░████   ░██           ░██   ░░██     ██ ░██    ░██
    ░██  ░████████ ░░███████     ░░██    ░████████     ░██    ░░███████  ░░███████
    ░░   ░░░░░░░░   ░░░░░░░       ░░     ░░░░░░░░      ░░      ░░░░░░░    ░░░░░░░
     */
var e=Object.assign;import{M as t,d as a,u as r,c......

Add different banners

Since 0.6.0, it supports incoming function callback to set the content of Banner. When using ContentCallback type, this plugin will judge what content should be added according to the internal logic of the function.

  1. When a valid string is returned, it will become the banner content
  2. Returning a Falsy value will skip processing(e.g. '', null, undefined)

e.g.

// vite.config.ts
import banner from 'vite-plugin-banner'
// Other dependencies...

export default defineConfig({
  plugins: [
    banner((fileName: string) => {
      // Or use switch statement
      return fileName.endsWith('.js')
        ? 'This message will inject into `js` files.'
        : 'This message will inject into other files.'
    }),
  ],
})

Optional parameter format

Sometimes plugins can't get outDir successfully (for example, in VitePress, the plugin get through viteConfig.build.outDir is always a .temp temporary directory, not the final output directory), so you need to manually specify the output directory to inform the plugin.

Take VitePress as an example:

// docs/.vitepress/config.ts
import { defineConfig } from 'vitepress'
import banner from 'vite-plugin-banner'
import pkg from '../../package.json'

const outDir = '../dist'

export default defineConfig({
  // Specify the output directory for packaging
  outDir,

  // Use Vite plugins
  vite: {
    plugins: [
      // Please remember to use the options in Object format here
      banner({
        outDir,
        content: `/**\n * name: ${pkg.name}\n * version: v${pkg.version}\n * description: ${pkg.description}\n * author: ${pkg.author}\n * homepage: ${pkg.homepage}\n */`,
      }),
    ],
  },
  // ...
})

In addition to outDir, you can also use debug, verify and other options, see the above Options description for details.

License

MIT License © 2021 chengpeiquan

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].