unplugin-define
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

unplugin-define NPM version

Replace variables in code with other values or expressions. Supports Vite, Rollup, Webpack, Rspack and more.

NOTE

The original intention of this plugin is to provide compatibility for lower-level plugins. You should give priority to using the define that comes with the build tool.
such as:
Vite define

Install

npm i unplugin-define

See example: example/

Usage

UnpluginDefine([
  {
  // include: [],
  // exclude: [],
    replacements: {
      'process.env.NODE_ENV': '\'development\'',
      'API_PREFIX': '/api',
      'TRUE': true,
      'TRUE_STRING': 'true',
      'UNDEFINED': undefined,
      'UNDEFINED_STRING': 'undefined',
    }
  },
// ...
])
// input
if (process.env.NODE_ENV === 'development')
  console.log('run in development mode')

fetch('API_PREFIX/test')

console.log(TRUE === TRUE_STRING)
console.log(UNDEFINED === UNDEFINED_STRING)
// output
if ('development' === 'development')
  console.log('run in development mode')

fetch('/api/test')

console.log(true === true)
console.log(undefined === undefined)
Vite
// vite.config.ts
import UnpluginDefine from 'unplugin-define/vite'

export default defineConfig({
  plugins: [
    UnpluginDefine({
      // ...
    }),
  ],
})


Rollup
// rollup.config.js
import UnpluginDefine from 'unplugin-define/rollup'

export default {
  plugins: [
    UnpluginDefine({
      // ...
    }),
  ],
}


Webpack
// webpack.config.js
const UnpluginDefine = require('unplugin-define/webpack')

module.exports = {
  plugins: [
    UnpluginDefine({
      // ...
    }),
  ]
}


Nuxt
// nuxt.config.js
export default {
  buildModules: [
    ['unplugin-define/nuxt', {
      // ...
    }],
  ],
}

This module works for both Nuxt 2 and Nuxt Vite


Vue CLI
// vue.config.js
const UnpluginDefine = require('unplugin-define/webpack')

module.exports = {
  configureWebpack: {
    plugins: [
      UnpluginDefine({
        // ...
      }),
    ],
  },
}


esbuild
// esbuild.config.js
import { build } from 'esbuild'
import UnpluginDefine from 'unplugin-define/esbuild'

build({
  plugins: [
    UnpluginDefine({
      // ...
    }),
  ],
})


Rspack
// rspack.config.js
const UnpluginDefine = require('unplugin-define/rspack')

module.exports = defineConfig({
  plugins: [
    UnpluginDefine({
      // ...
    }),
  ]
})


Package Sidebar

Install

npm i unplugin-define

Weekly Downloads

3

Version

0.1.2

License

MIT

Unpacked Size

8.83 MB

Total Files

77

Last publish

Collaborators

  • xiankq