unplugin-define
v0.1.2
Published
Replace variables in code with other values or expressions.Supports Vite, Rollup, Webpack, Rspack and more.
Downloads
10
Maintainers
Readme
unplugin-define
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.config.ts
import UnpluginDefine from 'unplugin-define/vite'
export default defineConfig({
plugins: [
UnpluginDefine({
// ...
}),
],
})
// rollup.config.js
import UnpluginDefine from 'unplugin-define/rollup'
export default {
plugins: [
UnpluginDefine({
// ...
}),
],
}
// webpack.config.js
const UnpluginDefine = require('unplugin-define/webpack')
module.exports = {
plugins: [
UnpluginDefine({
// ...
}),
]
}
// nuxt.config.js
export default {
buildModules: [
['unplugin-define/nuxt', {
// ...
}],
],
}
This module works for both Nuxt 2 and Nuxt Vite
// vue.config.js
const UnpluginDefine = require('unplugin-define/webpack')
module.exports = {
configureWebpack: {
plugins: [
UnpluginDefine({
// ...
}),
],
},
}
// esbuild.config.js
import { build } from 'esbuild'
import UnpluginDefine from 'unplugin-define/esbuild'
build({
plugins: [
UnpluginDefine({
// ...
}),
],
})
// rspack.config.js
const UnpluginDefine = require('unplugin-define/rspack')
module.exports = defineConfig({
plugins: [
UnpluginDefine({
// ...
}),
]
})